xquerysaxonbasex

how to query the result of fn:analyze-string()?


In XQuery (in BaseX which uses Saxon 10.something as well as in XSLT with Saxon 9.9.1.7 within Oxygen), I tried to iterate the matches of fn:analyze-string() by fn:analyze-string($term, $pattern)/match which did not give me any output although the function itself returned the typical element with <match> and <non-match> childnodes. According to the definition, the function returns "[...] an XML structure [...]" (cf.). Hence, one should be able to query it like that and the given expression should work.

Since it did not, I then tried fn:analyze-string($term, $pattern)/*[fn:local-name(.) eq 'match']...and it worked. Replacing the asterisk and the condition in the brackets with child::match etc. didn't, however.

My question is: Why? What is the difference; what did I miss here? In all cases, the child nodes are queried by the name match. So, why does only the version with the asterisk get me the results?


Solution

  • The match elements are in a namespace, specifically the "fn" namespace (http://www.w3.org/2005/xpath-functions). So the unqualified name test match won't find them unless this happens to be the default namespace for elements (which won't be the case by default). You should match them using fn:match (where fn is bound to the right namespace), or *:match.

    This question is probably the most commonly asked question on StackOverflow about XSLT, XPath, and XQuery. However, because it's asked in so many different ways and the symptoms vary so widely, it's hard to close it as a duplicate. But if you search for "XPath default namespace" you'll find thousands of other users (2137 at the last count) falling into the same error.