I'm playing around with xpath syntax and got stuck on one problem:
Given the following xml
<root xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
<employees>
<employee id="1">
<name>
Johnny Dapp
<br />
<br />
</name>
</employee>
<employee id="2"><name>Al Pacino</name></employee>
<employee id="3"><name>Robert De Niro</name></employee>
<employee id="4"><name>Kevin Spacey</name></employee>
<employee id="5"><name>Denzel Washington</name></employee>
<notanemployee id="14"><name>HELLO WORLD</name></notanemployee>
</employees>
<foo:companies>
<foo:company id="6">Tata Consultancy Services</foo:company>
<foo:company id="7">Wipro</foo:company>
<foo:company id="8">Infosys</foo:company>
<foo:company id="9">Microsoft</foo:company>
<foo:company id="10">IBM</foo:company>
<foo:company id="11">Apple</foo:company>
<foo:company id="12">Oracle</foo:company>
<bar:company id="13">Google</bar:company>
</foo:companies>
</root>
I first tried
//text()
which to my knowledge should select all the text nodes, and it does.
However I then tried
//text()[1]
expecting that it would select the first text node in the xml, which should be a text node consisting of a line break and then some white spaces (after the root opening tag and before the employees opening tag), but to my surprise it returned this
Text='
Johnny Dapp
'
Text='Al Pacino'
Text='Robert De Niro'
Text='Kevin Spacey'
Text='Denzel Washington'
Text='HELLO WORLD'
Text='
'
Text='Tata Consultancy Services'
Text='Wipro'
Text='Infosys'
Text='Microsoft'
Text='IBM'
Text='Apple'
Text='Oracle'
Text='Google'
How does that even make sense? How did it even return a set of text nodes to begin with?
I then tried
(//text())[1]
and it did return the first text node of the xml. How is this different from
//text()[1]
Btw I'm using two online xpath testers: