如何使用重复的子元素验证xml

时间:2010-04-07 07:27:07

标签: xml

我有一个xml文档,其中有三个子元素以任何顺序重复。 我有     xsd:xsd中的sequence元素,因为xml没有得到验证。 我不能使用xsd:all因为元素不止一次出现。

请帮助我。

这是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>

这是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>

1 个答案:

答案 0 :(得分:8)

你需要这样的东西:

<?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>

<xs:choice>可让您选择所选内容中的任何一个元素,并且由于<xs:choice>的属性为maxOccurs=unbounded,因此您可以进行任意数量的重复 - - &GT;您可以按任何顺序选择任何数量的元素。