Xpath is a common way to access data in xml. Sets of elements or attributes data can be selected. Xpath looks similar to a path pointing to a file on a file system. Some gui programs as editix has good xpath support, where the gui lets you select the desired data and then you can copy xpath from it.
Table 11.2. xpath
/chapter/sect1[2] | The second <sect1> is selected that is a subtag of <chapter> |
/chapter/sect1[2]/title[1] | The first (and probably only) <title> of the second <sect1> is selected that is a subtag of <chapter> |
/chapter/sect1[2]/title[1]/text() | The actual data=text of the first (and probably only) <title> of the second <sect1> is selected that is a subtag of <chapter> |
This is quite straight forward. To access the text of an element use the / character
<parent element>
/<child element>
To access value of an attribute use the @ character
<parent element>
/<child element>
@<childs attribute>
Just use a child elements name and you select all of the children having this name. You can also
extend the child's name with the / character to address a subchild <child element>
/< sub-child element>
The * character can be used instead of a real element name
Instead of <parent element>/*/<sub-child element> the syntax <parent element>//< sub-child element> can be used
To select parent elements of a child element use <child element>/..
To have the or function use the ¦ character <parent element1>
¦ <parent element2>
To get the current element use the . Character
To select just elements fulfilling a certain criteria <parent element>
[<childelement>
< test>
<value>
]
and attribute <parent element>
[<childattribute>
<test>
<value>
]
Test can be < = ... . and value a number.
The / character selects the root of the document, therefore everything.