Error: It was detected that 'sites' has no namespace, but components with no target namespace are not referenceable from schema document

Error: It was detected that 'sites' has no namespace, but components with no target namespace are not referenceable from schema document

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

Ln 16 Col 84 - cvc-elt.1: Cannot find the declaration of element 'sites'. 1 Errors [Xerces-J 2.9.1] Validating XML Schema "sites.xsd" ... Ln 32 Col 49 - src-resolve.4.1: Error resolving component 'sites'. It was detected that 'sites' has no namespace, but components with no target namespace are not referenceable from schema document.

If 'sites' is intended to have a namespace, perhaps a prefix needs to be provided. If it is intended that 'sites' has no namespace, then an 'import' without a "namespace" attribute should be added

架构如下:

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

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

在您的 <xs:schema 中添加 xmlns,其值与 targetNamespace 相同,例如:

xmlns="http://example.com/weekendfunsnacks/sites"

所以结果会是这样的:

<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="http://example.com/weekendfunsnacks/sites" 
  ....