如何在两个目标节点之间形成不相关节点的Xpath [英] How to form Xpath with irrelevant nodes in between two target nodes

查看:29
本文介绍了如何在两个目标节点之间形成不相关节点的Xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用 Xpath 获取有时在目标节点之间存在不相关节点的节点.

我的 xml 文件通常采用以下形式:

<目标><目标>我想获得的文本</目标></目标><目标2><goal>我想获得的第二个目标</goal></target2><身体>

我需要将目标的目标和目标 2 的目标放在单独的字符串中.因此,我使用 Xpath "//body/target/goal" 获得了我想获得的文本",并使用 Xpath "//body/target2/goal" 获得了我想获得的第二个目标".

非常简单.

但是后来我发现在目标和目标(或目标 2 和目标)之间可能有单独的节点(未知),如下所示:

<目标><烦人的><目标>我想获得的文本</目标></烦人的></目标><目标2><想法><goal>我想获得的第二个目标</goal></noidea></target2><身体>

我仍然需要区分这两个目标,所以我不能只用//goal"来累积目标.我需要什么样的 2 个 Xpath 才能获得我需要获得的每个文本?

非常感谢您提前

解决方案

target/target2goal 之间使用通配符:<预>//身体/目标/*/目标

<预>//身体/目标2/*/目标

How would I obtain a node using Xpath that sometimes has irrelevant nodes in between goal nodes.

I have xml file typically in the form as following:

<body>
    <target>
         <goal>Text I want to obtain</goal>
    </target>
    <target2>
         <goal>Second Target I want to obtain</goal>
    </target2>
<body>

And I need to throw target's goal and target2's goals in separate string. So I got the "Text I want to obtain" using Xpath "//body/target/goal", and got the "Second Target I want to obtain" using Xpath "//body/target2/goal".

Very simple.

However later I found out that in between target and goal (or target 2 and goal) there could be separate nodes (not known) as following:

<body>
    <target>
         <annoying>
              <goal>Text I want to obtain</goal>
         </annoying>
    </target>
    <target2>
         <noidea>
              <goal>Second Target I want to obtain</goal>
         </noidea>
    </target2>
<body>

I still have to distinguish the two targets so I cannot just accumulate goal with "//goal". What kind of 2 Xpaths wold I need to obtain each text I need to obtain?

Thank you very much in advance

解决方案

Use a wildcard between target/target2 and goal:

//body/target/*/goal

//body/target2/*/goal

这篇关于如何在两个目标节点之间形成不相关节点的Xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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