Shintaro Takechi
Shintaro Takechi

Reputation: 1365

How to form Xpath with irrelevant nodes in between two target nodes

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

Upvotes: 0

Views: 147

Answers (1)

user142162
user142162

Reputation:

Use a wildcard between target/target2 and goal:

//body/target/*/goal
//body/target2/*/goal

Upvotes: 1

Related Questions