XML的XSD验证失败

时间:2018-12-18 06:33:13

标签: xml xsd xsd-validation xml-validation

我有一个XML文件,其内容如下

<?xml version="1.0" encoding="UTF-8"?>
<SETTINGS Version="1">
<CLEANING amount="40"/>
<CLEANING name="abcd"/>
<CLEANING initials="ABCD"/>
<MAINTENANCE state="on"/>
<MAINTENANCE temperature="F"/>
</SETTINGS>

我使用某些工具为此XML创建了XSD模式,该工具生成了以下XSD输出。

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="SETTINGS">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="CLEANING" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="amount" type="xs:unsignedByte" use="required" />
                        <xs:attribute name="name" type="xs:string" use="required" />
                        <xs:attribute name="initials" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
                <xs:element name="MAINTENANCE" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="state" type="xs:string" use="required" />
                        <xs:attribute name="temperature" type="xs:string" use="required" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="Version" type="xs:unsignedByte" use="required" />
        </xs:complexType>
    </xs:element>
</xs:schema>

现在,当我尝试根据生成的XSD架构验证此XMl时,我得到了以下验证错误(使用一些在线验证程序进行了验证)。

Not valid.
Error - Line 3, 24: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 24; cvc-complex-type.4: Attribute 'name' must appear on element 'CLEANING'.
Error - Line 3, 24: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 24; cvc-complex-type.4: Attribute 'initials' must appear on element 'CLEANING'.
Error - Line 4, 24: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 24; cvc-complex-type.4: Attribute 'amount' must appear on element 'CLEANING'.
Error - Line 4, 24: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 24; cvc-complex-type.4: Attribute 'initials' must appear on element 'CLEANING'.
Error - Line 5, 28: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 28; cvc-complex-type.4: Attribute 'amount' must appear on element 'CLEANING'.
Error - Line 5, 28: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 28; cvc-complex-type.4: Attribute 'name' must appear on element 'CLEANING'.
Error - Line 6, 26: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 26; cvc-complex-type.4: Attribute 'temperature' must appear on element 'MAINTENANCE'.
Error - Line 7, 31: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 31; cvc-complex-type.4: Attribute 'state' must appear on element 'MAINTENANCE'.

XML和XSD都是在线验证的有效格式。我无法理解此错误的原因。我怀疑这是因为具有多个共享相同名称的元素的原因,我认为在生成XSD架构时存在一些限制。不确定到底是什么问题。

我在这里做错了什么?该错误的解决方案是什么?

1 个答案:

答案 0 :(得分:2)

<xs:complexType>
  <xs:attribute name="amount" type="xs:unsignedByte" use="required" />
  <xs:attribute name="name" type="xs:string" use="required" />
  <xs:attribute name="initials" type="xs:string" use="required" />
</xs:complexType>
  

require一词表示需要该属性,因此XML的核心文字为:

<CLEANING amount="40" name="abcd" initials="ABCD"/>

如果要编写,则需要删除require的XSD

这与MAINTENANCE相同

  

您的XSD验证此文本:

<SETTINGS Version="1">
    <CLEANING amount="40" name="abcd" initials="ABCD"/>
    <MAINTENANCE state="on" temperature="F"/>
</SETTINGS>

但是,如果您希望通过这种外观来验证XML,则不可能放入use=require,因此您将得到如下内容:

 ...
    <xs:complexType>
        <xs:attribute name="amount" type="xs:unsignedByte" />
        <xs:attribute name="name" type="xs:string" />
        <xs:attribute name="initials" type="xs:string" />
    </xs:complexType>
...

此外,如果您想拥有自己的表格(例如<a att1 ="" /> <a att2="" />

  

您需要像这样更改属性:

<SETTINGS Version="1">
    <A amount="40" />
    <B name="abcd" />
    <C initials="ABCD"/>
    <D state="on" />
    <E temperature="F"/>
</SETTINGS>

但是我认为最后一种可能对第一种选择来说是过大的