XSD用于重复具有不同子项的元素

时间:2014-07-24 20:00:45

标签: xml xsd repeat xsd-validation

嗨我有一个看起来像的xml,

<ABC>
<DEF Value="123">
<XYZ>
<RootEle Text="Now" Date="SomeDate"/>
</XYZ> 
</DEF>
<DEF AnotherValue="123">
<XYZ>
<AnotherRootEle AnotherText="NowOrNever" AnotherDate="SomeOtherDate"/>
</XYZ> 
</DEF>
</ABC>

我需要为此编写一个xsd。但是我写的xsd并不适用于上面的xml。

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
   <xsd:element name="ABC">
      <xsd:complexType>
         <xsd:sequence minOccurs="1" maxOccurs="1">
            <xsd:element name="DEF">
               <xsd:complexType>
                  <xsd:sequence minOccurs="1" maxOccurs="unbounded">
                     <xsd:element name="XYZ">
                        <xsd:complexType>
                           <xsd:sequence minOccurs="1" maxOccurs="1">
                              <xsd:element name="RootEle">
                                 <xsd:complexType>
                                    <xsd:attribute name="Text" use="optional">
                                       <xsd:annotation>
                                          <xsd:documentation>Text</xsd:documentation>
                                       </xsd:annotation>
                                       <xsd:simpleType>
                                          <xsd:restriction base="xsd:string">
                                             <xsd:maxLength value="40" />
                                          </xsd:restriction>
                                       </xsd:simpleType>
                                    </xsd:attribute>
                                    <xsd:attribute name="Date" use="optional">
                                       <xsd:annotation>
                                          <xsd:documentation>Date</xsd:documentation>
                                       </xsd:annotation>
                                       <xsd:simpleType>
                                          <xsd:restriction base="xsd:string">
                                             <xsd:maxLength value="40" />
                                          </xsd:restriction>
                                       </xsd:simpleType>
                                    </xsd:attribute>
                                 </xsd:complexType>
                              </xsd:element>
                           </xsd:sequence>
                        </xsd:complexType>
                     </xsd:element>
                  </xsd:sequence>
                  <xsd:attribute name="Value" use="optional">
                     <xsd:annotation>
                        <xsd:documentation>Name of the flow</xsd:documentation>
                     </xsd:annotation>
                     <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                           <xsd:maxLength value="40" />
                        </xsd:restriction>
                     </xsd:simpleType>
                  </xsd:attribute>
                  <xsd:attribute name="AnotherValue" use="optional">
                     <xsd:annotation>
                        <xsd:documentation>Name of interface</xsd:documentation>
                     </xsd:annotation>
                     <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                           <xsd:maxLength value="40" />
                        </xsd:restriction>
                     </xsd:simpleType>
                  </xsd:attribute>
               </xsd:complexType>
            </xsd:element>
         </xsd:sequence>
      </xsd:complexType>
   </xsd:element>
</xsd:schema>

上面的xsd适用于下面的xml但不适用于首先发布的xdd,即它不适用于具有不同子元素的重复元素。

<ABC>
    <DEF Value="123">
        <XYZ>
            <RootEle Text="Now" Date="SomeDate"/>
        </XYZ>
    </DEF>
</ABC>

对此有何帮助?

1 个答案:

答案 0 :(得分:1)

sergioFC对他的评论是正确的,你已将事件定义为错误的级别

而不是

        <xsd:element name="DEF">
           <xsd:complexType>
              <xsd:sequence minOccurs="1" maxOccurs="unbounded">

你应该

        <xsd:element minOccurs="1" maxOccurs="unbounded" name="DEF">
           <xsd:complexType>
              <xsd:sequence>

但是,XML中的AnotherRootEle根本没有出现在XSD中。

为此,您需要通过以下选项重新定义XYZ。

          <xsd:element name="XYZ">
            <xsd:complexType>
              <xsd:sequence minOccurs="1" maxOccurs="1">
                <xsd:choice minOccurs="0">
                  <xsd:element name="RootEle">
                    <xsd:complexType>
                      <xsd:attribute name="Text" use="optional">
                        <xsd:annotation>
                          <xsd:documentation>Text</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                          <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="40" />
                          </xsd:restriction>
                        </xsd:simpleType>
                      </xsd:attribute>
                      <xsd:attribute name="Date" use="optional">
                        <xsd:annotation>
                          <xsd:documentation>Date</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                          <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="40" />
                          </xsd:restriction>
                        </xsd:simpleType>
                      </xsd:attribute>
                    </xsd:complexType>
                  </xsd:element>
                  <xsd:element name="AnotherRootEle">
                    <xsd:complexType>
                      <xsd:attribute name="AnotherText" use="optional">
                        <xsd:annotation>
                          <xsd:documentation>Text</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                          <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="40" />
                          </xsd:restriction>
                        </xsd:simpleType>
                      </xsd:attribute>
                      <xsd:attribute name="AnotherDate" use="optional">
                        <xsd:annotation>
                          <xsd:documentation>Date</xsd:documentation>
                        </xsd:annotation>
                        <xsd:simpleType>
                          <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="40" />
                          </xsd:restriction>
                        </xsd:simpleType>
                      </xsd:attribute>
                    </xsd:complexType>
                  </xsd:element>
                </xsd:choice>
              </xsd:sequence>
            </xsd:complexType>
          </xsd:element>