1
<root>
  <a>
    <b>
      <ba>
        <baa>RM</baa>
        <bab>*DB:ZA:4</bab>
      </ba>
    </b>
    <c>
      <ca>M</ca>
      <cb>1</cb>
    </c>
  </a>
  <a>
    <b>
      <ba>
        <baa>RM</baa>
        <bab>*DB:ZA:4</bab>
      </ba>
    </b>
    <c>
      <ca>S</ca>
      <cb>1</cb>
    </c>
  </a>
</root>

And the following xPath: //a[//bab/text() = '*DB:ZA:4' and //ca/text() = 'S']

This returns both 'a' nodes. But I only want the one with ca='S'

I can achieve that by using .//ca/text() = 'S'. But I don't know why. Can someone epxlain? Does the xpath loses the context where it is after the 'and' ?

I used this XPath-Tester: http://videlibri.sourceforge.net/cgi-bin/xidelcgi

GarfieldKlon
  • 11,170
  • 7
  • 31
  • 33

3 Answers3

2

Because locator like element[//anotherElement] - returns element, but ensures that somewhere anotherElement is present.

And locator element[.//anotherElement] - returns element element for which there's descendant anotherElement.

Adding dot, in that case, changes search from absolute to relative path.

Vitaliy Moskalyuk
  • 2,463
  • 13
  • 15
1

//ca/text() = 'S' starts from the root of the document, so it will always select a text node with the content 'S' regardless of which a element is the context.

Use .//ca/text() instead, as this searches within the context of the current node.

Tom W
  • 5,108
  • 4
  • 30
  • 52
1

// will return elements located anywhere in the document, starting from root. .// will search relative to current node. It may be misleading, because you might think that using it inside a[] is already relative. It's the same with absolute expressions / and ./.

AdamL
  • 12,421
  • 5
  • 50
  • 74