XPath Lecture 36 Robb T. Koether Hampden-Sydney College Wed, Apr 16, 2014 Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 1 / 28
1 XPath 2 Executing XPath Expressions 3 XPath Expressions 4 Predicates 5 Axes 6 Assignment Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 2 / 28
Outline 1 XPath 2 Executing XPath Expressions 3 XPath Expressions 4 Predicates 5 Axes 6 Assignment Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 3 / 28
XPath XPath is a method of writing expressions that will navigate an XML document to extract the desired information. XPath expressions describe paths through the XML tree. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 4 / 28
XPath Nodes XPath expressions search for matching nodes. There are several types of node in an XML document. Element nodes Attribute nodes Text nodes Document node Comment nodes Instruction nodes Namespace nodes Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 5 / 28
Outline 1 XPath 2 Executing XPath Expressions 3 XPath Expressions 4 Predicates 5 Axes 6 Assignment Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 6 / 28
Executing XPath Expressions When an XPath expression is executed, the result is a list of all values that match the expression. The list could be a long string of text or it could a sequence of elements, depending on the expression. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 7 / 28
Executing XPath Expressions One way to run XPath is to download Saxon-HE from the Saxon website: http://saxon.sourceforge.net/ It includes the executable Query.exe. Then run Query.exe from the command line: >Query query-file The query-file will open the XML file and process it. The output will appear in the command window, or it can be redirected to a file. It is also possible to have the XML file invoke the XPath expression on itself in a browser. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 8 / 28
Outline 1 XPath 2 Executing XPath Expressions 3 XPath Expressions 4 Predicates 5 Axes 6 Assignment Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 9 / 28
XPath Expressions The Root Node doc("library.xml")/library XPath expressions begin with doc("file-name"); If the root node is named library, then the above expression matches the entire XML document, beginning at the root element library. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 10 / 28
XPath Expressions Navigating Down the Tree doc("library.xml")/library/books/book We may move any number of levels down the tree by naming the elements. The value of the expression is a list of all elements (including their contents) that match the expression. The above expression matches all book elements that are children of books elements that are children of the library element. Try out various paths with the library database. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 11 / 28
XPath Expressions The Double Slash doc("library.xml")//author The double slash // means any number of levels down. The above expression matches author elements at any level in the document. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 12 / 28
XPath Expressions The Wildcard doc("library.xml")//author/* The asterisk * is a wildcard that means any child. The above expression matches all child elements of author elements. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 13 / 28
XPath Expressions The Or Operator doc("library.xml")//(fname lname) The vertical stroke means or. We may use it to match more than one type of element. The above expression matches fname and lname elements. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 14 / 28
XPath Expressions The Current Node doc("library.xml")//author/. doc("library.xml")//author The dot. means the current node. The two expressions above have the same value. The current node is called the context node. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 15 / 28
XPath Expressions The Current Node doc("library.xml")//author/lname doc("library.xml")//author/./lname These two expressions also have the same value. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 16 / 28
XPath Expressions The Parent Node doc("library.xml")//author/../title Two dots.. means the parent node. The above expression will match the title elements of every book, collection, or volume with an author element. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 17 / 28
Outline 1 XPath 2 Executing XPath Expressions 3 XPath Expressions 4 Predicates 5 Axes 6 Assignment Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 18 / 28
XPath Predicates Predicates... node[predicate] A predicate is a statement that may be true or false. In an XPath expression, we may include predicates at any level. The predicate is written in square brackets. The predicate is applied to the children of the context node. The XPath expression will match elements in the expression for which the predicate is true. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 19 / 28
XPath Predicates The Parent Node doc("library.xml")//book[genre] doc("library.xml")//book[pages < 400]/title The first expression will match all book elements of books that have a genre. The second expression will match the title elements of books with less than 400 pages. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 20 / 28
XPath Predicates Write XPath expressions that will match the following. The first and last names of all authors. All last names of the authors of books that have a genre. All books published before 1930. All titles of volumes in the Fiction genre. All titles of books, volumes, or collections in the Fiction genre. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 21 / 28
Outline 1 XPath 2 Executing XPath Expressions 3 XPath Expressions 4 Predicates 5 Axes 6 Assignment Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 22 / 28
XPath Axes An axis is an expression that helps navigate through the XML tree. Examples are. and.. The following axes are available. self child parent ancestor descendant preceding-sibling following-sibling The axis is always relative to the context node. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 23 / 28
XPath Axes XPath Axes axis-name node-name predicates The axis name is followed by :: The default axis is child:: Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 24 / 28
XPath Axes The parent Axis doc("library.xml")//volumes/parent::collection [genre="fiction"]//lname The above expression will match the last names (of authors) of volumes in collections of the Fiction genre. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 25 / 28
XPath Axes The parent Axis doc("library.xml")//lname/ancestor::books doc("library.xml")//lname[ancestor::books] doc("library.xml")//title/..[descendant::mname] doc("library.xml")//book[title="the Deerslayer"] /preceding-sibling::book/title What will these expressions produce? Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 26 / 28
Outline 1 XPath 2 Executing XPath Expressions 3 XPath Expressions 4 Predicates 5 Axes 6 Assignment Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 27 / 28
Assignment Assignment Visit the W3Schools websites http://www.w3schools.com/xpath/ Visit the sections labeled XPath HOME through XPath Summary. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, 2014 28 / 28