首页 > 解决方案 > 为什么这个 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());
    }
}

标签: xmlxsdxsd-validationxercesxsd-1.1

解决方案


删除剩余的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"/>

exampleXML 中的元素,

<?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>

推荐阅读