// equals descendant-or-self::node()/
Watch it! // is expensive since it is relative path and has to traverse all tree!
Case sensitive names of nodes, elements and attributes !
//Person is not equal //person
Everything inside [ ] is expression. When you need another expression inside [ ] sometimes you can write:
//person[address/city]
; in this case / is used inside [] instead of :
//person[address[city]]
Relative and absolute path
Example of relative path:
//person[1]
; this will return EVERY person that is the FIRST child of its parent ! In sample this will include "Oliver child person" and John Sullivan.
Another relative:
//*/*/*/*/name()
; every element that has 4 ancestors ( city -> address -> person -> persons -> root
String='city'
String='person'
String='city'
Absolute path:
/persons/person[1]
; or use :
(//person)[1]
; this will return only FIRST child of persons node!
Sample XML:
<?xml-stylesheet type="text/xml" href="persons.xsl"?>
<persons>
<person>
<id>1</id>
<age>20</age>
<first-name>
John
</first-name>
<last-name>
Sullivan
</last-name>
<salary>200.00</salary>
</person>
<person>
<id>2</id>
<age>25</age>
<first-name>
Mark
</first-name>
<last-name>
Johny
</last-name>
</person>
<person>
<address>
<city state="France">Paris</city>
</address>
<id>3</id>
<age>30</age>
<first-name>
Mark
</first-name>
<last-name>
Johny
</last-name>
</person>
<person>
<address>
<person>
Oliver child person
</person>
<city state="UK">London</city>
</address>
<id>4</id>
<age>40</age>
<first-name>
Oliver
</first-name>
<last-name>
Johny
</last-name>
<salary>300.00</salary>
</person>
<person>
<id>5</id>
<age>60</age>
<first-name>
Kris
</first-name>
<last-name>
Sullivan
</last-name>
<salary>300.00</salary>
</person>
</persons>
1. Get all persons:
//person
2. Get all persons text:
//person/string()
3. Get second person:
//person[2]
;returns: Mark, Johny, Id=2
4. Get fourth person:
//person[position() eq 4]
;returns: Oliver, Johny, Id=4
5. Get first two persons:
//person[position() lt 3]
6. Get persons that have element Salary:
//person[salary]
7. Get every person whose next person sibling in document order has salary that equals to preceding person.
//person[sallary = following-sibling::person/sallary]
8. Get persons whose age is between 20 and 60 and has position before filtering greater than 3 :
//person[age gt "20" and age lt "60" and position() gt 3]
9. Find persons whose salary is greater than salary of first person that has info about salary:
//person[salary gt //person[salary][1]/salary/text() ]
10. Look for persons that have City info. For them look their parent Person and person Id.
//person/address/city/../../id
11. Find cities whose attribute State is France:
//person/address/city[@state="France"]
Example of sequences introduced in XPath 2.0:
12. Iterate through all persons and output first-name value:
for $n in /persons/person return $n/first-name
13. Iterate all persons with value for salary. On each iteration calculate second sequence by multiplying current person id element value from from first sequence with 2.
for $n in /persons/person[salary], $id in $n/id * 2 return $id
14. Are there any person that has salary greater than $299.00 :
every $p in /persons/person/salary satisfies $p gt "299.00"
;returns true
15. Do all persons older than 18 have salary element ?
every $adult in /persons/person[age gt "30"] satisfies $adult[salary]
;returns true
16. Do at least one person that have city info have salary info:
some $personwcity in /persons/person/address/city satisfies $personwcity/../..[salary]
;returns true
17. Show last Person from Persons node:
/persons/person[last()]
18. Show first-name of persons whose Id is greater than 3. Observe use of dot as self reference.
/persons/person/id[. gt "3"]/../first-name
19. Show first and last person.
(//person)[1] union (//person)[last()]
20. Show only persons with city info and salary info
Version 1:
//person[address/city] intersect //person[salary]
Version 2:
//person[address/city and salary]
21. Show persons that DON'T have salary:
//person except //person[salary]
22. Show all persons ONLY if there are 2 or more persons with address info:
//person[count(/persons/person/address) ge 2]
No comments:
Post a Comment