RE: xs:key and nesting

George,

I tried your suggestion -- and various other experiments as well.  Nothing seems to work as I expect.


EXPERIMENT 1 - NO NESTING, NO EMPHASIS

XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.ubs.com/namespace/wbml-render"
    xmlns:wbr="http://www.ubs.com/namespace/wbml-render"
    xmlns="http://www.ubs.com/namespace/wbml-render" elementFormDefault="qualified"
    attributeFormDefault="unqualified">
    <xs:complexType name="footnoteBase" mixed="true">
        <xs:annotation>
            <xs:documentation>The text to which the footnote pertains.</xs:documentation>
        </xs:annotation>
    </xs:complexType>
    <xs:complexType name="footnote">
        <xs:annotation>
            <xs:documentation>Composed of base and explanation.</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="footnoteBase" type="footnoteBase"/>
            <xs:element name="footnoteExplanation" type="xs:string" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="footnoteId" type="xs:NMTOKEN" use="optional"/>
        <xs:attribute name="footnoteRef" type="xs:NMTOKEN" use="optional"/>
    </xs:complexType>
    <xs:element name="footnote" type="footnote"/>
    <xs:element name="para">
        <xs:complexType mixed="true">
            <xs:sequence>
                <xs:element ref="footnote" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
        <xs:key name="ref_or_explanation">
            <xs:selector xpath=".//wbr:footnote"/>
            <xs:field xpath="wbr:footnoteExplanation | @footnoteRef"/>
        </xs:key>
    </xs:element>
</xs:schema>

XML:
<?xml version="1.0" encoding="UTF-8"?>
<wbr:para xmlns:wbr="http://www.ubs.com/namespace/wbml-render"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.ubs.com/namespace/wbml-render
    file:/Users/richard/Documents/Work/Accessibility/Schemas/test_footnote_noNesting_noEmphasis.xsd"> Now is 
    <wbr:footnote footnoteRef="abc">
        <wbr:footnoteBase>the</wbr:footnoteBase>
        <!--<wbr:footnoteExplanation>As Clinton said, "It depends on what you mean by 'the'."</wbr:footnoteExplanation>-->
    </wbr:footnote> for 
    <wbr:footnote>
        <wbr:footnoteBase>the</wbr:footnoteBase>
        <wbr:footnoteExplanation>As Clinton said, "It depends on what you mean by 'the'."</wbr:footnoteExplanation>
    </wbr:footnote> for 
    <wbr:footnote>
        <wbr:footnoteBase>the</wbr:footnoteBase>
        <wbr:footnoteExplanation>As Hilary Clinton said, "It depends on what he means by 'the'."</wbr:footnoteExplanation>
    </wbr:footnote> for 
</wbr:para>

RESULT: XML is valid.

EXPERIMENT 2 - NO NESTING, EMPHASIS

XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.ubs.com/namespace/wbml-render"
    xmlns:wbr="http://www.ubs.com/namespace/wbml-render"
    xmlns="http://www.ubs.com/namespace/wbml-render" elementFormDefault="qualified"
    attributeFormDefault="unqualified">
    <xs:element name="emphasis" type="xs:string"/>
    <xs:complexType name="footnoteExplanationWithEmphasis" mixed="true">
        <xs:annotation>
            <xs:documentation>The text explaining the footnote. The explanation may have emphasis child elements (think italics or bold).</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element ref="emphasis" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="footnoteExplanation" type="xs:NMTOKEN" fixed="footnoteExplanation"/>
    </xs:complexType>
    <xs:complexType name="footnoteBase" mixed="true">
        <xs:annotation>
            <xs:documentation>The text to which the footnote pertains.</xs:documentation>
        </xs:annotation>
    </xs:complexType>
    <xs:complexType name="footnoteWithEmphasis">
        <xs:annotation>
            <xs:documentation>Composed of base and explanation.  Explanation may have emphasis child elements.</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="footnoteBase" type="footnoteBase"/>
            <xs:element name="footnoteExplanation" type="footnoteExplanationWithEmphasis"
                minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="footnoteId" type="xs:NMTOKEN" use="optional"/>
        <xs:attribute name="footnoteRef" type="xs:NMTOKEN" use="optional"/>
    </xs:complexType>
    <xs:element name="footnote" type="footnoteWithEmphasis"/>
    <xs:element name="para">
        <xs:complexType mixed="true">
            <xs:sequence>
                <xs:element ref="footnote" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
        <xs:key name="ref_or_explanation">
            <xs:selector xpath=".//wbr:footnote"/>
            <xs:field xpath="wbr:footnoteExplanation/@footnoteExplanation | @footnoteRef"/>
        </xs:key>
    </xs:element>
