【问题标题】:Error: It was detected that 'sites' has no namespace, but components with no target namespace are not referenceable from schema document错误:检测到“站点”没有命名空间,但没有目标命名空间的组件无法从架构文档中引用
【发布时间】:2021-12-10 09:43:50
【问题描述】:

几天来,我一直在研究这种“高级”模式,但我不明白为什么它一直告诉我找不到“站点”。我重新阅读了这一章,甚至创建了一个副本来试验(以前有效),但我不明白。我正在尝试导入和组合模式,但我不确定如何让它正常工作......以下是错误:

Ln 16 Col 84 - cvc-elt.1:找不到元素的声明 '网站'。 1 错误 [Xerces-J 2.9.1] 验证 XML 模式“sites.xsd” ... Ln 32 Col 49 - src-resolve.4.1:解析组件“站点”时出错。 检测到“站点”没有命名空间,但组件没有 目标命名空间不可从架构文档中引用。

如果 'sites' 打算有一个命名空间,也许前缀需要 提供。如果打算让“站点”没有命名空间,那么 应添加不带“命名空间”属性的“导入”

这是架构:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

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

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

   <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:element ref="sites"  />
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>​

这里是 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<sites xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xs="http://example.com/weekendfunsnacks/sites/ns"
       xsi:schemaLocation="http://example.com/weekendfunsnacks/sites/ns sites.xsd">
    <site>
        <name>Weekend Fun Snacks</name>
        <totalPages>127</totalPages>
    <urlset xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9/ns">
         <url>
            <loc>http://example.com/weekendfunsnacks/?cat=58</loc>
         </url>
         <url>
            <loc>http://example.com/weekendfunsnacks/?cat=2</loc>
            <lastmod>2017-12-29T06:03:34+00:00</lastmod>
         </url>
         <url>
            <loc>http://example.com/weekendfunsnacks/?cat=15</loc>
            <lastmod>2017-12-29T05:24:04+00:00</lastmod>
         </url>
         <url>
            <loc>http://example.com/weekendfunsnacks/?cat=93</loc>
         </url>
         <url>
            <loc>http://example.com/weekendfunsnacks/?cat=55</loc>
         </url>
      </urlset>
    </site>
    <site>
        <name>Paleo Snacks</name>
        <totalPages>52</totalPages>
    <urlset xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9/ns">
         <url>
            <loc>http://example.com/primalsnacks/?cat=6</loc>
         </url>
         <url>
            <loc>http://example.com/primalsnacks/?cat=18</loc>
            <lastmod>2017-09-19T17:13:19+00:00</lastmod>
         </url>
         <url>
            <loc>http://example.com/primalsnacks/?cat=54</loc>
            <lastmod>2017-09-19T15:24:01+00:00</lastmod>
         </url>
         <url>
            <loc>http://example.com/primalsnacks/?cat=52</loc>
            <lastmod>2017-09-28T21:03:11+00:00</lastmod>
         </url>
         <url>
            <loc>http://example.com/primalsnacks/?cat=201</loc>
            <lastmod>2017-10-06T07:03:26+00:00</lastmod>
         </url>
         <url>
            <loc>http://example.com/primalsnacks/?cat=11</loc>
         </url>
      </urlset>
    </site>
    <site>
        <name>Veg Snacks</name>
        <totalPages>17</totalPages>
     <urlset xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9/ns">
         <url>
            <loc>http://example.com/vegsnacks/?cat=102</loc>
         </url>
         <url>
            <loc>http://example.com/vegsnacks/?cat=23</loc>
         </url>
         <url>
            <loc>http://example.com/vegsnacks/?cat=1</loc>
         </url>
         <url>
            <loc>http://example.com/vegsnacks/?cat=55</loc>
            <lastmod>2017-06-12T08:05:32+00:00</lastmod>
         </url>
         <url>
            <loc>http://example.com/vegsnacks/?cat=201</loc>
         </url>
         <url>
            <loc>http://example.com/vegsnacks/?cat=87</loc>
         </url>
      </urlset>
    </site>
</sites>​

【问题讨论】:

    标签: xml xsd xml-namespaces xsd-validation xml-validation


    【解决方案1】:

    您的 XSD 有一个 targetNamespace,因此您的 ref="sites" 必须引用该命名空间。

    定义一个命名空间前缀,比如w:,与目标命名空间相同,然后在引用中使用它:ref="w:sites"

    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                 targetNamespace="http://example.com/weekendfunsnacks/sites"
                 xmlns:w="http://example.com/weekendfunsnacks/sites"
                 elementFormDefault="qualified" attributeFormDefault="unqualified">
        
      <xs:import namespace="http://www.sitemaps.org/schemas/sitemap/0.9"
                 schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" />
      
      <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:element ref="w:sites"  />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
      
      
    

    【讨论】:

    • 非常感谢。但现在它说“[Xerces-J 2.9.1] 检查“sites.xml”的格式是否正确...... Ln 16 Col 84 - 与元素类型关联的属性“xsi:schemaLocation”的前缀“xsi” “站点”未绑定。1 错误“因此 XML 仍未验证。
    • 上述 XSD 本身格式正确且正确。您的新错误来自对 XML 文件的解析,表明您必须将此声明添加到“sites.xml”的根元素中:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    • 非常感谢您的帮助。我想我明白了很多。但是,XML 文件仍未验证: w3.org/2001/XMLSchema-instance" xmlns="example.com/weekendfunsnacks/sites/ns" xsi:schemaLocation="example.com/weekendfunsnacks/sites/nssites.xsd"> .... 它说:“[ Xerces-J 2.9.1] 检查“sites.xml”的格式是否正确... Ln 93 Col 9 - 结尾部分中不允许有内容。1 错误“
    • 同样,上面的 XSD 本身格式正确且正确。您的新错误表明您在应用此答案时引入了一些语法错误。 ...我看到您现在已经接受了答案,因此大概发现了语法错误。很好。
    猜你喜欢
    • 2015-12-30
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2010-10-19
    • 2011-08-23
    相关资源
    最近更新 更多