XPath Question
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
XPath Question
Dear Users
I am looking for a solution to frame xpath logic to compare attributes.
<menucontainer id="menu1" op="o3" indicator="highlight">
<menubox>
<menutitle>TITLE</menutitle>
<menuoptionlist>
<menuoption id="o1">Select a path from below :</menuoption>
<menuoption id="o2">IMAGE1</menuoption>
<menuoption id="o3">IMAGE2</menuoption>
<menuoption id="o4">IMAGE3</menuoption>
<menuoption id="o5">IMAGE4</menuoption>
<menuoption id="o6">IMAGE5</menuoption>
</menuoptionlist>
</menubox>
</menucontainer>
Here I want to compare menucontainers op value (op="03") against the menuoption id's.So here the ideal match would be IMAGE2. And I would be highlighting the IMAGE2 in red colour.
I tried //menublock/menucontainer/@op= //menublock/menucontainer/menuoptionlist/menuoption/@id
but it is not dynamic.It checks first result and applies the same result everywhere.pls help in framing the xpath .
pls help
Regards,
Sriram Rammohan
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
If the context node is <menuoption> then your XPath test "IF" condition is: ancestor::menucontainer/@op = @id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I would use a predicate to find the menuoption that matched the menucontainer attribute. Try something like...
menuoption[@id = parent::menucontainer/@op]
-Jeff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Jeff's answer needs just one tweak for your example:
menuoption[@id = ancestor::menucontainer/@op]
I would also point out that using the // operator, as in your original example code, is a terrible idea and should be avoided wherever possible. You can read more online about the reasons why.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thanks Jeff & Gareth. I now understand the impact of the using // .
However I wanted to enforce a IF condition at "menuoptions" tag to check if the id = ancestors op (xpath check)
So if I use , current()[@id = ancestor:: menucontainer/@op] it seems not to work. pls advice
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
If the context node is <menuoption> then your XPath test "IF" condition is: ancestor::menucontainer/@op = @id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thanks a lot It worked well
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Ah, missed the menubox element in there.