I’m failing in my first attempt to get the fn:id() XQuery function to do something.
So far I have a document called bar.xml;
<bar>
<foo id="a"/>
<foo id="b"/>
<foo id="c"/>
</bar>
And an XQuery;
doc('bar.xml')/fn:id(("a", "c"))
But everything I try just seems to return an empty set.
I have yet to learn XML schemas. Can fn:id() work without declaring a separate schema file for bar.xml?
You need to indicate that your id attributes are ids.
One way to do this is to add a schema or DTD to the document that specifies this.
The other (far easier) way is to use
xml:idin place ofidin your document.On a related note, you may wish to use
element-with-id()instead ofid().element-with-id()does whatid()was originally intended to do, andidremains in the specification for compatibility reasons.