XPath 语法 - 如何在复杂的位置路径中使用 position()

Eli*_*ist 2 xml xpath position

我正在编写一个递归算法来为给定元素生成唯一的最小 XPath。主要思想是允许在一个文档中选择一个元素(即 PC 上 chrome 中的 html 元素),并能够在类似文档(即移动版本中的同一站点)中找到相应的元素。

在此过程中,我需要为整个给定文档生成一个完整的 XPath 字符串,即对于给定节点,遍历整个树并将所有节点及其所有属性附加到字符串中。

例如,对于以下文档(想要的元素用“*”标记):

<?xml version="1.0" encoding="UTF-16"?>
<node>
    <node/>
    <node id="content">
        <node>
            <node>
                <node id="url_text_field"/>
                *<node id="go_button" text="Go">
                </node>*
                <node id="back_button" text="Back">
                </node>
            </node>
            <node id="webViewPlaceholder">
                <node/>
            </node>
        </node>
    </node>
</node>
Run Code Online (Sandbox Code Playgroud)

我的代码生成的 XPath:

//*[@id='go_button' and @text='Go' and parent::*[child::*[@id='url_text_field'] and child::*[@id='back_button' and @text='Back'] and parent::*[child::*[@id='webViewPlaceholder'] and parent::*[@id='content']]]]
Run Code Online (Sandbox Code Playgroud)

yield<node id="go_button" text="Go"> 完全符合元素。

我的问题是,在一种特殊情况下(即 - 当包含所需元素的子树具有相同的“兄弟”时)我必须使用该元素的position()=SOME_NUMBER(或“索引”节点[SOME_NUMBER])来唯一标识元素,我在语法上遇到了问题。

例如,对于更复杂的文档(同样,元素被标记为“*”。index 属性不是原始文档的一部分,添加只是为了参考):

<?xml version="1.0" encoding="UTF-16"?>
<node>
    <node/>
    <node id="content" index="a">
        <node>
            <node>
                <node id="url_text_field"/>
                <node id="go_button" text="Go" index="a1">
                </node>
                *<node id="go_button" text="Go" index="a2">
                </node>*
                <node id="back_button" text="Back">
                </node>
            </node>
            <node id="webViewPlaceholder">
                <node/>
            </node>
        </node>
    </node>
    <node id="content" index="b">
        <node>
            <node>
                <node id="url_text_field"/>
                <node id="go_button" text="Go" index="b1">
                </node>
                <node id="go_button" text="Go" index="b2">
                </node>
                <node id="back_button" text="Back">
                </node>
            </node>
            <node id="webViewPlaceholder">
                <node/>
            </node>
        </node>
    </node>
</node>
Run Code Online (Sandbox Code Playgroud)

当然,前面的 XPath 找到了四个元素:

<node id="go_button" text="Go" index="a1"></node>
<node id="go_button" text="Go" index="a2"></node>
<node id="go_button" text="Go" index="b1"></node>
<node id="go_button" text="Go" index="b2"></node>
Run Code Online (Sandbox Code Playgroud)

我尝试在 XPath 的各个位置添加位置节点(例如//*[@id='go_button' and @text='Go' and position=2 and parent::*[child::*[@id='url_text_field'] and child::*[@id='back_button' and @text='Back'] and parent::*[child::*[@id='webViewPlaceholder'] and parent::*[@id='content'][1]]]]不起作用),但找不到仅匹配第一个“父”子树下的第二个“兄弟”子树的方法。

dre*_*rew 5

只需附加[postion()=1]到您的路径表达式 - 这将返回您的 xml 中与您的路径匹配的两个节点(索引 a1 和 b1)。如果你只想要第一个,那么不要使用///descendant::而是使用。 //往往会产生意想不到的后果。


Eli*_*ist 5

解决方案是使用索引节点而不是调用position.
我用圆括号括住整个 xpath 表达式,并添加索引:

(xpath_expression)[index]
Run Code Online (Sandbox Code Playgroud)