Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

XPath parent axis not behaving as expected

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading