XPath Eugenia Fernandez IUPUI
XML Path Language (XPath) a data model for representing an XML document as an abstract node tree a mechanism for addressing information found in the document node tree used to navigate to and address components of an XML document
Dealing with Abstraction All input and output documents in XSLT are manipulated in an abstract fashion The input XML document is transformed into an abstract source node tree The XSLT processor performs all operations on the source node tree, not the original document A new output node tree is created as the result of the transformation XPath is used to navigate the source node tree
An XML Tree Jake XML Document Document root ID 123Jake23000 Tree Structure
Types of Nodes 1. Root every XML document has a single root node, the only node without a parent, corresponds to the document, above the XML root element 2. Element 3. Attribute not children of their defining elements located along separate attribute axis IMPLIED attributes are not defined in the tree if attribute has a default value in the DTD it appears in the node tree
Type of Nodes 4. Text any sequence of consecutive characters, including whitespace 5. Namespace attached to all elements describes the namespace definitions 6. Processing instruction but not the declaration 7. Comment
Node Properties Each node has four properties value of the node name of the node parent node set of child nodes
Location Paths An XPath expression for locating nodes in an XML document absolute location paths defines location relative to root node begins with / (/ specifies the root) relative location paths defines location relative to current node does not begin with / current node changes at every step can specify axis (direction) to move in
Building Location Paths Made up of location steps relative path product/price absolute path/catalog/product/price Location steps evaluated left to right
Location Steps Each location step made up of three parts axis::node-test [predicate] axis – indicates the direction from current node, e.g. child axis selects child elements, the attribute axis selects attributes node-test specifies nodes by their type or their name returns a set of nodes or a node-set predicate Boolean expression that filters the node-set obtained by node-test enclosed in []
Node Tests by type along any axis processing-instruction() comment() text() node() by name the character * all elements
Location Step Example child::product[price>35][3] this is a relative path searches from current node through the child axis finds all child product elements the 1 st predicate filters the product elements to those who have a price element > 35 the 2 nd predicate keeps the third one only
XPath Axes self child parent attribute descendant descendant-or-self ancestor ancestor-or-self following following-sibling preceding preceding-sibling namespace For more information visit
XPath Axes self parent ancestor child descendent preceding-sibling following-sibling following preceding
Axes Examples select=“child::partNbr” child elements from the current node with the name partNbr select=“preceding::partNbr” all partNbr nodes that appear before the current node not including ancestors & attributes select=“../partNbr” sibling & self nodes with the name partNbr by first going to the parent (with..) and then to the parent’s children (with /)
Shortcuts No axis implies the child:: axis character = the attribute axis, i.e., attribute::. = self::node() or the current node.. = parent::node() the parent node // = /descendant-or-self::node()/ i.e., all descendents
Using Predicates By position [3] or [last()] numbered in proximity order to current node along the axis reverse axis ancestor, ancestor-or-self, preceding, preceding-sibling, numbered in reverse document order, or in reverse direction from current node forward axis all other axes, numbered in document order or forward from current node
Using Predicates By Presence child product element which has a code attribute By Value child product element with code attribute with value of “123”
Sources “Practical Transformation using XSLT and Xpath”, G. Ken Holman, presentation at XML One Conference, Chicago, IL, May, “Exchanging and Transforming Data Using XML and XSLT”, a Microsoft Certified Course.