cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

XPath Question

SOLVED
Highlighted

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

Tags (2)
1 ACCEPTED SOLUTION

Accepted Solutions

Re: XPath Question

If the context node is <menuoption> then your XPath test "IF" condition is: ancestor::menucontainer/@op = @id

View solution in original post

6 REPLIES 6

Re: XPath Question

I would use a predicate to find the menuoption that matched the menucontainer attribute. Try something like...

menuoption[@id = parent::menucontainer/@op]

-Jeff

Re: XPath Question

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.

Re: XPath Question

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

Re: XPath Question

If the context node is <menuoption> then your XPath test "IF" condition is: ancestor::menucontainer/@op = @id

View solution in original post

Re: XPath Question

Thanks a lot It worked well

Re: XPath Question

Ah, missed the menubox element in there.