<xs:complexType>
<xs:all>
<xs:element name="AN" minOccurs="0"/>
<xs:element name="ME" minOccurs="0"/>
<xs:element name="preview" minOccurs="0"/>
<xs:element name="NZ" minOccurs="0"/>
<xs:element name="RE" minOccurs="0"/>
<xs:element name="RU" minOccurs="0"/>
<xs:element name="AU" minOccurs="0"/>
<xs:element name="SE" minOccurs="0"/>
<xs:element name="CM" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
In the above code, i want the element "ME" to be used twice in XML and other elements should be used exactly once but in anyorder.
What you want to do is the following. However while this is fine under XSD 1.1, its not under XSD 1.0.
<xs:element name="MyElm">
<xs:complexType>
<xs:all>
<xs:element name="AN" />
<xs:element name="ME" minOccurs="2" maxOccurs="2" />
<xs:element name="preview" />
<xs:element name="NZ" />
<xs:element name="RE" />
<xs:element name="RU" />
<xs:element name="AU" />
<xs:element name="SE" />
<xs:element name="CM" />
</xs:all>
</xs:complexType>
</xs:element>
In XSD 1.0, I'm pretty sure this is not possible. If you can live without them being 'in any order' then you can use a xs:sequence, and if you can live with 0-1 and 0-2 items then you could use a xs:choice. But that's as close as you can get.
Related
I want to do the validations like:
<A>
<B> or <C>
<D>
</A>
In the , the first element should be one of the B and C. The second element is D. And at the same time, the first element B or C and the second element do not in sequence. It could become and . Anybody know how to do it?
To match your example, please try:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="A">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="B" type="xs:string"/>
<xs:element name="C" type="xs:string"/>
</xs:choice>
<xs:element name="D" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
You have to use two things here, the first one is <xs:choice> which allows only one elements to be present in xml.
In order to make an element optional (D) you have to specify minOccurs="0".
Edited (after feedback): All valid cases are covered with this XSD.
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="B" type="xs:string"/>
<xs:element name="C" type="xs:string"/>
<xs:element name="D" type="xs:string"/>
<xs:complexType name="OurType">
<xs:choice>
<xs:sequence>
<xs:element ref="D"/>
<xs:choice>
<xs:element ref="B"/>
<xs:element ref="C"/>
</xs:choice>
</xs:sequence>
<xs:sequence>
<xs:choice>
<xs:element ref="B"/>
<xs:element ref="C"/>
</xs:choice>
<xs:element ref="D"/>
</xs:sequence>
</xs:choice>
</xs:complexType>
<xs:element name="A" type="OurType"/>
</xs:schema>
I get the best practice. It should be as below:
<xs:element name="A">
<xs:complexType>
<xs:all>
<xs:element ref="Choice" minOccurs="1" maxOccurs="1"/>
<xs:element ref="D" minOccurs="0" maxOccurs="1"/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name="D" type="xs:string"/>
<xs:element name="Choice" abstract="true"/>
<xs:element name="B" substitutionGroup="Choice">
</xs:element>
<xs:element name="C" substitutionGroup="Choice">
</xs:element>
I have been trying to form this XSD, can someone help please...
I have an element 'country' as below:
<xs:element name="country">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:long" minOccurs="0" />
<xs:element name="isoCode" type="xs:string" minOccurs="0" />
<xs:element name="currencyCode" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
Now, I need to form XSD with two elements, 'source country' and 'destination country' which should both reference to 'country'. Can someone please help me to form that XSD.
<xs:element name="crossCountries">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:long" minOccurs="0" />
<xs:element ref="country" /> <!-- Source Country -->
<xs:element ref="country" /> <!-- Destination Country -->
</xs:sequence>
</xs:complexType>
</xs:element>
You cannot reference an element and assign a different tag name to that reference. What you want to do instead is to define the content model for that element (a complex type would do) and reuse that under differently named tags.
<xs:complexType name="country">
<xs:sequence>
<xs:element name="id" type="xs:long" minOccurs="0" />
<xs:element name="isoCode" type="xs:string" minOccurs="0" />
<xs:element name="currencyCode" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
Then:
<xs:element name="crossCountries">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:long" minOccurs="0" />
<xs:element name="sourceCountry" type="country" /> <!-- Source Country -->
<xs:element name="destinationCountry" type="country" /> <!-- Destination Country -->
</xs:sequence>
</xs:complexType>
</xs:element>
Below is the sample xml which has multiple <rulex> that starts with sequence 1 and it can end up to many rule like <rule1> , <rule2>, <rule3> etc....
<?xml version="1.0" encoding="UTF-8"?>
<AddressChange_181>
<rules>
<rule1>
<conditions>xya</conditions>
<response_path>abc</response_path>
</rule1>
<rule2>
<conditions>xxxx</conditions>
<response_path>aaaa</response_path>
</rule2>
<rule3>
<conditions>yyyyy</conditions>
<response_path>ffff</response_path>
</rule3>
<rule4>
<conditions>zzzz</conditions>
<response_path>yyyy</response_path>
</rule4>
<default>
<response_path>uuuuu</response_path>
</default>
</rules>
</AddressChange_181>
Below is the schema where i tried creating dynamic <rulex> element name for the above xml.
when i generate xml from this schema i do not get the same xml format as above xml.
can you please let me know how to create schema with multiple element name that starts with a sequence number.
My requirement is to add more than one rule (<rule1>,<rule2>,<rule3> etc...) in xml file and this xml file should be validated against schema.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mock_rule_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rules">
<xs:complexType>
<xs:sequence>
<xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="default" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rule">
<xs:complexType>
<xs:simpleContent>
<xs:restriction base="xs:anyType">
<xs:pattern value="rule/d{1,3}"></xs:pattern>
</xs:restriction>
</xs:simpleContent>
<xs:sequence>
<xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
<xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="default">
<xs:complexType>
<xs:sequence>
<xs:element ref="response_path"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conditions" type="xs:string">
</xs:element>
<xs:element name="response_path" type="xs:string"/>
</xs:schema>
Thanks,
Madhu
There is no way to define such a structure using XSD if the number of ruleX tags is arbitrarily defined. If you can constrain the upper bound to a maximum, and you really have to stick with ruleX naming convention, than you can define a complex type such as ruleType, then a bunch of rule1, rule2,... , ruleN elements of that type - I would call this messy... I wouldn't recommend this.
XSD (with a maximum of 6):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mock_rule_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rules">
<xs:complexType>
<xs:sequence>
<xs:element ref="rule1" minOccurs="0"/>
<xs:element ref="rule2" minOccurs="0"/>
<xs:element ref="rule3" minOccurs="0"/>
<xs:element ref="rule4" minOccurs="0"/>
<xs:element ref="rule5" minOccurs="0"/>
<xs:element ref="rule6" minOccurs="0"/>
<xs:element ref="default"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="ruleType">
<xs:sequence>
<xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
<xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:element name="default">
<xs:complexType>
<xs:sequence>
<xs:element ref="response_path"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conditions" type="xs:string">
</xs:element>
<xs:element name="response_path" type="xs:string"/>
<xs:element name="rule1" type="ruleType"/>
<xs:element name="rule2" type="ruleType"/>
<xs:element name="rule3" type="ruleType"/>
<xs:element name="rule4" type="ruleType"/>
<xs:element name="rule5" type="ruleType"/>
<xs:element name="rule6" type="ruleType"/>
</xs:schema>
Alternatively, you could have a tag named "rule" with a #sequence attribute.
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mock_rule_list">
<xs:complexType>
<xs:sequence>
<xs:element ref="rules" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="rules">
<xs:complexType>
<xs:sequence>
<xs:element ref="rule" minOccurs="0" maxOccurs="unbounded"/>
<xs:element ref="default"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="SequenceKey">
<xs:selector xpath="rule"/>
<xs:field xpath="#sequence"/>
</xs:unique>
</xs:element>
<xs:complexType name="ruleType">
<xs:sequence>
<xs:element ref="conditions" minOccurs="1" maxOccurs="1"/>
<xs:element ref="response_path" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="sequence" type="xs:int" use="required"/>
</xs:complexType>
<xs:element name="default">
<xs:complexType>
<xs:sequence>
<xs:element ref="response_path"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="conditions" type="xs:string">
</xs:element>
<xs:element name="response_path" type="xs:string"/>
<xs:element name="rule" type="ruleType"/>
</xs:schema>
Sample XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<rules xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<rule sequence="1">
<conditions>conditions1</conditions>
<response_path>response_path1</response_path>
</rule>
<rule sequence="2">
<conditions>conditions2</conditions>
<response_path>response_path2</response_path>
</rule>
<default>
<response_path>response_path1</response_path>
</default>
</rules>
Or it could also be that one could simply use the index of the <rule/> within the parent collection .
I would like to validate this XML:
<meta>
<house>
<big ... />
<little ... />
<big ... />
</house>
<flat>
<red ... />
<red ... />
<yellow ... />
</flat>
</meta>
I wrote that.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="meta">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="house">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name='big' />
<xs:element name='little' />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="flat">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name='red'/>
<xs:element name='yellow'/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
But that does not validate my example.
Without 'house' or 'flat', and only meta, that worked.
Where could be my problem ?
Found !
The solution: add a "xs:choice" for each "xs:sequence", like this:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="meta">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name="house">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name='big' />
<xs:element name='little' />
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="flat">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element name='red'/>
<xs:element name='yellow'/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I want to specify that either fieldname or freetext must always be present in XML files that apply to this XSD. Is there a way to do that?
<xs:complexType name="tSome">
<xs:sequence>
<!-- either one of the two below has to be present. -->
<xs:element name="fieldname" type="xs:string" />
<xs:element name="freetext" type="xs:string" />
<!-- this one below must always be present -->
<xs:element name="dbtablename" type="xs:string" />
</xs:sequence>
</xs:complexType>
There is a Choice Indicator in XML Schema, which allows you to take one of the contained elements, but not two or more. If you want any 2 of 3, I suggest doing something like this:
<xs:choice>
<xs:element name="fieldname" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="freetext" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="dbtablename" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:choice>
<xs:choice>
<xs:element name="fieldname" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="freetext" type="xs:string" minOccurs="0" maxOccurs="1" />
<xs:element name="dbtablename" type="xs:string" minOccurs="0" maxOccurs="1" />
</xs:choice>
(Maybe maxOccurs will prevent you from choosing one and the same element twice.)
If that does not work, nothing will I think.
Edited: I didn't correctly understand the question the first time. If you want dbtablename to always be present with any one of fieldname or freetext, then this is the answer:
<xs:complexType name="tSome">
<xs:sequence>
<xs:choice>
<xs:element name="fieldname" type="xs:string" />
<xs:element name="freetext" type="xs:string" />
</xs:choice>
<xs:element name="dbtablename" type="xs:string" />
</xs:sequence>
</xs:complexType>
So, you want either fieldname or freetext and not both? or maybe both? and then dbtablename optionally?
Here is 1 or 2 of the elements:
<xs:choice minOccurs="1" maxOccurs="2">
<xs:element name="fieldname" type="xs:string"/>
<xs:element name="freetext" type="xs:string"/>
<xs:element name="dbtablename" type="xs:string"/>
</xs:choice>
Is this what you want? or did you want dbtablename to be separate?