针对两个用于Google图像站点地图的XSD验证XML

问题描述

|| 我有一个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>
我将标准XSD用于站点地图和Google图片,但由于两者均未相互引用,因此我看不到如何使“ 0”成为“ 1”的有效子代。 如果有帮助,这里是执行验证的代码
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);
我能找到的最接近的SO问题需要预先解析和拆分XML文件,因为要应用的架构根据数据值而有所不同。我的要求要简单得多,希望可以更轻松地解决。 更新:我有一个旧的架构,不允许该元素有任何其他子代。 sitemaps.org已更新其XSD以添加
<xsd:any namespace=\"##other\" minOccurs=\"0\" maxOccurs=\"unbounded\" processContents=\"strict\"/>

解决方法

实际上,站点地图架构允许该位置的任何元素,只要它来自另一个名称空间,并提供周围的架构(因为\“ processContent \”是严格的。但是,您的数据无效,< caption>必须出现在之前。 当我在Java 1.6上对其进行测试时,它会验证确定。,花了我一段时间找出进行模式验证的语法(Google自己的示例实际上并未针对XSD文件进行验证): <code><pre><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\"> </pre></code></div><div class="topcard-tags"><a href="https://m.jb51.cc/tag/xml/" class="tag_link" target="_blank">xml</a><a href="https://m.jb51.cc/tag/xmlp/" class="tag_link" target="_blank">xml</a><a href="https://m.jb51.cc/tag/pxml/" class="tag_link" target="_blank">xml</a><a href="https://m.jb51.cc/tag/pxml-1/" class="tag_link" target="_blank">xml</a><a href="https://m.jb51.cc/tag/pxml-2/" class="tag_link" target="_blank">xml</a><a href="https://m.jb51.cc/tag/pxml/" class="tag_link" target="_blank">xml</a><a href="https://m.jb51.cc/tag/xsd/" class="tag_link" target="_blank">xsd</a><a href="https://m.jb51.cc/tag/pxsd/" class="tag_link" target="_blank">xsd</a><a href="https://m.jb51.cc/tag/xsdp/" class="tag_link" target="_blank">xsd</a><a href="https://m.jb51.cc/tag/pliangge/" class="tag_link" target="_blank">两个</a><a href="https://m.jb51.cc/tag/tuxiang/" class="tag_link" target="_blank">图像</a><a href="https://m.jb51.cc/tag/ditu/" class="tag_link" target="_blank">地图</a><a href="https://m.jb51.cc/tag/ditup/" class="tag_link" target="_blank">地图</a><a href="https://m.jb51.cc/tag/yongyu/" class="tag_link" target="_blank">用于</a><a href="https://m.jb51.cc/tag/pyongyu/" class="tag_link" target="_blank">用于</a><a href="https://m.jb51.cc/tag/zhandian/" class="tag_link" target="_blank">站点</a><a href="https://m.jb51.cc/tag/zhandianp/" class="tag_link" target="_blank">站点</a><a href="https://m.jb51.cc/tag/zhendui/" class="tag_link" target="_blank">针对</a><a href="https://m.jb51.cc/tag/pzhendui/" class="tag_link" target="_blank">针对</a><a href="https://m.jb51.cc/tag/yanzheng/" class="tag_link" target="_blank">验证</a><a href="https://m.jb51.cc/tag/yanzhengp/" class="tag_link" target="_blank">验证</a><a href="https://m.jb51.cc/tag/pyanzheng/" class="tag_link" target="_blank">验证</a></div> </div> </div> </div> <!-- row end--> </div> </div> <footer id="footer"> <div class="container"> <div class="copyright"> Copyright © 2018 编程之家. 当前版本 V7.0.16<br> <span class="ml5">编程之家 版权所有 <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备13020303号-8</a> </div> </div> </footer> <script src="https://m.jb51.cc/js/count.js"></script> </body> </html>