Understanding XPath

Monday, March 3, 2008

Though you do not have to be familiar with XPath to use the XSLT Mapper, you may benefit from some knowledge of XPath when building advanced expressions in XSLT. On the Web, you will find numerous publications dedicated to XSLT and related topics. Most books on XSLT contain sections on XPath. For a complete description of XPath and how to use it, read the W3C XML Path Language specification. This document is available at http://www.w3.org.

While XPath and XSLT are defined as separate specifications by the World Wide Web Consortium (W3C), XPath functions as a sub-language within an XSLT style sheet. The XPath specification provides a concise and intuitive way to locate and process items in an XML document based on a path through the document’s hierarchy of nodes. XPath uses a compact syntax for use within Uniform Resource Identifiers (URIs) and XML attribute values.

XPath models an XML document as a tree of nodes. XPath uses expressions to refer to different types of nodes—element nodes, attribute nodes, and text nodes—and XPath defines a way to compute a value for each type of node.

XPath expressions contain all the information to address a particular location in an XML schema. XPath expressions can contain location paths, function calls, variable references, unions of sets, comparisons, mathematical operations, and so on, all of which can be used together or independently to return a node set, a node value, or a mathematical or functional manipulation of that node value from an XML document. Additionally, an XPath expression can be evaluated to return three other object types: Boolean results, floating point numbers, and strings.
XPath Syntax

The purpose of an XPath expression is to identify one or more source nodes or to define how result nodes will be populated or modified. To address nodes in an XML document, XPath uses a syntax that is similar to an informal set of directions for finding a particular geographic location. For example, in XPath syntax, instructions on how to get to a particular beach in a park might look like this:

/Canada/BC/Vancouver/StanleyPark/ThirdBeach

Though its syntax is analogous to a file or directory structure, a key difference between XPath and earlier similar languages is that XPath specifies a route, rather than pointing to a specific set or sequence of characters, words, or other elements.

Similarly, if you wanted to locate the price of a particular line item in a purchase order, your XPath location path might look like this:

/PurchaseOrder/item[name=’Leaf blower’]/price

A basic XPath expression can consist of one or more location paths. An XPath location path is essentially a description of how to get from the current location to another one. It is made up of a series of location steps separated by a delimiting character. A location step can be composed of an axis, a node test, and a predicate, which are expressed as follows:

axis_name/node_test[predicate]

The axis specifies a route through the schema tree starting from a particular context node. The route is defined in terms of a list of nodes reached from the context node. The node test then selects a set of nodes using the selected axis. Expressions use predicates to further filter the set of nodes selected. For example, the following expression would return the Price child of the second occurrence of PurchaseOrder/Item within an XML document:

PurchaseOrder/Item[2]/Price

If a predicate is used to filter a node in an XPath expression, the node to which the predicate applies is known as the context node. You can access the context node directly using the expression “.”, which is the value of the select attribute within a <xsl:value-of> statement.

In all of the above examples, the XPath expressions are relative to the starting location. In XPath, the current node is the starting point where the location addressing begins at runtime. A node becomes the current node when it is processed using an <xsl:apply-templates> or <xsl:for-each> statement. You can access the current node directly using the current() function. If within an XPath expression, a predicate is applied to the current node, then the current node is also the context node and you can access it directly using the expression “.”.

In addition to the context node, current node, and location paths, XPath applies two other key concepts: the hierarchical tree structure in XML documents and logical relationships, such as ancestor, attribute, child, parent, and self, which are defined in the XML Information Set specification from the W3C.

source : http://developer.capeclear.com/7_beta/help/index.jsp?topic=/com.capeclear.eclipse.studiohelp/studio/wstransform-introductiontoxpathandxslt.html



 
Indelv.com is for sale!
 
ERP systemen
Alle ERP-systemen op een rij, compleet met ERP-nieuws en ERP-software informatie.
www.ERPcentraal.nl
ERP systemen
Alle ERP-systemen op een rij.
www.erpmatrix.nl


Quick Links
Our Friends
Cool Places
Visit also
About Us