问题
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