不知道为什么我的 XML 文件出现无效/找不到语法以及为什么我的架构也没有验证? [英] Not sure why my XML file is coming up as invalid/no grammar found and why my schema is not validating either?
问题描述
我对 XML 有点陌生,但我一直无法找出这些错误.这是我第一次尝试创建高级"模式,但我不确定导入以及如何准确引用模式中的urlset"......这些是错误:
I am somewhat new to XML, but I have been unable to figure out these errors. This is my first time attempting to create "advanced" schemas, but I was uncertain about importing and how exactly to reference the "urlset" in the schema... These are the errors:
Ln 14 Col 7 - 文档无效:未找到语法.
Ln 14 Col 7 - Document is invalid: no grammar found.
Ln 14 Col 7 - 文档根元素sites",必须匹配 DOCTYPE 根null".2 错误
Ln 14 Col 7 - Document root element "sites", must match DOCTYPE root "null". 2 Errors
...
Ln 31 Col 98 - s4s-att-invalid-value:元素element"中ref"的无效属性值.记录原因:cvc-datatype-valid.1.2.1: 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd' 不是 'QName' 的有效值.1 错误
Ln 31 Col 98 - s4s-att-invalid-value: Invalid attribute value for 'ref' in element 'element'. Recorded reason: cvc-datatype-valid.1.2.1: 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd' is not a valid value for 'QName'. 1 Error
代码如下:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cc="http://example.com/weekendfunsnacks/sites/ns"
xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9"
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="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<sites xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:crs="http://example.com/weekendfunsnacks/sites/ns">
<site>
<name>Weekend Fun Snacks</name>
<totalPages>127</totalPages>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<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>
任何帮助或启发将不胜感激,谢谢.
Any help or enlightenment would be greatly appreciated, thank you.
推荐答案
您需要在架构和 XML 中执行一些fixture:
You need to perform some fixture both in your schema and in your XML:
您只需要调用"对要导入的站点地图架构中定义的 urlset 元素的引用:<xs:element ref="sm:urlset"/>
<xs:element ref="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"/>
.
You just need to "call" a reference to the urlset element that is defined in the sitemap schema you are importing : <xs:element ref="sm:urlset" />
instead of <xs:element ref="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" />
.
在这里您会注意到的一点是,您从 sitemap.xsd 导入的元素绑定到命名空间http://www.sitemaps.org/schemas/sitemap/0.9/ns
因此您需要添加前缀 sm:
urlset 元素.
A point that you will notice here is that the elements you are importing from sitemap.xsd are bound to the the namespacehttp://www.sitemaps.org/schemas/sitemap/0.9/ns
thus you need to add the prefix sm:
the urlset element.
您还需要为自己的架构定义目标命名空间,方法是将 targetNamespace="http://example.com/weekendfunsnacks/sites/ns"
添加到架构的根元素.
You will also need to define a target namespace to your own schema, by adding targetNamespace="http://example.com/weekendfunsnacks/sites/ns"
to the root element of your schema.
完全更正的架构将是:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cc="http://example.com/weekendfunsnacks/sites/ns"
xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9"
elementFormDefault="qualified" attributeFormDefault="unqualified"
targetNamespace="http://example.com/weekendfunsnacks/sites/ns">
<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="sm:urlset" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
然后是 XML 实例:
既然已经在架构中进行了一些修复,您将需要从 XML 中适当地调用它.主要的错误是您没有在
元素上设置 schemaLocation
.这将更正此问题:
Then the XML instance:
Now that some fixes have been made in the schema, you will need to call it appropriately from the XML. What mainly goes wrong is that you haven't set a schemaLocation
on the <sites>
element. This will correct this issue:
<sites xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/weekendfunsnacks/sites/ns funsnacks.xsd"
xmlns="http://example.com/weekendfunsnacks/sites/ns"
xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9">
同时请注意,我为 sitemap.xsd 架构添加了命名空间声明 (xmlns:sm
).这将引导我们进行下一个修复 - 您必须为 <urlset>
元素使用这个命名空间前缀.下面提供了一个修复示例.您也不再需要这些元素上的 xsi
相关声明:
Notice also at the same time that I have added a namespace declaration (xmlns:sm
) for the sitemap.xsd schema. This leads us to the next fix to be undertaken - you have to use this namespace prefix for the <urlset>
element. An example of the fixup is provided below. You also do not need anymore the xsi
-related declaration on these elements:
<sm:urlset>
<sm:url>
<sm:loc>http://example.com/vegsnacks/?cat=55</sm:loc>
<sm:lastmod>2017-06-12T08:05:32+00:00</sm:lastmod>
</sm:url>
<!-- and so on ... -->
</sm:urlset>
您完全更正的输入示例将变为:
Your fully corrected input example will turn to:
<?xml version="1.0" encoding="UTF-8"?>
<sites xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://example.com/weekendfunsnacks/sites/ns funsnacks.xsd"
xmlns="http://example.com/weekendfunsnacks/sites/ns"
xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9">
<site>
<name>Weekend Fun Snacks</name>
<totalPages>127</totalPages>
<sm:urlset>
<sm:url>
<sm:loc>http://example.com/weekendfunsnacks/?cat=58</sm:loc>
</sm:url>
<sm:url>
<sm:loc>http://example.com/weekendfunsnacks/?cat=2</sm:loc>
<sm:lastmod>2017-12-29T06:03:34+00:00</sm:lastmod>
</sm:url>
<sm:url>
<sm:loc>http://example.com/weekendfunsnacks/?cat=15</sm:loc>
<sm:lastmod>2017-12-29T05:24:04+00:00</sm:lastmod>
</sm:url>
<sm:url>
<sm:loc>http://example.com/weekendfunsnacks/?cat=93</sm:loc>
</sm:url>
<sm:url>
<sm:loc>http://example.com/weekendfunsnacks/?cat=55</sm:loc>
</sm:url>
</sm:urlset>
</site>
<site>
<name>Paleo Snacks</name>
<totalPages>52</totalPages>
<sm:urlset>
<sm:url>
<sm:loc>http://example.com/primalsnacks/?cat=6</sm:loc>
</sm:url>
<sm:url>
<sm:loc>http://example.com/primalsnacks/?cat=18</sm:loc>
<sm:lastmod>2017-09-19T17:13:19+00:00</sm:lastmod>
</sm:url>
<sm:url>
<sm:loc>http://example.com/primalsnacks/?cat=54</sm:loc>
<sm:lastmod>2017-09-19T15:24:01+00:00</sm:lastmod>
</sm:url>
<sm:url>
<sm:loc>http://example.com/primalsnacks/?cat=52</sm:loc>
<sm:lastmod>2017-09-28T21:03:11+00:00</sm:lastmod>
</sm:url>
<sm:url>
<sm:loc>http://example.com/primalsnacks/?cat=201</sm:loc>
<sm:lastmod>2017-10-06T07:03:26+00:00</sm:lastmod>
</sm:url>
<sm:url>
<sm:loc>http://example.com/primalsnacks/?cat=11</sm:loc>
</sm:url>
</sm:urlset>
</site>
<site>
<name>Veg Snacks</name>
<totalPages>17</totalPages>
<sm:urlset>
<sm:url>
<sm:loc>http://example.com/vegsnacks/?cat=102</sm:loc>
</sm:url>
<sm:url>
<sm:loc>http://example.com/vegsnacks/?cat=23</sm:loc>
</sm:url>
<sm:url>
<sm:loc>http://example.com/vegsnacks/?cat=1</sm:loc>
</sm:url>
<sm:url>
<sm:loc>http://example.com/vegsnacks/?cat=55</sm:loc>
<sm:lastmod>2017-06-12T08:05:32+00:00</sm:lastmod>
</sm:url>
<sm:url>
<sm:loc>http://example.com/vegsnacks/?cat=201</sm:loc>
</sm:url>
<sm:url>
<sm:loc>http://example.com/vegsnacks/?cat=87</sm:loc>
</sm:url>
</sm:urlset>
</site>
</sites>
另一种可能性是根据需要更改 <urlset>
上的默认名称空间(尽管在我看来,使用前缀更清晰,以便您可以明确地知道您正在使用的名称空间),像这样:
Another possibility is to change the default namespace on <urlset>
as required (although that in my opinion it is clearer to use prefixes so that you can explicitely know what namespace you are working), like this:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://example.com/weekendfunsnacks/?cat=2</loc>
<lastmod>2017-12-29T06:03:34+00:00</lastmod>
</url>
<!-- and so on -->
</urlset>
这篇关于不知道为什么我的 XML 文件出现无效/找不到语法以及为什么我的架构也没有验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!