</xs:schema>

Note the modifications necessary in order to avoid a mix of complex and simple type in the xs:field!

The XML of Experiment 1 should validate -- at least, that's my intention! -- but it does not.  Xerces (i.e., Oxygen 7) complains that the last two elements have duplicate key.  By moving the xs:key into the element, I can avoid this error message:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.ubs.com/namespace/wbml-render"
    xmlns:wbr="http://www.ubs.com/namespace/wbml-render"
    xmlns="http://www.ubs.com/namespace/wbml-render" elementFormDefault="qualified"
    attributeFormDefault="unqualified">
    <xs:element name="emphasis" type="xs:string"/>
    <xs:complexType name="footnoteExplanationWithEmphasis" mixed="true">
        <xs:annotation>
            <xs:documentation>The text explaining the footnote. The explanation may have emphasis child elements (think italics or bold).</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element ref="emphasis" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="footnoteExplanation" type="xs:NMTOKEN" fixed="footnoteExplanation"/>
    </xs:complexType>
    <xs:complexType name="footnoteBase" mixed="true">
        <xs:annotation>
            <xs:documentation>The text to which the footnote pertains.</xs:documentation>
        </xs:annotation>
    </xs:complexType>
    <xs:complexType name="footnoteWithEmphasis">
        <xs:annotation>
            <xs:documentation>Composed of base and explanation.  Explanation may have emphasis child elements.</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="footnoteBase" type="footnoteBase"/>
            <xs:element name="footnoteExplanation" type="footnoteExplanationWithEmphasis"
                minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="footnoteId" type="xs:NMTOKEN" use="optional"/>
        <xs:attribute name="footnoteRef" type="xs:NMTOKEN" use="optional"/>
    </xs:complexType>
    <xs:element name="footnote" type="footnoteWithEmphasis">
        <xs:key name="ref_or_explanation">
            <xs:selector xpath="."/>
            <xs:field xpath="wbr:footnoteExplanation/@footnoteExplanation | @footnoteRef"/>
        </xs:key>
    </xs:element>
    <xs:element name="para">
        <xs:complexType mixed="true">
            <xs:sequence>
                <xs:element ref="footnote" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

XML:
<?xml version="1.0" encoding="UTF-8"?>
<wbr:para xmlns:wbr="http://www.ubs.com/namespace/wbml-render"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.ubs.com/namespace/wbml-render
    file:/Users/richard/Documents/Work/Accessibility/Schemas/test_footnote_noNesting_Emphasis.xsd"> Now is 
    <wbr:footnote footnoteRef="abc">
        <wbr:footnoteBase>the</wbr:footnoteBase>
        <!--<wbr:footnoteExplanation>As Clinton said, "It depends on what you mean by 'the'."</wbr:footnoteExplanation>-->
    </wbr:footnote> for 
    <wbr:footnote>
        <wbr:footnoteBase>the</wbr:footnoteBase>
        <wbr:footnoteExplanation>As Clinton said, "It depends on what you mean by '<wbr:emphasis>the</wbr:emphasis>'."</wbr:footnoteExplanation>
    </wbr:footnote> for 
    <wbr:footnote>
        <wbr:footnoteBase>the</wbr:footnoteBase>
        <wbr:footnoteExplanation>As <wbr:emphasis>Hilary Clinton</wbr:emphasis> said, "It depends on what he means by 'the'."</wbr:footnoteExplanation>
    </wbr:footnote> for 
</wbr:para>

This has emphasis elements and validates.

