XSD验证 - 一个或两个

时间:2016-03-29 12:15:45

标签: xml xsd

请在下面找到我正在使用的XSD。我想要至少有一个或两个元素: STYPE1 STYPE2

获得的错误:元素'元素'无效,错位或过于频繁发生。

这是我的xsd:



<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="table">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="rows">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="row" maxOccurs="unbounded" minOccurs="0">
                      <xs:complexType mixed="true">
                        <xs:element name="SNO" type="xs:string"/>		
                          <xs:choice minOccurs="1" maxOccurs="2">
                            <xs:element name="STYPE1" type="Type1"/>						  
                            <xs:element name="STYPE2" type="Type2"/>	
                          <xs:choice>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  
  <!-- Start with either A or B followed by 8 characters -->
  <xs:simpleType name="Type1">
   <xs:restriction base="xs:string">
       <xs:pattern value="(A|B)([a-zA-Z0-9])*"/>
	   <xs:length value="9"/>
   </xs:restriction>
  </xs:simpleType>
  
  <!-- Start with either C or D followed by 6 characters -->
  <xs:simpleType name="Type2">
   <xs:restriction base="xs:string">
       <xs:pattern value="(C|D)([a-zA-Z0-9])*"/>
	   <xs:length value="7"/>
   </xs:restriction>
  </xs:simpleType>
</xs:schema>
&#13;
&#13;
&#13;

这是我的xml文件:

&#13;
&#13;
<root>
  <table>
    <rows>
        <row>
           <SNO>1</SNO>
           <STYPE1>A12345678</STYPE1>
           <STYPE2>C654321</STYPE2>
        </row>  	
		<row>
           <SNO>2</SNO>
           <STYPE1>B12345678</STYPE1>
           <STYPE2>D654321</STYPE2>
        </row>        
    </rows>
  </table>
</root>
&#13;
&#13;
&#13;

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

架构无效,因为SNO元素声明和xs:choice元素周围缺少xs:sequence元素。

<xs:complexType mixed="true">
  <xs:sequence>
    <xs:element name="SNO" type="xs:string"/>       
    <xs:choice minOccurs="1" maxOccurs="2">
      <xs:element name="STYPE1" type="Type1"/>                        
      <xs:element name="STYPE2" type="Type2"/>  
    </xs:choice>
  </xs:sequence>
</xs:complexType>

完整的架构将是:

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema
    attributeFormDefault="unqualified"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="table">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="rows">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="row" maxOccurs="unbounded" minOccurs="0">
                      <xs:complexType mixed="true">
                        <xs:sequence>
                          <xs:element name="SNO" type="xs:string"/>
                          <xs:choice minOccurs="1" maxOccurs="2">
                            <xs:element name="STYPE1" type="Type1"/>
                            <xs:element name="STYPE2" type="Type2"/>
                          </xs:choice>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <!-- Start with either A or B followed by 8 characters -->
  <xs:simpleType name="Type1">
    <xs:restriction base="xs:string">
      <xs:pattern value="(A|B)([a-zA-Z0-9])*"/>
      <xs:length value="9"/>
    </xs:restriction>
  </xs:simpleType>

<!-- Start with either C or D followed by 6 characters -->
  <xs:simpleType name="Type2">
    <xs:restriction base="xs:string">
      <xs:pattern value="(C|D)([a-zA-Z0-9])*"/>
      <xs:length value="7"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>