Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

am I right about what these XPath expressions mean?

Tags:

xpath

this is for school, if you know your XPath and wouldn't mind telling me if I'm correct:

1. //a[/b]/a

every 'a' that has a parent 'a' in a tree where the root is 'b'. (the location of the [/b] is irrelevant? i.e. is the above equivalent to //a/a[/b] ?

2. //*[//a]//a[/a][a]

breaking it down from left to right: //*[//a] means all elements having a descendant 'a', therefore //*[//a]//a (quite school-excerisely) means all 'a' elements. and //*[//a]//a[/a] means all 'a' elements in a tree where the root is 'a', and finally - //*[//a]//a[/a][a] means all 'a' elements in a tree where the root is 'a' that have a child 'a'.

Thanks for any help, can't seem to get a straight answer anywhere.

like image 896
bloodcell Avatar asked Nov 14 '22 08:11

bloodcell


1 Answers

What class is quizzing you on arcane XPath queries? Wow.

  1. //a[/b]/a
    Yes, you have that right. The location of [/b] is irrelevant.

  2. //*[//a]//a[/a][a]
    Technically //*[//a]//a is equivalent to //*//a which means all 'a' elements which have an ancestor element. So if the root element is 'a' it won't won't match. Aside from that, yes, your analysis is correct.

For what it's worth, a few years ago I implemented a full XPath parser from scratch in JavaScript for a project I worked on. So I really hope my answers are right!

like image 172
John Kugelman Avatar answered Jan 01 '23 11:01

John Kugelman