EXPERIMENT 3 - NESTING, EMPHASIS

XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.ubs.com/namespace/wbml-render"
    xmlns:wbr="http://www.ubs.com/namespace/wbml-render"
    xmlns="http://www.ubs.com/namespace/wbml-render" elementFormDefault="qualified"
    attributeFormDefault="unqualified">
    <xs:element name="emphasis" type="xs:string"/>
    <xs:complexType name="footnoteExplanationWithEmphasis" mixed="true">
        <xs:annotation>
            <xs:documentation>The text explaining the footnote. The explanation may have emphasis child elements (think italics or bold).</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element ref="emphasis" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="footnoteExplanation" type="xs:NMTOKEN" fixed="footnoteExplanation"/>
    </xs:complexType>
    <xs:complexType name="footnoteBase" mixed="true">
        <xs:annotation>
            <xs:documentation>The text to which the footnote pertains.</xs:documentation>
        </xs:annotation>
    </xs:complexType>
    <xs:complexType name="footnoteWithEmphasis">
        <xs:annotation>
            <xs:documentation>Composed of base and explanation.  Explanation may have emphasis child elements.</xs:documentation>
        </xs:annotation>
        <xs:sequence>
            <xs:element name="footnoteBase" type="footnoteBase"/>
            <xs:element name="footnoteExplanation" type="footnoteExplanationWithEmphasis"
                minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="footnoteId" type="xs:NMTOKEN" use="optional"/>
        <xs:attribute name="footnoteRef" type="xs:NMTOKEN" use="optional"/>
    </xs:complexType>
    <xs:element name="footnote" type="footnoteWithEmphasis">
        <xs:key name="ref_or_explanation">
            <xs:selector xpath="."/>
            <xs:field xpath="wbr:footnoteExplanation/@footnoteExplanation | @footnoteRef"/>
        </xs:key>
    </xs:element>
    <xs:element name="para">
        <xs:complexType mixed="true">
            <xs:sequence>
                <xs:element ref="footnote" minOccurs="0" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

XML:
<?xml version="1.0" encoding="UTF-8"?>
<wbr:para xmlns:wbr="http://www.ubs.com/namespace/wbml-render"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.ubs.com/namespace/wbml-render
    file:/Users/richard/Documents/Work/Accessibility/Schemas/test_footnote_Nesting_Emphasis.xsd"> Now is 
    <wbr:footnote footnoteRef="abc">
        <wbr:footnoteBase>the</wbr:footnoteBase>
        <!--<wbr:footnoteExplanation>As Clinton said, "It depends on what you mean by 'the'."</wbr:footnoteExplanation>-->
    </wbr:footnote> for 
    <wbr:footnote>
        <wbr:footnoteBase>the</wbr:footnoteBase>
        <wbr:footnoteExplanation>As Clinton said, "It depends on what you mean by '<wbr:emphasis>the</wbr:emphasis>'."</wbr:footnoteExplanation>
    </wbr:footnote> for 
    <wbr:footnote>
        <wbr:footnoteBase>the</wbr:footnoteBase>
        <wbr:footnoteExplanation>As <wbr:emphasis>Hilary Clinton</wbr:emphasis> said, "It depends on what he means by 'the'."</wbr:footnoteExplanation>
    </wbr:footnote> for 
    <wbr:footnote>
        <wbr:footnoteBase>
            <wbr:footnote>
                <wbr:footnoteBase>
                    <wbr:footnote footnoteRef="x22">
                        <wbr:footnoteBase>all</wbr:footnoteBase>
                    </wbr:footnote> good
                </wbr:footnoteBase>
                <wbr:footnoteExplanation>In some inner sense.</wbr:footnoteExplanation>
            </wbr:footnote> men
        </wbr:footnoteBase>
        <wbr:footnoteExplanation>Good women, too.</wbr:footnoteExplanation>
    </wbr:footnote>
</wbr:para>

Of the nested footnotes only the inner-most validates.  For each of the containing footnotes Xerces complains that the field matches more than one value within the scope of its selector, but that the key has no value!  Commenting out the inner-most footnote cause what then becomes the inner-most footnote to suddenly become valid; the enclosing footnote is still not.

