How to validate an xml with repeating child elements
I have an xml document with three child elements repeating in any order. I had xsd:sequence element in the xsd because of which the xml doesn't get validated. I can not use xsd:all because the elements occur more than once.
Kindly help me on this.
Here is the xml
<Trailer>
<TrailerField name="SegmentLabelOne" length="4" type="String">TSTS</TrailerField>开发者_运维技巧;
<TrailerField name="SegmentLabelTwo" length="2" type="String">00</TrailerField>
<CountItem length="10" type="Numeric">MT</CountItem>
<TrailerField name="SegmentLabelThree" length="2" type="String">01</TrailerField>
<CountItem length="10" type="Numeric">MA</CountItem>
<TrailerField name="SegmentLabelFour" length="2" type="String">02</TrailerField>
<TrailerField name="FilerOne" length="65" type="String"> </TrailerField>
</Trailer>
And here is the xsd
<xsd:complexType name="TrailerSegment">
<xsd:sequence>
<xsd:element name="NameOfElement" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="name" type="xsd:string"></xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="CountItem" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="length" type="xsd:string"></xsd:attribute>
<xsd:attribute name="type" type="xsd:string"></xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="TrailerField" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="name" type="xsd:string"></xsd:attribute>
<xsd:attribute name="length" type="xsd:string"></xsd:attribute>
<xsd:attribute name="type" type="xsd:string"></xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
You need something 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="Trailer">
<xs:complexType>
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="TrailerField">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="name" type="xs:string" use="required" />
<xs:attribute name="length" type="xs:unsignedByte" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="CountItem">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="length" type="xs:unsignedByte" use="required" />
<xs:attribute name="type" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The <xs:choice>
gives you the choice of any one of the elements inside the choice, and since the <xs:choice>
has an attribute of maxOccurs=unbounded
, you can have any number of repetitions therefore --> you get to choose any number of those elements included, in any order.
精彩评论