Not sure why my XML file is coming up as invalid/no grammar found and why my schema is not validating either?

对着背影说爱祢 提交于 2019-12-24 03:37:17

问题


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 - Document is invalid: no grammar found.

Ln 14 Col 7 - Document root element "sites", must match DOCTYPE root "null". 2 Errors

...

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

Here is the code:

<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.


回答1:


You need to perform some fixture both in your schema and in your XML:

First, in the schema:

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" />.

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.

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.

The fully corrected schema will be:

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

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">

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>

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>


来源:https://stackoverflow.com/questions/32859980/not-sure-why-my-xml-file-is-coming-up-as-invalid-no-grammar-found-and-why-my-sch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!