In summary, there seem to be many problems:
1.  As you noted, there is a problem with the order in which the particles of the 'or' (|) are specified.
2.  The xs:key must be attached to the footnote, not to an element that is sure to be an ancestor of any footnote, if Xerces is to validate even unnested footnote.
3.  Even attaching the xs:key to each footnote does not seem to isolate them from each other sufficiently to ensure that Xerces validates nested footnotes.

Interestingly, LIBXML validate the sample of with nesting and emphasis.

Can you give me some insight into what Xerces might be doing to arrive at its conclusion that my XML is invalid?

Thanks,
Richard


-----Original Message-----
From: George Cristian Bina [mailto:george@oxygenxml.com]
Sent: Fri 02/17/06 09:36
To: George Cristian Bina
Cc: Liu, Richard; xmlschema-dev@w3.org
Subject: Re: xs:key and nesting
 
Looking over the already recorded issues it seems that this is a 
manifestation of the
https://issues.apache.org/jira/browse/XERCESJ-832

A workaround is to replace

<xs:field xpath="wbr:footnoteExplanation | @footnoteRef"/>

with

<xs:field xpath="@footnoteRef | wbr:footnoteExplanation"/>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


George Cristian Bina wrote:
> The problem appears also with the latest SVN snapshot. I will report 
> that on Apache Jira as soon as that will be available, now I get 
> "Service Temporarily Unavailable" when trying to access Apache Jira.
> 
> Best Regards,
> George
> ---------------------------------------------------------------------
> George Cristian Bina
> <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
> http://www.oxygenxml.com
> 
> 
> George Cristian Bina wrote:
>> Hi Richard,
>>
>> This looks to me like a bug in Xerces 2.7.1.
>> I will check that against the current Xerces SVN snapshot to see if it 
>> shows the same problem and let you know.
>>
>> Best Regards,
>> George
>> ---------------------------------------------------------------------
>> George Cristian Bina
>> <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
>> http://www.oxygenxml.com
>>
>>
>> richard.liu@ubs.com wrote:
>>> I have the following schema:
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>>>     targetNamespace="http://www.ubs.com/namespace/wbml-render"
>>>     xmlns:wbr="http://www.ubs.com/namespace/wbml-render"
>>>     xmlns="http://www.ubs.com/namespace/wbml-render"
>>> elementFormDefault="qualified"
>>>     attributeFormDefault="unqualified">
>>>     <xs:complexType name="footnoteExplanation">
>>>         <xs:annotation>
>>>             <xs:documentation>The text explaining the
>>> footnote.</xs:documentation>
>>>         </xs:annotation>
>>>         <xs:simpleContent>
>>>             <xs:extension base="xs:string"/>
>>>         </xs:simpleContent>
>>>     </xs:complexType>
>>>     <xs:complexType name="footnote">
>>>         <xs:annotation>
>>>             <xs:documentation>Composed of base and
>>> explanation.</xs:documentation>
>>>         </xs:annotation>
>>>         <xs:sequence>
>>>             <xs:element name="footnoteBase" type="footnoteBase"/>
>>>             <xs:element name="footnoteExplanation"
>>> type="footnoteExplanation" minOccurs="0"/>
>>>         </xs:sequence>
>>>         <xs:attribute name="footnoteId" type="xs:NMTOKEN"
>>> use="optional"/>
>>>         <xs:attribute name="footnoteRef" type="xs:NMTOKEN"
>>> use="optional"/>
>>>     </xs:complexType>
>>>     <xs:complexType name="footnoteBase" mixed="true">
>>>         <xs:annotation>
>>>             <xs:documentation>The text to which the footnote
>>> pertains.</xs:documentation>
>>>         </xs:annotation>
>>>         <xs:sequence>
>>>             <xs:element ref="footnote" minOccurs="0"
>>> maxOccurs="unbounded"/>
>>>         </xs:sequence>           </xs:complexType>
>>>     <xs:element name="footnote" type="footnote">
>>>         <!--<xs:key name="ref_or_explanation">
>>>             <xs:selector xpath="."/>
>>>             <xs:field xpath="wbr:footnoteExplanation | @footnoteRef"/>
>>>         </xs:key>-->
>>>     </xs:element>
>>>     <xs:element name="para">
>>>         <xs:complexType mixed="true">
>>>             <xs:sequence>
>>>                 <xs:element ref="footnote" minOccurs="0"
>>> maxOccurs="unbounded"/>
>>>             </xs:sequence>
>>>         </xs:complexType>
>>>         <xs:key name="ref_or_explanation">
>>>             <xs:selector xpath=".//wbr:footnote"/>
>>>             <xs:field xpath="wbr:footnoteExplanation | @footnoteRef"/>
>>>         </xs:key>
>>>     </xs:element>
>>> </xs:schema>
>>>
>>> and the following XML schema instance
>>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <wbr:para xmlns:wbr="http://www.ubs.com/namespace/wbml-render"
>>>     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>     xsi:schemaLocation="http://www.ubs.com/namespace/wbml-render
>>>  
>>> file:/Users/richard/Documents/Work/Accessibility/Schemas/test_footnote.x
>>> sd"> Now is     <wbr:footnote footnoteRef="abc">
>>>         <wbr:footnoteBase>the</wbr:footnoteBase>
>>>         <!--<wbr:footnoteExplanation>As Clinton said, "It depends on
>>> what you mean by 'the'."</wbr:footnoteExplanation>-->
>>>     </wbr:footnote> for     <wbr:footnote>
>>>         <wbr:footnoteBase>
>>>             <wbr:footnote>
>>>                 <wbr:footnoteBase>
>>>                     <wbr:footnote footnoteRef="x22">
>>>                         <wbr:footnoteBase>all</wbr:footnoteBase>
>>>                     </wbr:footnote> good
>>>                 </wbr:footnoteBase>
>>>                 <wbr:footnoteExplanation>In some inner
>>> sense.</wbr:footnoteExplanation>
>>>             </wbr:footnote> men
>>>         </wbr:footnoteBase>
>>>         <wbr:footnoteExplanation>Good women,
>>> too.</wbr:footnoteExplanation>
>>>     </wbr:footnote>
>>> </wbr:para>
>>>
>>> The xs:key on the para element is supposed to ensure that each footnote
>>> descendant has either a footnoteExplanation child or a footnoteRef
>>> attribute.  The first footnote child of para "works" as desired (in
>>> Oxygen 7, i.e., Xerces):  Uncommenting the footnoteExplanation element
>>> results in an error, as does deleting the footnoteRef attribute.
>>>
>>> The second footnote element is rather complicated, because the model
>>> permits "nesting" footnotes.  The XML seems valid:  The second footnote
>>> element has a footnoteExplanation and no footnoteRef, so does its first
>>> footnote descendent, and the innermost footnote has a footnoteRef
>>> attribute and no footnoteExplanation.
>>>
>>> Nevertheless, I receive the following two error messages (I've pasted
>>> the offending statement after the Location):
>>>
>>> SystemID:
>>> /Users/richard/Documents/Work/Accessibility/Schemas/test_footnote.xml
>>> Location: 18:88
>>>                 <wbr:footnoteExplanation>In some inner
>>> sense.</wbr:footnoteExplanation>
>>> Description: E Identity constraint error:  field
>>> "./wbr:footnoteExplanation|@footnoteRef" matches more than one value
>>> within the scope of its selector; fields must match unique values.
>>>
>>> SystemID:
>>> /Users/richard/Documents/Work/Accessibility/Schemas/test_footnote.xml
>>> Location: 21:76
>>>         <wbr:footnoteExplanation>Good women,
>>> too.</wbr:footnoteExplanation>
>>> Description: E Identity constraint error:  field
>>> "./wbr:footnoteExplanation|@footnoteRef" matches more than one value
>>> within the scope of its selector; fields must match unique values.
>>>
>>> Can anybody tell me what I'm missing?
>>>
>>> Thanks,
>>> Richard
>>>

Received on Friday, 17 February 2006 13:43:00 UTC