Take a Walk on the Absolute Side

xslt for dummies
Chapter 5 - XPath Espresso
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002

If you work with directories and files on your computer, youre probably familiar with the concept of relative and absolute paths. For example, suppose I am in the c:\Windows\System32 directory and want to open up a file named espresso.txt in c:\Lattes . If I used a relative path , I would type the following to get to the file:


Or to use an absolute path, I would use:


Like directory/file structures, location paths can either be relative or absolute. A relative location path is defined by the axis relation to the current node. Each of the preceding examples in this chapter is relative. In contrast, an absolute location path starts at the root node and then has specified steps to descend the tree to the desired axis. A / character, meaning "start at the root," is always placed at the start of an absolute location path. For example, to get the content of the introduction element from Listing 5-1, I can use the following absolute path:

 <xsl:template match="/book/introduction"> <xsl:apply-templates/> </xsl:template> 

The match pattern starts with / to denote an absolute path and then looks for a book element node just under the root node with an introduction element node as its child. If found, the node set is applied using xsl:apply-templates .

An absolute path is also used when you want to specifically work with the root node. To demonstrate , suppose I want to surround all the contents of my xsltfordummies-toc.xml document (including the document element) with a new element called dummies . The following template rule does the trick:

 <xsl:template match="/"> <dummies genre="technology"> <xsl:copy-of select="."/> </dummies> </xsl:template> 

The match pattern of / selects the root node for the template and adds literal text before and after the result of the xsl:copy-of instruction.

 Tip   Notice that some of the XPath syntax looks similar to traditional file system syntax -- . , .. , / , and // ? That is more than a coincidence because the drafters of the XPath specification had directory/file syntax in mind when they defined the language.

2000-2002    Feedback

XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net