如果XQuery包含给定的关键字,则返回文本节点

son*_*ony 4 xpath xquery

我的xml文件的测试样本如下所示:

的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>
Run Code Online (Sandbox Code Playgroud)

现在,我想编写一个xquery,它将找到所有<entry>具有<libx:libapp>子元素的元素.然后,对于所有这样的条目,如果标题包含给定的关键字(例如链接),则返回标题.因此,在我的示例xml文档中,xquery应该返回"Link ISBN".

我的示例xquery如下所示:

samplequery.xq(此处doc_name是上面显示的xml文件,libapp_matchkey是关键字,例如'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()
Run Code Online (Sandbox Code Playgroud)

此xquery返回null而不是预期的结果'Link ISBN'.这是为什么?

Dim*_*hev 7

我想写一个xquery,它将找到所有具有孩子的元素.然后,对于所有这样的条目,如果标题包含给定的关键字(例如链接),则返回标题.

只需使用:

/*/entry[libx:libapp]/title[contains(.,'Link')]/text()
Run Code Online (Sandbox Code Playgroud)

在XQuery中包装此XPath表达式,我们得到:

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

当应用于提供的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>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

Link ISBN
Run Code Online (Sandbox Code Playgroud)