Given this XML Schema snippet:
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="param" type="param" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="format" type="format" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
</xs:element>
The intended result is valid <data> elements may contain 0 or more <param> elements followed by 0 or more <format> elements. Have I added the minOccurs/maxOccurs atttributes correctly, or should they be applied to the containing <xs:sequence>?
Correct or not, what would be the result of going one way or the other?
You have done it right and you can not add min/max occurs to sequence element. Using and XML editor that supports XML Schema might help you to validate your assumptions when you are in doubt. Here is a good free ware called XMLFox
Related
I have to create constraints on below xml on basis of its tag value
<struct>
<member>
<name>Identifier</name>
<value><i4>11002</i4></value>
</member>
<member>
<name>StartDate</name>
<value><dateTime.iso8601>20160701T12:00:00+0000</dateTime.iso8601>
</value>
</member>
<member>
<name>Type</name>
<value><i4>0</i4></value>
</member>
</struct>
The xsd format that i have created is like,it is just the short form of code and generated using a tool online.
<xs:element minOccurs="0" name="struct">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="member">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
<xs:element minOccurs="0" name="value">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="string" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
What i need is to put constraint on startDate that it is of correct format.
and identifier is only 5 to 10 digits long.
Your XML design is excessively meta and is thereby frustrating your ability to express constraints on data values using XSD 1.0.
You can do one of the following:
Redesign your XML to use concrete tag names such as Identifier, StartDate etc.
Constrain i4 and dateTime.iso8601 apart from name.
Use XSD 1.1 assertions to enforce checking based upon the value of
name.
Recommend you choose #1.
What I'm trying to do is, declare an parent element called "data", which are having 10 sub element of these one element are conditional.
My XSD is:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="data" >
<xs:complexType>
<xs:sequence>
<xs:element name="sub_data" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:all >
<xs:element ref="A"/>
<xs:element ref="B" minOccurs="0" maxOccurs="1"/>
<xs:element ref="C"/>
<xs:element ref="D"/>
<xs:element ref="E"/>
<xs:element ref="F"/>
<xs:element ref="G"/>
<xs:element ref="H"/>
<xs:element ref="I"/>
<xs:element ref="J"/>
<xs:element ref="K"/>
<xs:element ref="L"/>
<xs:element ref="M"/>
<xs:element ref="N"/>
<xs:element ref="element_group"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="status"/>
</xs:complexType>
</xs:element>
<xs:element name="O" type="xs:string" substitutionGroup="element_group">
<xs:element name="P" type="xs:string" substitutionGroup="element_group">
<xs:element name="Q" type="xs:string" substitutionGroup="element_group">
</xs:schema>
Requirement is:
All element from A to N are appearing in any order.
Element P,Q and R is also part of data but only one element appear from among 3. Order is also any.
More important I have cover 1st and 2nd point but I want one more restriction is that only and only four element will be become the part of <data> that means element count from <A> to <Q> is exact four,
Combination can any of them from <A> to <Q> but final count is only four, please help me.
Now currently i am unable to set maxOccures in <all> , it not compiling the xsd after setting maxOccures.
The simplest way to handle this is probably to use XSD 1.1 and use an assertion on the parent to specify that there must be exactly (or at most) four children. You will also need to make each child of the all-group optional, since thirteen of them will not appear.
The best way might be to redesign your XML to work better with your schema language instead of fighting it. It's hard to give advice on that, though, since your example is abstract enough to make it unclear why you are imposing the requirements you mention.
please assist, this is what i want to achieve in validating my xml file:
<?xml version="1.0" encoding="UTF-8"?>
<worker>
<name>dingo</name>
<ssn>12345</ssn>
</worker>
I want to ensure that the two simple elements 'name' and 'ssn' either have values (as a group) or do not have any value (as a group). They cannot exist individually with a value.
I have to use an XSD schema, so cannot use other options i see suggestions sometimes: Relax NG etc.
I looked into creating a group for elements 'name' and 'ssn' but i am unable to find out how to create a restriction for this group to obtain my condition.
My current XSD file:
<xs:complexType name="worker">
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="0" "maxOccurs="1">
<xs:element name="ssn" type="xs:positiveInteger" minOccurs="0" "maxOccurs="1">
</xs:sequence>
</xs:complexType>
<xs:complexType name="worker">
<xs:sequence minOccurs="0">
<xs:element name="name" type="xs:string">
<xs:element name="ssn" type="xs:positiveInteger">
</xs:sequence>
</xs:complexType>
You have to do
<xs:complexType name="worker">
<xs:group ref="workerGrp" minOccurs="0"/>
</xs:complexType>
<xs:group name="workerGrp">
<xs:sequence>
<xs:element name="name" type="xs:string">
<xs:element name="ssn" type="xs:positiveInteger">
</xs:sequence>
</xs:group>
If you look at the following xsd fragment you can conclude that the corresponding xml will first contain cars followed by busses eg:
car,car,bus,bus
HOWEVER I want the xml to be able to contain
car,bus,car,bus
What change do I need to make in the xsd below in order to achieve this?
<xs:element name="body">
<xs:complexType>
<xs:sequence>
<xs:element name="session" type="tns:session" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="car" type="tns:car" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="bus" type="tns:bus" />
</xs:sequence>
</xs:complexType>
</xs:element>
It's a bit cumbersome, but you might achieve what you're looking for like this:
create a <xs:choice> element with your car and bus elements inside; this defines that one of the contained elements can be used
make sure to have the attribtues minOccurs=1 and maxOccurs=unbounded on that <xs:choice> - this gives you any number of either car or bus elements - any number, any combination
So your XML schema would look something like this (I added some stuff just to be able to generate a sample XML and verify it works - tweak as needed):
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="body">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element minOccurs="0" maxOccurs="unbounded" name="car" type="CarType" />
<xs:element minOccurs="0" maxOccurs="unbounded" name="bus" type="BusType" />
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="CarType">
<xs:sequence>
<xs:element name="Maker" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="BusType">
<xs:sequence>
<xs:element name="Maker" type="xs:string" />
<xs:element name="Capacity" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:schema>
use <xs:any> insted of <xs:sequence>
This is a part of my xml schema
<xs:complexType name="Friend">
<xs:all>
<xs:element name="name" type="xs:string" />
<xs:element name="phone" type="xs:string" />
<xs:element name="address" type="xs:string" />
</xs:all>
</xs:complexType>
<xs:complexType name="Coworker">
<xs:all>
<xs:element name="name" type="xs:string" />
<xs:element name="phone" type="xs:string" />
<xs:element name="office" type="xs:string" />
</xs:all>
</xs:complexType>
For better maintainability, I would like to have the shared attributes in an (abstract) super type or something like that. But more important, I want that all elements are unordered and also optional.
Is this possible, and what is the best way to do it?
You have to limit yourself a little bit, some of the things you are trying to do are not possible in XML Schema.
Suppose you introduce a complex type called Person to be a super-type of Friend and Coworker. Here are your options:
Replace xs:all with xs:sequence, remove name and phone from the sub-types, add to the super-type, and add inheritance. Your elements now have to be ordered, but you can make them individually optional. It is illegal to use xs:all in type hierarchies in XML Schema, because the processor cannot tell where the parent content model stops and the child content model starts.
Replace xs:all with <xs:choice maxOccurs="unbounded"> in both types, and add your inheritance. Then your elements become unordered again, but they may repeat.
So in conclusion: given your type names up there, I would guess that your requirements will not be exactly met. I would go for the first option: insisting on arbitrary element order is often not as useful as it seems.
One-and-half year after this question and the accepted answer were posted, XSD 1.1 was published. In this version it is possible to specify what the OP asked for because a number of restriction on xs:all were lifted. One of them is that it is now possible to extend an xs:all.
Using XSD 1.1 you can specify the following:
<xs:complexType name="Person" abstract="true">
<xs:all>
<xs:element name="name" type="xs:string" minOccurs="0" />
<xs:element name="phone" type="xs:string" minOccurs="0" />
</xs:all>
</xs:complexType>
<xs:complexType name="Friend">
<xs:complexContent>
<xs:extension base="Person">
<xs:all>
<xs:element name="address" type="xs:string" minOccurs="0" />
</xs:all>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Coworker">
<xs:complexContent>
<xs:extension base="Person">
<xs:all>
<xs:element name="office" type="xs:string" minOccurs="0" />
</xs:all>
</xs:extension>
</xs:complexContent>
</xs:complexType>
This defines the following types:
Person: an abstract type with optional unordered name and phone elements;
Friend: extends Person adding an optional address element to the list of unordered elements;
Coworker: extends Coworker adding an optional office element to the list of unordered elements.
Note that this solution does not work for every XML processor: even though 8 years have passed since the publication of XSD 1.1, a lot of processors still only support XSD 1.0.