XQuery / XPath:如何在使用“|”时从祖先节点检索属性值操作员?

XQuery / XPath: How to retrieve an attribute value from an ancestor node when using "|" operator?

我正在将 XQuery 文件应用于两个 XML 文件:

-file1/snippet1-

<entry xml:id="SCHOM-2">
    <form type="hyperlemma" xml:lang="cu">
        <orth>абиѥ</orth>
    </form>
    <form type="lemma" xml:lang="cu">
        <orth>абиѥ</orth>
        <cit type="counterpart" xml:lang="grc">
            <form type="hyperlemma" xml:lang="grc">
                <orth>παραχρῆμα</orth>
            </form>
            <form type="lemma" xml:lang="grc">
                <orth>παραχρῆμα</orth>
            </form>
        </cit>
    </form>
</entry>

和 -file2/snippet2-

<entry xml:id="arg-3150">
    <form type="hyperlemma" xml:lang="grc">
        <orth>ἐξαυτῆς</orth>
    </form>
    <form type="lemma" xml:lang="grc">
        <orth>ἐξαυτῆς</orth>
        <cit type="translation" xml:lang="cu">
            <form type="hyperlemma" xml:lang="cu">
                <orth>абиѥ</orth>
            </form>
            <form type="lemma" xml:lang="cu">
                <orth>абие</orth>
            </form>
        </cit>
    </form>
</entry>

两个片段具有相同的结构。如果我在任何 <form type="hyperlemma"> 中查找“абиѥ”,snippet1 的路径是 $file//entry/form[@type='hyperlemma']/orth (path1),而 snippet2 的路径是 $file//entry/form/cit/form[@type='hyperlemma']/orth (path2).

在 XQuery 文件中,我有以下 FLWOR 表达式:

xquery version "3.0";
...
for $file in collection($collection_path),
    $path_to_hyperlemma in $file//(entry | cit)/form[@type='hyperlemma']/orth [ft:query(., $searchphrase)]
let $entry_number := $path_to_hyperlemma/../../@xml:id
return
....

$entry_number应该存储<entry xml:id="">的属性值。但是我这样做的方式(使用/../../)只有returns,当然,$file//entry/form[@type='hyperlemma']/orth的属性值。

是否可以在$entry_number中存储属性值,不管是path1还是path2?

另一个问题:我知道 // 的使用在性能方面并不理想。但是,如果我将 // 替换为绝对路径,我似乎无法引用不同级别的节点集。在这种情况下是否可以设置绝对路径?

以下重写可能有所帮助:

for $file in collection($collection_path),
    $path_to_hyperlemma in $file/(descendant::entry | descendant::cit)/
       form[@type='hyperlemma']/orth[ft:query(., $searchphrase)]
let $entry_number := $path_to_hyperlemma/ancestor::*/@xml:id
return ...
  • xml:id 属性已为所有祖先解析。这仅在只有一个祖先具有此类属性的情况下才有效。如果可能还有更多,您需要使用 select 其中之一(例如使用 [position() = 1][position() = last()])或者在您关于文档结构的问题中给我们一些说明。

  • descendant-or-self 步骤已通过使用括号内的 descendant 步骤删除。但是,请注意 // 无论如何都可能被 XQuery 处理器优化(如果可能,检查生成的查询计划)

let $entry_number := $path_to_hyperlemma/ancestor::entry[1]/@xml:id

将在树中查找名为 entry 的最近祖先元素的 xml:id