Skip to main content
1-Visitor
September 15, 2016
Solved

XPath Question

  • September 15, 2016
  • 1 reply
  • 3627 views

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

    Best answer by GarethOakes

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

    1 reply

    1-Visitor
    September 15, 2016

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

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

    -Jeff

    16-Pearl
    September 15, 2016

    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.

    1-Visitor
    September 16, 2016

    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