2 Replies Latest reply: Mar 2, 2014 7:05 PM by dvohra21 RSS

    XPath Question

    JP

      Hi,

       

      I have a question regarding XPath. Let's say I have the below XML, I want to get only the first leaf element that has a value of c. I tried to use /tree/branch/leaf[leaf = "c"] but its getting both. any help?

       

      <tree>

        <branch>

        <leaf>a</leaf>

        <leaf>c</leaf>

        <leaf>c</leaf>

        </branch>

      </tree>

        • 1. Re: XPath Question
          odie_63

          Hi,

          I tried to use /tree/branch/leaf[leaf = "c"] but its getting both. any help?

          I'd say it should have return none since the leaf element doesn't possess any child named "leaf".

          Which XPath engine are you using?

           

          Anyway, use a positional predicate :

          /tree/branch/leaf[.="c"][1]
          • 2. Re: XPath Question
            dvohra21

            XPath returns all nodes that match an expression if the expression does not select one of the nodes using predicates.

            Refer

            XPath Syntax