如何解决 "Invalid content was found" 错误?
How to solve "Invalid content was found" error?
我有以下 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,但遇到此错误:
Not valid. Error - Line 5, 17: org.xml.sax.SAXParseException;
lineNumber: 5; columnNumber: 17; cvc-complex-type.2.4.a: Invalid
content was found starting with element 'B'. One of '{B, D}' is
expected.
怎么了?一切似乎都是正确的。
对您的 XSD 进行以下更正(#3 是最难发现的 ):
- 将
maxOccurs="unbounded"
添加到 B
和 D
的声明中
因为在 XML 和 default for @maxOccurs
is 1
. 中有多个 B
和 D
元素
- 把
xs:all
改成xs:sequence
因为xs:all
children不能
在 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>
我有以下 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,但遇到此错误:
Not valid. Error - Line 5, 17: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 17; cvc-complex-type.2.4.a: Invalid content was found starting with element 'B'. One of '{B, D}' is expected.
怎么了?一切似乎都是正确的。
对您的 XSD 进行以下更正(#3 是最难发现的 ):
- 将
maxOccurs="unbounded"
添加到B
和D
的声明中 因为在 XML 和 default for@maxOccurs
is1
. 中有多个 - 把
xs:all
改成xs:sequence
因为xs:all
children不能 在 XSD 1.0 中不受限制(并且因为您在 XML 中有一个序列 无论如何)。 - 添加
elementFormDefault="qualified"
因为默认元素 在 XML 文档. 中,具有局部声明的是不合格的
B
和 D
元素
总而言之,这个 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>