XPath: using the root element

Author: Dave Cassel  |  Category: Software Development

In XQuery, I commonly find myself writing XPath statements to navigate into some block of XML to find some tasty nugget that’s in there somewhere. When doing so, I’ve found one particular aspect that frequently throws me off, getting me empty results instead of the information I wanted. Having finally figured it out, and after confirming with a friend that I’m not the only one who messes this up, here’s a little lesson to help you write your XPath.

Let’s start with the question that I found confusing: if you want to specify a full XPath from the top node down to what you’re looking for, should you specify the root node? Seems like a simple thing, but as is so often the case, the answer is, “it depends”. Consider this XML:

<doc>
  <book>
    <name>The Fellowship of the Ring</name>
  </book>
  <author>
    <name>J. R. R. Tolkien</name>
  </author>
</doc>

I want to retrieve the name of the book using XPath. I can’t just ask for “//name”, because I’d get two results, the book’s name and the author’s. I’ll be more precise and specify the full path. So what XPath do I use: “/doc/book/name” or “/book/name”?

It depends on how we got this chunk of XML. If our XML is a document, then the variable that holds the XML is pointing to the whole document, and <doc/> is the top-level node of that doc. So, assuming that /book.xml contains the XML above:

let $doc := fn:doc('/book.xml')
return $doc/doc/book/name

But if the XML is constructed, then the node that holds it is pointing to the top-level node itself — there is no document.

let $str :=
  <doc>
    <book>
      <name>The Fellowship of the Ring</name>
    </book>
    <author>
      <name>J. R. R. Tolkien</name>
    </author>
  </doc>
return $str/book/name

Simple as that. Now a little test: which XPath expression would you use in this case?

let $str :=
  xdmp:unquote('
    <doc>
      <book>
        <name>The Fellowship of the Ring</name>
      </book>
      <author>
        <name>J. R. R. Tolkien</name>
      </author>
    </doc>')

To find the answer, you need to know that xdmp:unquote() returns a document-node() (actually one or more). Now that we know we’ll get a document back, the answer is simple:

return $doc/doc/book/name

Tags: , ,

2 Responses to “XPath: using the root element”

  1. vani k Says:

    let $query:=processInstance/tasks/task/auditTask/createdOn 2017-04-25T23:59:59

    This xml got generated when I serialized my xquery. I am having problem with the operator=”≤”. when I do xdmp:unquote, and then do a let $colQuery as cts:query :=cts:query($qNode/*)

    it gives error about invalid operation. since the expected string is “<=" and not ascii version of it.

    how does one deal with doing xdmp:unquote of a string that has less than or greater than chars and the use the resulting node to create a cts:query?

    Please any help wud be greatly appreciated.

  2. Dave Cassel Says:

    vani, some of your formatting didn’t come through, so your question isn’t clear. Can you try again, using &amp; escaping for the XML?

Leave a Reply