xpath 在同级集合中导航文档 [英] xpath navigating documents in a collection with sibling

查看:26
本文介绍了xpath 在同级集合中导航文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在变量 $data 中收集了 1000 个 TEI 文档(注意:每个文档中最顶层的节点是 tei:TEI).

在 Xpath 中(在 Xquery 下)我可以输出所有 @type="example" 文档:

let $coll := $data/tei:TEI[@type="example"]返回 $coll

然后我可以从上面的结果中进一步提取一个文档:

let $coll := $data/tei:TEI[@type="example"]返回 $coll[@xml:id="TC0005"]

以上工作正常.

现在,我想获取某个文档之前和之后的文档,我认为可以使用 preceding-sibling/following-sibling 来完成:

let $coll := $data/tei:TEI[@type="example"]返回 ($coll[@xml:id="TC0005"]/preceding-sibling[1],$coll[@xml:id="TC0005"],$coll[@xml:id="TC0005"]/following-sibling[1])

然而,上面只返回 $coll[@xml:id="TC0005"] 的文档.

此语法对于在集合内导航文档到文档是否正确?

非常感谢.

解决方案

在文档节点的集合或序列中,你没有任何兄弟姐妹,兄弟姐妹只存在于每个文档树中,所以我认为你只是想要在形式

let $examples := $data/tei:TEI[@type="example"]对于 $example 在 $examples 中的 $pos其中 $example/@xml:id = 'TC0005'返回 ($examples[$pos - 1],$example$examples[$pos + 1])

这是基于我对 XQuery 和 XPath 序列的理解,我希望它也适用于您在 XQuery 数据库中的集合.

I have a collection of 1000s of TEI documents in the variable $data (note: the top-most node in each document is tei:TEI).

Within Xpath (under Xquery) I can output all the @type="example" documents with:

let $coll := $data/tei:TEI[@type="example"]
return $coll

I can then further extract one document from the above result with:

let $coll := $data/tei:TEI[@type="example"]
return $coll[@xml:id="TC0005"]

The above work fine.

Now, I would like to get the documents before and after a certain document, which I assume could be done with preceding-sibling / following-sibling:

let $coll := $data/tei:TEI[@type="example"]
return ($coll[@xml:id="TC0005"]/preceding-sibling[1],
       $coll[@xml:id="TC0005"],
       $coll[@xml:id="TC0005"]/following-sibling[1])

However the above only returns the document for $coll[@xml:id="TC0005"].

Is this syntax correct for navigating document to document within the collection?

Many thanks.

解决方案

In a collection or sequence of document nodes you don't have any siblings, siblings only exists in each document tree, so I think you simply want positional access in the form of

let $examples := $data/tei:TEI[@type="example"]
for $example at $pos in $examples
where $example/@xml:id = 'TC0005'
return (
  $examples[$pos - 1],
  $example
  $examples[$pos + 1]
)

That is based on my understanding of XQuery and XPath sequences, I hope it applies to your collection in an XQuery database as well.

这篇关于xpath 在同级集合中导航文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