[Interest] QXmlSchemaValidator fails when testing for unique attributes

Murphy, Sean smurphy at walbro.com
Wed Apr 6 05:15:14 CEST 2016


I'm having trouble using QXmlSchemaValidator trying to validate an XML file that uses the <unique> element when the field is an attribute. I've been able to replicate it with this simple schema/document combination:

Schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="authors">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="author" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="name" use="required"/>
                        <xs:attribute name="age" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
        <xs:unique name="nameUniqueness">
            <xs:selector xpath="author"/>
            <xs:field xpath="@name"/>
        </xs:unique>
    </xs:element>
</xs:schema>

Document instance
<?xml version="1.0" encoding="UTF-8"?>
<authors xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///./authors.xsd">
    <author name="foo" age="1"/>
    <author name="bar" age="2"/>
    <author name="baz" age="3"/>
</authors>

In this example, the document should validate, since all 3 authors' name attributes are unique. Using XMLSpy the document does indeed validate, and if I change one of the authors to be a duplicate (i.e. change "baz" to "bar" so now there are two "bar" entries) and validate again in XMLSpy it fails saying that "Duplicate value: ['bar'] was already matched by the <unique> identity constraint 'nameUniqueness' within the scope of element <authors>." So from an XML standpoint everything seems to work fine.

But creating a small Qt onlyapplication (based on the "schema" example shipped with Qt) of my own that attempts to validate this example using QXmlSchemaValidator fails. The error message is "Non-unique value found for constraint nameUniqueness", and it appears to fail on the last author listed, so in this case the "baz" line. But if you delete the last author, the error just moves up a line - so deleting the "baz" line moves the error up to the "bar" line. If I delete both the "baz" and "bar" lines, then it will validate...

Am I doing something wrong, or is this just a bug?
Sean


More information about the Interest mailing list