[xquery-talk] getting an attrib value from another attrib value

Michael Kay mike at saxonica.com
Mon Dec 4 14:24:03 PST 2006


> The answer you are looking for is:
> 
> doc("...")//*[@name = "Torquay"]/@uid
> 

Just one little addition:

> I have only got so far as being able to get the whole node (i.e. 
> including children) with the matching name attribute as follows:

The query above returns a single attribute node (assuming Torquay is unique,
which I know it is!).

If you changed the expression to:

doc("...")//*[@name = "Torquay"]

then the query still returns only a single node, this time an element node.

The difference only arises if and when you serialize the results. The
serialization spec actually doesn't allow you to serialize a single
attribute node (because the result of serialization is always XML, and in
lexical XML attributes can't exist in isolation). That means the serializer
will choke on John Snelson's solition. When you serialize an element, on the
other hand, you get the whole subtree rooted at that element. But if you are
returning the results to an application, without serialization, then either
expression is fine.

If you just want the uid as a string you can write:

doc("...")//*[@name = "Torquay"]/@uid/string()

Michael Kay
http://www.saxonica.com/




More information about the talk mailing list