xpath navigating documents in a collection with sibling

夙愿已清 提交于 2019-12-13 17:45:04

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/57537549/xpath-navigating-documents-in-a-collection-with-sibling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!