Search code examples
xmlxquerysaxonidref

How to use the XQuery fn:idref() function?


I can't get the XQuery function fn:idref() to return anything.

I have this XML document doc.xml;

<doc>
  <foo idref="xyz"/>
  <bar xml:id="xyz"/>
</doc>

And this XQuery;

let $d := doc("doc.xml")
return $d/idref("xyz")

But the result is always empty. I'm guessing that the attribute idref="xyz" needs to be declared as type idref but can that be done without a schema?

I'm using Saxon XQuery 1.0 processor.


Solution

  • Yes, the idref() function only retrieves attributes labelled as being IDREFs, and that only happens as a result of schema validation.

    There's a deprecated legacy setting config.setRetainDTDAttributeTypes() that allows it to work as a result of DTD validation, but I wouldn't use it.

    You can always do doc("doc.xml")//*[@idref="xyz"]. This will result in a serial search in Saxon-HE and Saxon-PE, but will make use of an index in Saxon-EE. I would generally recommend this over using the idref() function.

    In XSLT, of course, you can use keys.