Search code examples
xmlxsdxsd-validationxercesxsd-1.1

Why is this XSD 1.1 assertion failing?


I am using Xerces-J-bin.2.12.1-xml-schema-1.1.zip package of validating XSD 1.1 resulting into below error.

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.

Is that the case that it does not support all the XPaths?

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 Code:

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

In my Java code I am properly pointing to XSD 1.1 see below Java Code:

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());
    }
}

Solution

  • After removing the remnant ts namespace prefix, the only other issue in your posted XML/XSD pair is that the XSD references a non-existant type, attributeWithDistinct. Remove that @type attribute from xsd:element, and filling out full details yields the following XML which is valid against the following XSD, as expected:

    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>
    

    If you add a second attribute element,

      <attribute name="state" distinct="true"/>
    

    to the example element in the 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>
    

    then you get the expected assertion failure:

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

    as expected.

    I have not confirmed your Java code, but the fact that you're getting a normal assertion validation failure rather than a not-allowed error message indicates that you're getting through to XSD 1.1 validation successfully.


    Update per OP's change to use namespaces

    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>