【发布时间】: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>
我对@987654321@ 和Google Images 使用标准XSD,但由于两者都没有引用另一个,我不知道如何使<image:image> 成为<url> 的有效子级。
如果有帮助,这里是执行验证的代码。
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