Search code examples
xmlxsdxquerybasex

XML IDREF works only as an attribute but not as an element


I have the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="simple_model.xsd">
    <cars>
        <passengers>
            <person idref="ud9e69b75-1604-4a85-9747-5ec530b3641b"/>
        </passengers>
    </cars>
    <persons>
        <person myid="ud9e69b75-1604-4a85-9747-5ec530b3641b">
            <name>Paul</name>
        </person>
    </persons>
</root>

with the corresponding schema:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="root" type="root"/>

  <xs:complexType name="root">
    <xs:sequence>
      <xs:element name="cars" type="car" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="persons" minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="person" type="person" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="car">
    <xs:sequence>
      <xs:element name="passengers" minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="person" type="referencePerson" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="referencePerson">
    <xs:sequence/>
    <xs:attribute name="idref" type="xs:IDREF"/>
  </xs:complexType>

  <xs:complexType name="person">
    <xs:sequence>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="myid" type="xs:ID"/>
  </xs:complexType>
</xs:schema>

When I now call idref("ud9e69b75-1604-4a85-9747-5ec530b3641b")/.. it returns me: <person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" idref="ud9e69b75-1604-4a85-9747-5ec530b3641b"/>

So far so good. But when I change the idref from an attribute to an element. Like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="simple_model.xsd">
    <cars>
        <passengers>
            <person>u74835c8c-e793-4fca-a370-27ff6f3f06d7</person>
        </passengers>
    </cars>
    <persons>
        <person myid="u74835c8c-e793-4fca-a370-27ff6f3f06d7">
            <name>Paul</name>
        </person>
    </persons>
</root>

and the corresponding schema changes to:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="root" type="root"/>

  <xs:complexType name="root">
    <xs:sequence>
      <xs:element name="cars" type="car" minOccurs="0" maxOccurs="unbounded"/>
      <xs:element name="persons" minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="person" type="person" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="car">
    <xs:sequence>
      <xs:element name="passengers" minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="person" type="xs:IDREF" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="person">
    <xs:sequence>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="myid" type="xs:ID"/>
  </xs:complexType>
</xs:schema>

Then the call idref("u74835c8c-e793-4fca-a370-27ff6f3f06d7") returns nothing. I tried it with BaseX and XML author but both show no results. Do you know what I missed?


Solution

  • It works as expected in Saxon, so I think it's either a product-specific issue, or something to do with the way you are running the query.