XQuery returns node text if it contains the specified keyword

Below is an example of a test example of my xml file:

test.xml

<feed> <entry> <title>Link ISBN</title> <libx:libapp xmlns:libx="http://libx.org/xml/libx2" /> </entry> <entry> <title>Link Something</title> <libx:module xmlns:libx="http://libx.org/xml/libx2" /> </entry> </feed> 

Now I want to write an xquery that will find all the <entry> elements for which <libx:libapp> will be a child. Then, for all such records, the title is returned if the title contains the specified keyword (for example, a link). So, in my example xml document, xquery should return β€œLink ISBN”.

My xquery example is shown below:

samplequery.xq (here doc_name is the xml file shown above, and libapp_matchkey is a keyword such as "Link")

 declare namespace libx='http://libx.org/xml/libx2'; declare variable $doc_name as xs:string external; declare variable $libpp_matchkey as xs:string external; let $feeds_doc := doc($doc_name) for $entry in $feeds_doc/feed/entry (: test whether entry has libx:libapp child and has "Link" in its title child :) where ($entry/libx:libapp and $entry/title/text()[contains(.,$libapp_matchkey)]) return $entry/title/text() 

This xquery returns null instead of the expected result of 'Link ISBN'. Why is this?

+4
source share
1 answer

I want to write an xquery that will find all the elements that are like a child. Then, for all such records, a title is returned if the title contains the specified keyword (for example, Link).

Just use :

 /*/entry[libx:libapp]/title[contains(.,'Link')]/text() 

Combining this XPath expression in XQuery, we get :

 declare namespace libx='http://libx.org/xml/libx2'; /*/entry[libx:libapp]/title[contains(.,'Link')]/text() 

when applied to the provided XML document :

 <feed> <entry> <title>Link ISBN</title> <libx:libapp xmlns:libx="http://libx.org/xml/libx2" /> </entry> <entry> <title>Link Something</title> <libx:module xmlns:libx="http://libx.org/xml/libx2" /> </entry> </feed> 

required, the correct result is obtained :

 Link ISBN 
+6
source

Source: https://habr.com/ru/post/1332142/


All Articles