XSLT Tutorial
XPath: Context, Location Paths
Before we go into detail on XSLT, lets first discover some basics on
how to retrieve information from an XML file, using XPath.
XPath is a language for finding the information in an XML document.
Using XPath, we can specify the locations of document structures or data
in an XML document, and then process the information using XSLT. In practice, it can be
difficult to determine where XSLT stops and where XPath starts, but they were developed
as two different standards in the W3C.
Context
When we are working with XSLT, the context for a
query is the node in the source XML document currently
being processed. So in the template xsl:template match="/", we are
in the context of the root of the XML
document. This isn't the PEOPLE element, it's the whole document,
above the PEOPLE element.
When we are in a xsl:for-each loop, the context is
whichever node we are currently looping through. In our PEOPLE example, it's the
PERSON element.
Understanding the context being processed by an XSL template is
important. If you're confused, it will help you to understand why your XSL file
isn't creating the output file you're expecting. When you're debugging XSL,
the first question you should ask is, "What context is being processed?".
Location Paths
The location path sets the context of the node that you're
trying to find. We've already seen an example of this, where we
are finding all PERSON elements in the PEOPLE element. The context is set
using the location path of the root (match="/").
To code a location path, you can use an abbreviated or non-abbreviated
syntax. You might need to check which one your parser supports. An example of the
non-abbreviated syntax is shown below. (From the May 2000 release, the
MSXML parser supports both.)
Using the example from Worksheet 1, suppose we insert a new level into the
XML document so that the first address is as follows:
<PEOPLE>
<anotherlevel>
<PERSON PERSONID="p1">
<NAME>Mark Wilson</NAME>
<ADDRESS>911 Somewhere Circle, Canberra, Australia</ADDRESS>
<TEL>(++612) 12345</TEL>
<FAX>(++612) 12345</FAX>
<EMAIL>markwilson@somewhere.com</EMAIL>
</PERSON>
</anotherlevel>
If we don't change the selection in the xsl:for-each
location path, the first address won't be found. The following
unabbreviated selection will work, however.
Unabbreviated:
<xsl:for-each select="child::PEOPLE/descendant::PERSON">
An equivalent way to make the selection using the
abbreviated syntax is as follows:
Abbreviated:
<xsl:for-each select="PEOPLE//PERSON">
We will be using the abbreviated format!!
Path Expressions:
A path expression is an expression used for selecting a node set by
following a path or steps. Although the complete set of path expressions includes a
much larger group of operators, here are some of the most useful ones:
| element |
Selects all element children of the context node.
Example: article
Select all the child nodes of the article element.
|
| / |
Select from the root node of the current document, of the node in context.
Example: /article
Selects the article element,
starting at the root of the document.
Example: x/article
Select all article element that are children of x.
|
| // |
Selects nodes in the document from the current
context that matches the selection no matter where they are.
When used with a context, this operator selects all descendant
nodes in the context, no matter how many levels deep they
are.
Example: //article
Select all article elements no matter where they are in the
document.
Example: x//article
Select all article nodes that are descendant of the x element,
no matter where they exist under the x element.
|
|