Xml 如何解决;发现无效内容";错误?

Xml 如何解决;发现无效内容";错误?,xml,xsd,xsd-validation,xml-validation,Xml,Xsd,Xsd Validation,Xml Validation,我有以下XSD: <?xml version="1.0"?> <xs:schema xmlns:item="http://www.example.org/ItemSchema" targetNamespace="http://www.example.org/ItemSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="I

我有以下XSD:

<?xml version="1.0"?>
<xs:schema xmlns:item="http://www.example.org/ItemSchema"
           targetNamespace="http://www.example.org/ItemSchema"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:simpleType name="Item">
        <xs:restriction base="xs:string">
            <xs:pattern value="[a-fA-F]"/>
            <xs:length value="1"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="ItemWithAttr">
        <xs:simpleContent>
            <xs:extension base="item:Item">
                <xs:attribute name="C" use="required">
                    <xs:simpleType>
                        <xs:restriction base="xs:positiveInteger">
                            <xs:pattern value="[0-9]{4}"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:attribute>
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <xs:element name="A">
        <xs:complexType>
            <xs:all>
                <xs:element name="B" type="item:ItemWithAttr" minOccurs="0"/>
                <xs:element name="D" type="item:ItemWithAttr" minOccurs="0"/>
            </xs:all>
        </xs:complexType>
    </xs:element>

</xs:schema>

以及以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<A xmlns="http://www.example.org/ItemSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.example.org/ItemSchema abcd.xsd">
    <B C="0231">a</B>
    <B C="3124">a</B>
    <B C="4114">b</B>
    <B C="0312">b</B>
    <B C="1543">d</B>
    <B C="2345">b</B>
    <D C="1111">d</D>
    <D C="4321">b</D>
</A>

,并已获得此错误:

无效。错误-第5行,第17行:org.xml.sax.SAXParseException; 行号:5;栏目号:17;cvc复杂类型.2.4.a:无效 找到以元素“B”开头的内容。{B,D}中的一个是 预料之中


怎么了?一切似乎都是正确的

对您的XSD进行以下更正(#3是最难发现的):

  • maxOccurs=“unbounded”
    添加到
    B
    D
    因为在XML中有多个
    B
    D
    元素,而
  • xs:all
    更改为
    xs:sequence
    ,因为
    xs:all
    子项不能为 在XSD 1.0中是无界的(因为XML中有一个序列) 无论如何)
  • 添加
    elementFormDefault=“qualified”
    ,因为默认情况下元素 在XML文档中,带有本地声明的数据是不合格的
  • 总之,此XSD将验证您的XML:

    <?xml version="1.0"?>
    <xs:schema xmlns:item="http://www.example.org/ItemSchema"
               targetNamespace="http://www.example.org/ItemSchema"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               elementFormDefault="qualified">    
      <xs:simpleType name="Item">
        <xs:restriction base="xs:string">
          <xs:pattern value="[a-fA-F]"/>
          <xs:length value="1"/>
        </xs:restriction>
      </xs:simpleType>
      <xs:complexType name="ItemWithAttr">
        <xs:simpleContent>
          <xs:extension base="item:Item">
            <xs:attribute name="C" use="required">
              <xs:simpleType>
                <xs:restriction base="xs:positiveInteger">
                  <xs:pattern value="[0-9]{4}"/>
                </xs:restriction>
              </xs:simpleType>
            </xs:attribute>
          </xs:extension>
        </xs:simpleContent>
      </xs:complexType>    
      <xs:element name="A">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="B" type="item:ItemWithAttr" 
                        minOccurs="0" maxOccurs="unbounded"/>
            <xs:element name="D" type="item:ItemWithAttr" 
                        minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    哦!非常感谢你!你帮了我很多忙!