0
votes

I'm writing a library that generates complex XQuery expressions that target documents from two different namespaces. However, when I'm generating the query I don't know the particular namespace for each element I have to query, so I'm currently generating a disjunction of the two possible combinations:

//*[ns1:foo/ns1:bar = "some condition" or ns2:foo/ns2:bar = "some condition"]

Is there any way to generate this expression more nicely and more optimal in terms of performance?

One possible solution I found is to use:

declare default element namespace "*"

in which case I could simply generate:

//*[foo/bar = "some condition"]

but looks like that may add additional performance overhead as I cannot enumerate the two possible namespace there and use wildcard instead.

1
would something along the lines of //*[local-name()='foo']/bar[*condition here*] suffice? - JWiley

1 Answers

0
votes

My usual approach where you have to write a query that deals with two variants of an input source is to first have a phase where the input is normalized to eliminate the variation, and then run the main query over the normalized input.

One advantage of this is that the normalization step is reusable - it can be used whatever subsequent processing you want to do.

(Copying a document while changing all the namespace URIs - or indeed any such algorithmic transformation - is easier in XSLT than in XQuery, but it can be done in XQuery if you really want.)