xPath Made Simple : Tutorial for xPath Expression Languation
xPath is a language used for searching and exploring parts of an XML documents using Path Expressions which is similar the the file system.
Given the following Examples of XML, we will illustrate the different syntax of xPath afterwards:
Feature Extraction Mark Nixon Java, A Beginner's Guide Herbert Schildt No Angel Dido Pop Old Yellow Moon Emmylou Harris Country
Selecting XML Elements
1 /library/book/title : Select all title elements which are —UNDER—>book—UNDER—>library
2 //title : Select all title elements at any level or place within the document
3 //author | //artist : Select all author elements AND all artist elements
4 /library/book/child::* : Select all children elements UNDER element /library/book
[info]Other keywords for xPath that can be used to access the tree of elements:ancestor,attribute [/info]
Searching Attributes
1 /library/book/title/@language : Select the attribute language under the elements /library/book/title
2 /library/book/title/attribute::language : Select the attribute language under the elements /library/book/title . This is the same as above.
3 /library/book/title/@* : Select ALL attributes under the elements /library/book/title.
Conditions
[info]In short, we place conditions for xPath expression within [ condition ] [/info]
[info]in xPath : .. ( dot and dot ) means go UP one level.[/info]
1 /library/book/title[../author=’Mark Nixon’] : List all elements of : /library/book/title. The condition is that the author element which is found one level up from the current node (title), should be equals to “Mark Nixon”.
2 /library/book[@id<4]/title[../author='Mark Nixon'] There are two conditions here. The first checks if the attribute named id is less than 4.
Operators | Explanation |
!= | Not Equal |
= | Equal |
+ , * , – | Plus, Times, Minus for Math |
div | divide ( Divison ) for Math |
Mod | Modulus ( Reminder of division |
or | Logical OR |
and | Logical AND |
xPath Functions
1 text( ) : is used the access the text content of an element.
[info]text( ) is the most important function in xPath[/info]
2 position( ) : returns a number representing the position of this node in the sequence of nodes. In the example, we list titles for the second book.
3 count(ABC) : returns the number of elements named : ABC under the current node.In the example, we list element titles where the parent has only three sub-elements.
4 starts-with(s1,s2) : returns true if s1 starts with s2. In the example, we list element artists whose name starts with “D”
5 contains(s1,s2) : returns true if s1 contains s2. In the example, we list element whose titles contains “A”
Other functions include : string-length(ABC), substring(string, start, length?)