两个单例标签之间的XQuery内容

时间:2012-07-18 11:14:33

标签: xml xquery

您知道如何在两个单例标签之间选择内容进行XQuery查询,例如

<pb n="1"/>
   this is <foo>page</foo> number one
<pb n="2"/>
   this is <bar>page</bar> number two
<pb n="3"/>
   this is <x>page</x> number three

我想要第二页的内容,所以在<pb n="2"/>和下一个<pb/>之间。输出应该是:

   this is <bar>page</bar> number two

3 个答案:

答案 0 :(得分:3)

您可以使用following-sibling<<运算符一起执行此操作,如下所示:

let $xml := <xml>
<pb n="1"/> 
   this is <foo>page</foo> number one 
<pb n="2"/> 
   this is <bar>page</bar> number two 
<pb n="3"/> 
   this is <x>page</x> number three 
</xml>
return
  $xml/pb[2]/following-sibling::node()[. << $xml/pb[3]]

HTH!

答案 1 :(得分:1)

在TEI wiki上查看David Sewell的milestone-chunk()函数。请注意,本文还指向了eXist-db扩展函数util:get-fragment-between()

答案 2 :(得分:0)

这个查询恰好是纯XPath 2.0表达式

/*/node()[not(self::pb) and preceding-sibling::pb[1]/@n eq '2']

在此XML文档上执行(提供的XML片段包装在单个顶部元素中):

<t>
    <pb n="1"/>
       this is <foo>page</foo> number one
    <pb n="2"/>
       this is <bar>page</bar> number two
    <pb n="3"/>
       this is <x>page</x> number three
</t>

产生了想要的正确结果:

   this is <bar>page</bar> number two
相关问题