简体   繁体   English

XML 架构 - 找不到元素的声明

[英]XML Schema - cannot find the declaration of element

XML File: XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<rootItem xmlns="http://www.w3schools.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="schema.xsd">
    <product category="software" type="individual" currentlyOffered="Y">
        <tid>725</tid>
        <tname>MS Office</tname>
        <reviewDate>2017-12-05</reviewDate>
        <note staffID="ee21kfj">Need to specify Windows version</note>
        <note staffID="ef23mls">Is there a price update?</note>
    </product>
    <product category="hardware" type="individual" currentlyOffered="TBC">
        <tid>511</tid>
        <tname>Mouse</tname>
        <reviewDate>2016-09-26</reviewDate>
        <note staffID="fh26eij">Need to ensure ... minors</note>
        <note staffID="mm25por">Need to check ... this</note>
    </product>
</rootItem>

XML Schema: XML 架构:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <!-- definition of simple elements -->
    <xs:element name="tid" type="xs:positiveInteger"/>
    <xs:element name="tname" type="xs:string"/>
    <xs:element name="reviewDate" type="xs:date"/>
    <xs:element name="note" type="xs:string"/>

    <!-- definition of attributes -->
    <xs:attribute name="category" type="xs:string"/>
    <xs:attribute name="type" type="xs:string"/>
    <xs:attribute name="currentlyOffered" type="xs:string"/>
    <xs:attribute name="staffID">
        <xs:simpleType>
            <!-- Some Rules -->
        </xs:simpleType>
    </xs:attribute>

    <!-- definition of complex elements -->
    <xs:element name="product">
        <xs:complexType>
            <xs:sequence>
                <!-- Some Rules -->
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="rootItem">
        <xs:complexType>
            <!-- Some Rules -->
        </xs:complexType>
    </xs:element>

</xs:schema>

And the response I get is:我得到的回应是:

"Cannot find the declaration of element 'rootItem'.

Any ideas?有任何想法吗?

your xml instance uses the default namespace xmlns="http://www.w3schools.com" which is not defined in your schema.您的 xml 实例使用您的架构中未定义的默认命名空间xmlns="http://www.w3schools.com" I don't know which part of your data is changeable, but you either have to remove the namespace in the XML or add it as <xs:element name="rootItem" xmlns="http://www.w3schools.com"> in your schema.我不知道您的数据的哪一部分是可更改的,但是您必须删除 XML 中的命名空间或将其添加为<xs:element name="rootItem" xmlns="http://www.w3schools.com">在您的架构中。

(Have a look at the comment of Michael Kay where the right action to correct the schema file is mentioned) (查看 Michael Kay 的评论,其中提到了纠正架构文件的正确操作)

将目标命名空间添加到架构:

<xs:schema targetNamespace="http://www.w3schools.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">

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

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