问题
I have been working on this "advanced" schema for days with the flu and I cannot figure out why it keeps telling me that "sites" is not found. I re-read the chapter and even created a copy to experiment with (which worked before) and I do not understand. I am trying to import and combine schemas and I am not sure how to get it to work properly... Here are the errors:
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
Here is the schema:
<?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>
And here is the 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>
回答1:
Your XSD has a targetNamespace
, so your ref="sites"
has to reference that namespace.
Define a namespace prefix, say w:
, to be the same as the target namespace, and then use it in the reference: 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>
来源:https://stackoverflow.com/questions/32900422/error-it-was-detected-that-sites-has-no-namespace-but-components-with-no-tar