简体   繁体   English

XML的XSD验证失败

[英]XSD verification for XML fails

I have an XML file which has contents as below 我有一个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>

I created an XSD schema for this XML using some tool which generated the below XSD output. 我使用一些工具为该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>

Now when i try to validate this XMl against the XSD schema generated, i get below errors in validation(validated using some online validator). 现在,当我尝试针对生成的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'.

Both XML and XSD are valid formats as validated online. XML和XSD都是在线验证的有效格式。 I am not able to understand the reason behind this error. 我无法理解此错误的原因。 I suspect it is because ofhaving multiple elements sharing same name for which I think there is some constraint in generating the XSD schema. 我怀疑这是因为具有多个共享相同名称的元素的原因,我认为在生成XSD架构时存在一些限制。 Not sure exactly what's wrong. 不确定到底是什么问题。

What am I doing wrong here and what could be solution for this error? 我在这里做错了什么,该错误的解决方案是什么?

<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>

the word require mean that the attribute is needed so the corect writting of your XML is : 单词require表示需要该属性,因此XML的corect编写为:

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

if you want to have you writing you need to remove require of your XSD 如果要编写,则需要删除XSD的require

this is the same for MAINTENANCE 维护是一样的

your XSD verify this text : 您的XSD验证此文本:

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

But if you want to validate you XML with this look it is impossible to put use=require so you will have somethig like : 但是,如果您想通过这种外观来验证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>
...

Moreover if you want to have your form (so <a att1 ="" /> <a att2="" /> ) 此外,如果您想拥有自己的表单(所以<a att1 ="" /> <a att2="" />

you will need to change your atribute like that : 您需要像这样更改属性:

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

but I think that the last possiblility is maybe overkill to the first option 但我认为最后的可能性对第一个选择可能是过大的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM