XQuery Annoyances…

If you are used to XSLT 1.0 and XForms, you see { $book/bk:title } and think nothing of it. XSLT 1.0 calls the curly-brace construct an Attribute Value Template, which is pretty descriptive of where it’s used. Always in an attribute, always converted into a string, even if you are actually pointing to an element.

In XQuery, though, the curly-brace construct can be used in many different places. Depending on the context, the above code might well insert a bk:title element into your output. The proper thing to do, of course, is { $book/bk:title/text() }. Many XSLT and XForms authors would omit the extra text() selector as superfluous, but in XQuery it matters.

What’s worse, depending on your browser, you might not see any output on the page within a <bk:title> element (or a title element of any namespace). Caveat browser! -m


One Response to “XQuery Annoyances…”

  1. Jeni Tennison http://www.jenitennison.com/


    Congrats on your move to MarkLogic!

    Bear in mind that if you use $book/bk:title/text() you’ll run into problems with mixed content (that’s why we don’t do that in XSLT). If that’s something you need to worry about, you probably want to pass the bk:title to a function that handles its content properly.

    If bk:title doesn’t have mixed content, or if you don’t care about the mixed content, I’d be inclined to use data($book/bk:title) or $book/bk:title/data(.) if you prefer, which returns an atomic value, rather than $book/bk:title/text(), which returns a sequence of nodes. But I think this is because of years of suffering with first-item semantics in XSLT.

MicahLogic is Stephen Fry proof thanks to caching by WP Super Cache