[xml] Failed schema validation with substitution groups and choice





Greetings.  While working on an XML based project for work, I ran into the
following bug with the libxml2 validator.  I've attached a simple test case
that, when run through xmllint, should reproduce the same results I am
receiving.

The problem is as follows.  The supplied libxml2 validator, xmllint, will fail
to validate the following instance document against the schema specified below
if you uncomment the line specified in the instance document; however when it
is commented, the instance document will validate just fine.

Instance document:
------------------

<?xml version="1.0" encoding="UTF-8"?>
<TestRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
          xsi:noNamespaceSchemaLocation="common-schema.xsd">
    <Events>
        <TestEvent>Test 1.</TestEvent>
        <!-- Uncomment the following element to cause libxml's xmllint to
            fail to validate. -->
        <!-- <TestEvent>Test 2.</TestEvent> -->
    </Events>
</TestRoot>



Schema to validate against:
---------------------------

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

    <!-- This is the head of our substitution group for events that have only
        simple content. -->
    <xs:element name="SimpleEvent" type="SimpleEventType"/>

    <!-- All SimpleEvent elements have the required set of attributes -->
    <xs:complexType name="SimpleEventType">
        <xs:simpleContent>
            <xs:extension base="xs:anySimpleType">
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

    <!-- Common members of the SimpleEvent substitution group -->
    <xs:element name="TestEvent" substitutionGroup="SimpleEvent">
        <xs:complexType>
            <xs:simpleContent>
                <xs:restriction base="SimpleEventType">
                    <xs:simpleType>
                        <xs:restriction base="xs:string"/>
                    </xs:simpleType>
                </xs:restriction>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>

    <!-- Root element -->
    <xs:element name="TestRoot" type="TestRootType"/>

    <!-- Core data type of an audit trail -->
    <xs:complexType name="TestRootType">
        <xs:sequence>
            <xs:element name="Events">
                <xs:complexType>
                    <xs:choice minOccurs="0" maxOccurs="unbounded">
                        <xs:element ref="SimpleEvent"/>
                    </xs:choice>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

</xs:schema>



I have done numerous test cases and pinned the problem to be the fact that a
substitution group is being used inside the choice group element, which is
specified to appear zero or more times in an instance document.  The key is
that each choice group contains one SimpleEvent, or any element that can be
substitutable for SimpleEvent (in this case, TestEvent is declared to be part
of the SimpleEvent substitution group, so it should work just fine).  In
essense, any choice group that contains an element that can be substituted by
another element in the corresponding substitution group, and if the schema
allows for that choice group to appear multiple times in an instance document,
then the validator will fail to validate any instance document that has more
than substitutable element specified (as in the above case when the second
SimpleElement is uncommented from the instance document).

From what I can gather, the libxml2 validator is getting confused and instead of
thinking that the two TestEvent elements in the original instance document
belong to two separate choice groups, it actually thinks that they are both in
the same choice group.  If this is true, this would explain the error message
that is obtained when I run xmllint:

"element TestEvent: Schemas validity error : Element 'TestEvent': This element
is not expected."

I have tried changing the choice group to a sequence group, and the exact same
error occurs.  I have also used the following validators, and they all will
validate the supplied instance document against the supplied schema without any
problem: Xerces, MSXML 4.0 SP2, MSXML.NET, and XSV, hence leading me to believe
this is truly a bug with the libxml2 validator.

I've also filled out a Bugzilla bug report describing what has been described in
this post.

Thanks,
Michael Romer



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]