【问题标题】:How to solve "Invalid content was found" error?如何解决“发现无效内容”错误?
【发布时间】:2017-03-20 00:39:18
【问题描述】:

我有以下 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>

我已尝试针对 XSD here 验证 XML,并收到此错误:

无效。错误 - 第 5、17 行:org.xml.sax.SAXParseException; 行号:5;列号:17; cvc-complex-type.2.4.a:无效 发现内容以元素“B”开头。 '{B, D}' 之一是 预计。

它有什么问题?一切似乎都是正确的。

【问题讨论】:

    标签: xml xsd xsd-validation xml-validation


    【解决方案1】:

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

    1. maxOccurs="unbounded" 添加到BD 的声明中 因为您的 XML 中有多个 BD 元素,以及 default for @maxOccurs is 1
    2. xs:all 更改为xs:sequence,因为xs:all 孩子不能 在 XSD 1.0 中是无限的(并且因为您的 XML 中有一个序列 无论如何)。
    3. 添加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>
    

    【讨论】:

    • 哦!非常感谢!你帮了我很多!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2019-06-28
    • 1970-01-01
    • 2015-03-15
    • 2019-12-04
    相关资源
    最近更新 更多