I have foo.xml, I would like to generate foo.xsd as it is generated with VisualStudio->Xml->CreateSchema, tried xsd.exe but results are not the same. (xsd.exe foo.xml)
How to call same command as VisualStudio->Xml->CreateSchema from command line?
Maybe one small example will help, notce type="xs:string" minOccurs="0" When xml is more complexed differences become huge.
xml:
<foo>
<x />
<y />
</foo>
VS:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="x" />
<xs:element name="y" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xsd.exe
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="x" type="xs:string" minOccurs="0" />
<xs:element name="y" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="foo" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Looks like it could be that Visual Studio is using XmlSchemaInference.InferSchema internally instead of xsd.exe? More information on XmlSchemaInference.InferSchema.
Related
I have a XSD definition which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ARTICLES">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="ARTICLE">
<xs:complexType>
<xs:sequence>
<xs:element name="NAME" type="xs:string" />
<xs:element name="TYPE" type="xs:unsignedByte" />
<xs:element name="LENGTH" type="xs:unsignedByte" />
<xs:element name="WIDTH" type="xs:unsignedShort" />
<xs:element name="HEIGHT" type="xs:unsignedShort" />
<xs:element maxOccurs="unbounded" name="PRICELIST">
<xs:complexType>
<xs:sequence>
<xs:element name="SALESCONTRACT" type="xs:string" />
<xs:element name="PRICE" type="xs:decimal" />
<xs:element name="VAT" type="xs:unsignedByte" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
An XML can contain a collection of ATRICLES and each ARTICLE can have 1 or more PRICELIST entries
Now I would like to take an element and restrict the value entries. i.e. With a regex or numbers only or in length.
With simpletype I found lots of examples that work with a pattern tag.
How would this work with complex types?
Regards,
Rick
You can simply apply all of those examples you've found using xs:simpleType to any of the parts of ARTICLE that you wish to further constrain. For example, to limit NAME to be of maximum length of 32 characters:
<xs:element name="NAME">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="32"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
I am now struggling a bit to generate automatically the XPaths leading to a certain type of my XSD schema.
In this example, I would like to get the XPath leading to "firstName" and "lastName", which means:
root/Friend/firstName, root/Friend/lastName, root/TenthGrader/firstName, root/tenthGrader/lastName, root/complicatedPerson/oldSelf/firstName, root/complicatedPerson/oldSelf/lastName
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="Friend" type="Student"/>
<xs:element name="TenthGrader">
<xs:complexType>
<xs:complexContent>
<xs:extension base="Student">
<xs:element name="Sexyteacher"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element ref="complicatedPerson"/>
<xs:element name="IdoNothing"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="complicatedPerson" type="Person"/>
<xs:complexType name="Person">
<xs:sequence>
<xs:element name="oldSelf" type="Student"/>
<xs:element name="Age" type="xs:int"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Student">
<xs:sequence>
<xs:element name="firstName"/>
<xs:element name="lastName"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
This is already giving me a headache.
Is there any known method to do this?
Thank you very much in advance!
Hello I have a problem with Oracle XMLDB Express Edition 10g.
I am trying to register a fully valid and working .xsd Schema via dbms_xmlschema.registerSchema, but keep getting
ORA-01741: illegal zero-length identifier
I have tried everything: triplechecked my xsd and query code.
There were a couple of topics on Oracle forums but there were no answers.
Please help.
Here is my code:
begin
dbms_xmlschema.registerSchema(
'opta',
'<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/opta/" targetNamespace="http://www.example.org/opta/">
<xs:element name="Document">
<xs:complexType>
<xs:sequence>
<xs:element name="Order" type="tns:Order" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="Order">
<xs:sequence>
<xs:element name="Products" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Product" type="tns:Product" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Date" type="xs:date" minOccurs="1"/>
<xs:element name="Client" type="tns:Client" minOccurs="1"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int"/>
</xs:complexType>
<xs:complexType name="Product">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Price" type="xs:int"/>
<xs:element name="Amount" type="xs:int"/>
<xs:element name="Producer" type="tns:Producer"/>
<xs:element name="Type" type="xs:string"/>
</xs:sequence>
<xs:attribute name="id" type="xs:int"/>
</xs:complexType>
<xs:complexType name="Producer">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Organization" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Client">
<xs:sequence>
<xs:element name="Name" type="xs:string"/>
<xs:element name="Organization" type="xs:string"/>
<xs:element name="Country" type="xs:string"/>
<xs:element name="City" type="xs:string"/>
<xs:element name="Address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
');
end;
I'm trying to make just one adapter for every type of element, so I created a bindings.xml file:
<jxb:bindings node="//xs:attribute[#type='Id']"
so, my intention is to address to every attribute of type "Id".
Problem is that xjc tells me "too many target nodes(3)" ... but it's just what I want!!
Try to add multiple="true" attribute:
<jxb:bindings multiple="true" node="//xs:attribute[#type='Id']"
I end up with a similar problem "too many target nodes(3)" however could not find any answer on any of the sites...Posting the solution which I found after lots of trail and error...Basic idea to solve "too many target nodes(3)" is to give complete XPATH of the node which is multiple in your XSD.
Below is my XSD:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="document">
<xs:complexType>
<xs:sequence>
<xs:element name="asset">
<xs:complexType>
<xs:sequence>
<xs:element name="attribute" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="string" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="value" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="date" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="value" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="array" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="struct" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="field" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="integer" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:byte" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="assetreference" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="type"/>
<xs:attribute type="xs:long" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="integer" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:long" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="file" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="integer" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:short" name="value"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="name" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:long" name="id"/>
<xs:attribute type="xs:string" name="type"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
and below is the JAXB binding file which is working for above XSD:
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.1">
<bindings schemaLocation= "../assetproduct.xsd" version="1.0">
<!-- Customise the package name
<schemaBindings>
<package name="com.example.schema"/>
</schemaBindings> -->
<!-- rename the value element -->
<bindings node="//xs:element[#name='document']">
<bindings node="//xs:element[#name='asset']">
<bindings node="//xs:element[#name='attribute']">
<bindings node="//xs:element[#name='string']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//xs:element[#name='date']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//xs:element[#name='array']">
<bindings node=".//xs:element[#name='struct']">
<bindings node=".//xs:element[#name='field']">
<bindings node=".//xs:element[#name='integer']/xs:complexType">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node=".//xs:element[#name='assetreference']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
</bindings>
</bindings>
</bindings>
<bindings node=".//xs:element[#name='array']/xs:complexType/xs:sequence/xs:element[#name='integer']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
<bindings node="//xs:element[#name='attribute']/xs:complexType/xs:sequence/xs:element[#name='integer']">
<bindings node=".//xs:attribute[#name='value']">
<property name="ValueAttribute"/>
</bindings>
</bindings>
</bindings>
</bindings>
</bindings>
</bindings>
</bindings>
I have the following xml:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Child name="MyType" compareMode="EQ">Child1</Child>
</Root>
Usually in order to verify such an xml one would use the following xml schema:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I want to limit values of a Child element and allow only the following: Child1, Child2 and Child3.
I know that usually restriction may be specified with the following schema:
<xs:restriction base="xs:string">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
Which restriction is correct in the first case?
Does this answer your question?
Having both an attribute and a restriction on an element in xml schema
I was wrong, because I didn't test my answer. I edited it:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="ChildContent">
<xs:restriction base="xs:string">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="Child">
<xs:simpleContent>
<xs:extension base="ChildContent">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child" type="Child" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
or with restriction:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Child">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="compareMode" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Child">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="Child">
<xs:enumeration value="Child1"/>
<xs:enumeration value="Child2"/>
<xs:enumeration value="Child3"/>
</xs:restriction>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
After hours spent with google: You cannot do this without named types.