【发布时间】:2020-01-20 17:46:37
【问题描述】:
我正在为我们的 xml 输入/输出设置架构,并且遇到了一个问题,即 XMLSpy 验证正常,但 Xerces 在其中一个 xs:asserts 上失败。 我正在使用最新的 xerces,xerces-2_12_0-xml-schema-1.1。
我已包含该发行版中的所有 .jar 文件(xercesSamples.jar 除外)
测试代码为:
SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/XML/XMLSchema/v1.1");
factory.setFeature("http://apache.org/xml/features/validation/cta-full-xpath-checking", true);
Schema schema = factory.newSchema(new File("C:/Imports/Test.xsd"));
validator = schema.newValidator();
validator.validate(new StreamSource("C:/Imports/Test.xml"));
我已将 xsd 文件精简为:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:lit="http://www.w3schools.com" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://www.w3schools.com" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:element name="MetrixXML">
<xs:complexType>
<xs:all>
<xs:element ref="lit:Page" minOccurs="1" maxOccurs="unbounded"/>
</xs:all>
<xs:attribute name="SchemaVersion" type="xs:float" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="Page">
<xs:complexType>
<xs:attribute name="ContentPositionRule" type="xs:string"/>
<xs:attribute name="FilePageNum" type="xs:nonNegativeInteger"/>
<xs:assert test="(//@SchemaVersion ge 2.1) or ((//@SchemaVersion lt 2.1) and not (@ContentPositionRule))"/>
</xs:complexType>
</xs:element>
</xs:schema>
xml 是:
<?xml version="1.0" encoding="UTF-8"?>
<MetrixXML xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com Test.xsd" SchemaVersion="2.1" >
<Page FilePageNum="1"/>
<Page ContentPositionRule="CenterEachPage"/>
</MetrixXML>
我得到的错误是:
org.xml.sax.SAXParseException: cvc-assertion: 元素“页面”的断言评估 ('(//@SchemaVersion ge 2.1) 或 ((//@SchemaVersion lt 2.1) 而不是 (@ContentPositionRule))') ' 在架构类型 '#AnonType_Page' 上没有成功。
在 XMLSpy 中,如果我将 SchemaVersion 设置为 2.0,则断言失败。如果我将其设置为 2.1,则断言成功。
我需要设置一些功能标志吗?
更新: 显然 XMLSpy 允许它不应该允许的事情。
因此,所需的测试是 如果(SchemaVersion
【问题讨论】:
-
在 XMLSpy 中,如果我将 SchemaVersion 设置为 2.0,则断言失败。如果我将其设置为 2.1,则断言成功。 请注意,您可能有一些拼写错误:断言需要 XSD 1.1,而不是 XSD 1.0;目前没有 XSD 2.0 或 2.1。有关如何使用 Xerces 针对 XSD 1.1 验证 XML,请参阅重复链接。
-
请注意,“SchemaVersion”属性与 xsd 无关。这是我们 xml 的私有属性。我已经阅读了引用的问题,但它并不相同,因为 xsd 1.1 有问题,而我的处理 xsd 1.1 - 但 xerces 没有处理更复杂的断言语句。模式工厂被告知使用 xsd 1.1,并且确实如此。它理解断言 - 但似乎没有正确评估它。
-
对不起,我看错了你的问题。我已经重新打开了。
标签: xsd xsd-validation xerces xpath-2.0 xsd-1.1