简体   繁体   English

XML无法找到元素的声明

[英]XML cannot find the declaration of element

Hello I'm still kinda new to XML, and while I did look around for this problem the answers I found didn't quite fit with my issue. 您好我仍然是XML的新手,虽然我确实四处寻找这个问题,但我找到的答案并不适合我的问题。 When I validate this xml it comes up with the error "Cannot find the declaration of element 'sites' ". 当我验证这个xml时,它会出现错误“无法找到元素'站点'的声明”。 I can't seem to figure out what is going on exactly and I was curious if anyone could help. 我似乎无法弄清楚到底发生了什么,如果有人能提供帮助,我很好奇。

XML below 下面的XML

  <sites xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:xsi="http://example.com/weekendfunsnacks/site"
      xsi:schemaLocation="http://example.com/weekendfunsnacks/site  sites.xsd">

    <xs:import namespace="http://sitemaps.org/schemas/sitemap/0.9"
         schemaLocation="http://sitemaps.org/schemas/sitemap/0.9sitemap.xsd" />

XSD XSD

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cc="http://example.com/weekendfunsnacks/sites"
xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9"
targetNamespace="http://example.com/weekendfunsnacks/sites"
elementFormDefault="qualified" attributeFormDefault="unqualified">

<xs:element name="sites">
  <xs:complexType>
     <xs:sequence>
        <xs:element name="site" maxOccurs="unbounded" minOccurs="0">
           <xs:complexType>
              <xs:sequence>
                 <xs:element type="xs:string" name="name"/>
                 <xs:element type="xs:byte" name="totalPages"/>
                </xs:sequence>
              </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

Thanks for any help! 谢谢你的帮助!

You have a couple of problems: 你有几个问题:

  • sites is not recognised because the schema declares that elementFormDefault="qualified" - this means that elements in the schema are in the schema's target namespace. 无法识别sites因为架构声明了elementFormDefault="qualified" - 这意味着架构中的元素位于架构的目标命名空间中。 <sites> declares an element in the global namespace - <sites> and <prefix:sites> are different elements . <sites>在全局命名空间中声明一个元素 - <sites><prefix:sites>不同的元素 Create a namespace prefix and use it to qualify the <sites> element in your instance document to fix this error. 创建名称空间前缀并使用它来限定实例文档中的<sites>元素以修复此错误。
  • <sites> is not closed - this is not well-formed XML, assuming this is your whole document. <sites>未关闭 - 这不是格式良好的XML,假设这是您的整个文档。

In answer to your followup comment: <xs:sites> is not correct, because the prefix xs is declared as a prefix for http://www.w3.org/2001/XMLSchema-instance . 在回答您的后续评论时: <xs:sites>不正确,因为前缀xs被声明为http://www.w3.org/2001/XMLSchema-instance的前缀。 <sites> is not in this namespace - it's in http://example.com/weekendfunsnacks/sites . <sites>不在此命名空间中 - 它位于http://example.com/weekendfunsnacks/sites So to add a new namespace prefix, add an attribute xmlns:prefix="http://example.com/weekendfunsnacks/sites" to the sites element, then use that prefix on the sites element. 因此,要添加新的名称空间前缀,请在sites元素中添加属性xmlns:prefix="http://example.com/weekendfunsnacks/sites" ,然后在sites元素上使用前缀。

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

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