【问题标题】:Validate XML against two XSDs for Google Images sitemap针对 Google 图片站点地图的两个 XSD 验证 XML
【发布时间】:2011-08-08 06:08:40
【问题描述】:

我有一个 XML 文件(使用 Google 的 <image:image> 扩展名的站点地图),我需要针对两个本地 XSD 文件进行验证,但验证失败,因为 <url> 不允许 <image:image> 作为孩子。完整的错误信息是

org.xml.sax.SAXParseException: 
cvc-complex-type.2.4.a: Invalid content was found starting with element 'image:image'.
One of '{"http://www.sitemaps.org/schemas/sitemap/0.9":lastmod, 
         "http://www.sitemaps.org/schemas/sitemap/0.9":changefreq, 
         "http://www.sitemaps.org/schemas/sitemap/0.9":priority}' 
is expected.

这是我要验证的站点地图 XML:

<?xml version="1.0"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
  <url>
    <loc>http://example.com/index.html</loc>
    <image:image>
      <image:loc>http://example.com/images/mysite.jpg</image:loc>
      <image:title>My Site's Logo</image:title>
      <image:caption>Logo for My Site by Andy Warhol (not really)</image:caption>
    </image:image>
  </url>
  ...
</urlset>

我对@9​​87654321@ 和Google Images 使用标准XSD,但由于两者都没有引用另一个,我不知道如何使&lt;image:image&gt; 成为&lt;url&gt; 的有效子级。

如果有帮助,这里是执行验证的代码。

Source document = ...
StreamSource[] source = new StreamSource[] {
        new StreamSource(this.getClass().getResourceAsStream("sitemap.xsd"), "http://www.sitemaps.org/schemas/sitemap/0.9"),
        new StreamSource(this.getClass().getResourceAsStream("sitemap-image.xsd"), "http://www.google.com/schemas/sitemap-image/1.1")
    };
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(source)
             .newValidator().validate(document);

closest SO question I could find 需要预先解析和拆分 XML 文件,因为要应用的架构因数据值而异。我的要求要简单得多,我希望更容易解决。

更新:我的旧架构不允许任何其他子元素使用。 sitemaps.org 已更新其 XSD 以添加

<xsd:any namespace="##other" minOccurs="0" maxOccurs="unbounded" processContents="strict"/>

【问题讨论】:

    标签: xml validation xsd


    【解决方案1】:

    我花了一段时间才弄清楚进行架构验证的语法(Google 自己的示例实际上并未针对 XSD 文件进行验证):

    <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
            http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd"
             xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
             xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    

    【讨论】:

      【解决方案2】:

      实际上,站点地图架构允许该位置的任何元素,只要它来自另一个名称空间并且提供周围的架构(因为“processContent”是严格的。但是,您的 数据无效, 必须出现在 之前。

      当我在 Java 1.6 上测试它时,它验证正常。

      【讨论】:

      • 哇,sitemaps.org 必须更改架构而没有更改修订号——可能是因为站点地图结构没有更改。我检查了我链接的文件与我在一年多的申请中拥有的文件,当然它们有所不同。旧的不允许任何其他元素。 “升级”到最新架构解决了这个问题。谢谢!
      • 欢迎您。这种变化至少是向后兼容的。无论如何,模式是邪恶的——或者,更确切地说,模式的(大部分)使用是;-)
      猜你喜欢
      • 2012-09-20
      • 2021-04-11
      • 2011-04-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2011-07-18
      • 1970-01-01
      • 2011-01-24
      相关资源
      最近更新 更多