为什么这个 XSD 1.1 断言失败了?

Why is this XSD 1.1 assertion failing?

我正在使用 Xerces-J-bin.2.12.1-xml-schema-1.1.zip 验证包 XSD 1.1 导致以下错误。

example.xml is not valid because cvc-assertion: Assertion evaluation ('count(attribute[string(@distinct) = 'true']) = 1') for element 'ts:example' on schema type '#AnonType_example' did not succeed.

是不是所有的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 引用了 non-existant 类型,attributeWithDistinct.从 xsd:element 中删除 @type 属性,并填写完整的详细信息会产生以下 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"/>

到XML中的example元素,

<?xml version="1.0" encoding="UTF-8"?>
<example>
  <attribute name="building" distinct="false"/>
  <attribute name="state" distinct="true"/>
  <attribute name="state" distinct="true"/>
</example>

然后你得到预期的断言失败:

Assertion evaluation ('count(attribute[string(@distinct) = 'true']) = 1') for element 'example' on schema type '#AnonType_example' did not succeed.

符合预期。

我还没有确认你的 Java 代码,但你得到的是正常的断言验证失败而不是 not-allowed 错误消息这一事实表明你正在进入 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>