Home > Software engineering >  XPath parent axis not behaving as expected
XPath parent axis not behaving as expected

Time:07-21

Given the following XML document:

<table>
<tr>   <td>A</td>         <td>B</td>         <td>C</td>     </tr>
<tr>   <td>D</td>       <td>E</td>    <td>E</td>      </tr>
<tr>   <td>F</td>    <td>G</td>         <td>H</td>      </tr>
</table>

I get different results for the following expressions and I don't understand what is the reason for that.

for: //td[parent::td] I get an empty set (namely, "No match").
for: //td[..] I get list of all the td elements.

I don't understand what is the difference between the expressions, because that .. is shortcut of parent::.

CodePudding user response:

Difference:

  • //td[parent::td] selects nothing because no td elements have td parents.

  • //td[..] selects all td elements because all td elements have a parent.

Both //td[parent::*] and //td[..] would select all td elements.

  • Related