xml - 为什么这个 XSD 1.1 断言失败?
问题描述
我正在使用验证 XSD 1.1 的 Xerces-J-bin.2.12.1-xml-schema-1.1.zip 包导致以下错误。
example.xml 无效,因为 cvc-assertion: Assertion evaluation ('count(attribute[string(@distinct) = 'true']) = 1') for element 'ts:example' on schema type '#AnonType_example' 没有成功。
是不是它不支持所有的 XPath?
XML:
<?xml version="1.0" encoding="UTF-8"?>
<ts:example xmlns:ts="http://tokenscript.org/2020/06/tokenscript">
<ts:attribute name="building" distinct="false"/>
<ts:attribute name="state" distinct="true"/>
</ts:example>
XSD 代码:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ts="http://tokenscript.org/2020/06/tokenscript"
targetNamespace="http://tokenscript.org/2020/06/tokenscript"
elementFormDefault="qualified">
<element name="example">
<complexType>
<sequence>
<element name="attribute" minOccurs="0" maxOccurs="unbounded" type="ts:attributeWithDistinct"/>
</sequence>
<assert test="count(attribute[string(@distinct) = 'true']) = 1"/>
</complexType>
</element>
<complexType name="attributeTS">
<sequence>
<element minOccurs="0" name="label" />
<element minOccurs="0" name="origins" />
</sequence>
<attribute name="name" use="required" type="NCName"/>
</complexType>
<complexType name="attributeWithDistinct">
<complexContent>
<extension base="ts:attributeTS">
<attribute name="distinct" type="boolean" default="false"/>
</extension>
</complexContent>
</complexType>
</schema>
在我的 Java 代码中,我正确地指向了 XSD 1.1,请参见下面的 Java 代码:
private static final String W3C_XML_SCHEMA_11_NS_URI = "http://www.w3.org/XML/XMLSchema/v1.1";
private static void validateFile(File xmlFile, File xsdFile) throws SAXException, IOException
{
// 1. Lookup a factory for the W3C XML Schema language
//SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
SchemaFactory factory = SchemaFactory.newInstance(W3C_XML_SCHEMA_11_NS_URI);
// 2. Compile the schema.
File schemaLocation = xsdFile;
Schema schema = factory.newSchema(schemaLocation);
// 3. Get a validator from the schema.
Validator validator = schema.newValidator();
// 4. Parse the document you want to check.
Source source = new StreamSource(xmlFile);
// 5. Check the document
try
{
validator.validate(source);
System.out.println(xmlFile.getName() + " is valid.");
}
catch (SAXException ex)
{
System.out.println(xmlFile.getName() + " is not valid because ");
System.out.println(ex.getMessage());
}
}
解决方案
删除剩余的ts
命名空间前缀后,您发布的 XML/XSD 对中唯一的另一个问题是 XSD 引用了不存在的类型,attributeWithDistinct
. 从 中删除该@type
属性xsd:element
,并填写完整详细信息会生成以下 XML,该 XML 对以下 XSD 有效,如预期的那样:
XML
<?xml version="1.0" encoding="UTF-8"?>
<example>
<attribute name="building" distinct="false"/>
<attribute name="state" distinct="true"/>
</example>
XSD
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<xs:element name="example">
<xs:complexType>
<xs:sequence>
<xs:element name="attribute" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:assert test="count(attribute[string(@distinct) = 'true']) = 1"/>
</xs:complexType>
</xs:element>
</xs:schema>
如果添加第二个attribute
元素,
<attribute name="state" distinct="true"/>
到example
XML 中的元素,
<?xml version="1.0" encoding="UTF-8"?>
<example>
<attribute name="building" distinct="false"/>
<attribute name="state" distinct="true"/>
<attribute name="state" distinct="true"/>
</example>
然后你得到预期的断言失败:
架构类型上
'count(attribute[string(@distinct) = 'true']) = 1'
的元素的断言评估 ( )未成功。'example'
'#AnonType_example'
正如预期的那样。
我还没有确认您的 Java 代码,但是您收到正常的断言验证失败而不是不允许的错误消息这一事实表明您已成功通过 XSD 1.1 验证。
更新每个 OP 的更改以使用命名空间
XML
<?xml version="1.0" encoding="UTF-8"?>
<ts:example xmlns:ts="http://tokenscript.org/2020/06/tokenscript">
<ts:attribute name="building" distinct="false"/>
<ts:attribute name="state" distinct="true"/>
</ts:example>
XSD
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ts="http://tokenscript.org/2020/06/tokenscript"
targetNamespace="http://tokenscript.org/2020/06/tokenscript"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
elementFormDefault="qualified"
vc:minVersion="1.1">
<element name="example">
<complexType>
<sequence>
<element name="attribute" minOccurs="0" maxOccurs="unbounded" type="ts:attributeWithDistinct"/>
</sequence>
<assert test="count(ts:attribute[string(@distinct) = 'true']) = 1"/>
</complexType>
</element>
<complexType name="attributeTS">
<sequence>
<element minOccurs="0" name="label" />
<element minOccurs="0" name="origins" />
</sequence>
<attribute name="name" use="required" type="NCName"/>
</complexType>
<complexType name="attributeWithDistinct">
<complexContent>
<extension base="ts:attributeTS">
<attribute name="distinct" type="boolean" default="false"/>
</extension>
</complexContent>
</complexType>
</schema>
推荐阅读
- java - Guice,从属性中获取值时的类型转换
- npm - 如何通过语义发布取消发布错误的版本并使用正确的版本重新发布?
- javascript - 在 PhaserJS 中以 .png 格式下载像素艺术图
- adsense - Google AdSense 自动广告取决于屏幕宽度
- database - '列'CustID'不允许空值。' 尝试写入访问数据库 VB.net 时出错
- javascript - 如何在 onClick 事件 Nextjs 中暂停/启动 GSAP 动画
- javascript - 在 R Shiny 中,当链中的对象被隐藏时,如何维护反应链?
- android - android 设备上的色调和背景显示不同的颜色
- node.js - MongoDB:如何 1. 锁定集合 2. 读取 3. 基于读取的写入?
- flutter - 我可以在 Flutter Flame 中使用 Widget 作为实际组件吗