[xquery-talk] Referencing a document from within a query

Andrew Welch andrew.j.welch at gmail.com
Mon Aug 22 05:48:47 PDT 2011


> Is it possible to perform a XQuery that references nodes that reside in a
> document which contains the query which is being processed?
> Here is an example:
> I need to select the nodes named "title" which have an attribute "lang"
> equal to value of attribute node /data/lang/@name in the document where the
> query is declared:
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <data>
>     <lang name="en" />
>     <res>
>     {
>         for $x in collection("books")//title[
> compare(@lang, doc(.)/data/lang/@name) = 0 ]
>         return $x/title
>     }
>     </res>
> </data>

You could/should treat it as a normal xquery instead of an xml
document and then do:

let $lang := 'en';
return
<data>
    <lang name="{$lang}" />
    <res>
    { collection("books")//title[@lang = $lang]/title }
    </res>
</data>

-- 
Andrew Welch
http://andrewjwelch.com



More information about the talk mailing list