Flylib.com

Books Software

 
 
 

Take a Walk on the Absolute Side

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

Take a Walk on the Absolute Side

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:

..\..\Lattes\espresso.txt

Or to use an absolute path, I would use:

C:\Lattes\espresso.txt

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
Chapter 5 - XPath Espresso
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

Putting It All Together

The following bulleted lists give you a sampling of the various relative and absolute location paths Ive talked about in this chapter.

Axis examples

  • chapter or child::chapter selects the chapter element children of the current node.

  • @ name or attribute::name selects all the name attributes of the current node.

  • .. or parent::node() selects the parent of the current node.

  • . or self::node() selects the current node, regardless of its type.

  • self::chapter selects the current node if it is a chapter element.

  • decendant::chapter selects all the chapter descendants of the current node.

  • descendant-or-self::page selects all the page element descendants of the current node and, if the current node is a page element, then include it as well.

  • ancestor ::page selects all the page element ancestors of the current node.

  • ancestor-or-self::page selects all the page element ancestors of the current node and, if the current node is a page element, then include it as well.

  • chapter/descendant::page selects the page descendants of the chapter element children of the current node.

Node test examples

  • * or child::* selects all the element children of the current node.

  • @* or attribute::* selects all the attributes of the current node.

  • text() or child::text() selects all the text node children of the current node.

  • node() or child::node() selects all the node children of the current node.

  • */page selects all the page grandchildren of the current node.

Predicate examples

  • chapter[1] or chapter[position()=1] selects the first chapter element child of the current node.

  • chapter[last()] selects the last chapter element child of the current node.

  • U following-sibling::chapter[1] selects the next chapter element sibling of the current node.

  • preceding -sibling::chapter[1] selects the previous chapter element sibling of the current node.

  • chapter[@level] selects all the chapter element children of the current node that have a level attribute defined.

  • chapter[not(@level)] selects all the chapter element children of the current node that dont have a level attribute defined.

  • chapter[@level='advanced'] selects all the chapter element children of the current node that have a level attribute with a value of advanced .

  • chapter[summary] selects all the chapter element children of the current node that have one or more summary element children.

  • chapter[2][@level] selects the second chapter element child of the current node if it has a level attribute defined.

  • chapter[@level][2] selects the second chapter element child of the current node that has a level attribute defined.

Absolute path examples

  • / selects the root node.

  • /descendant::chapter selects all the chapter elements in the same document as the current node.

  • /descendant::summary[12] selects the 12th summary element in the document.

  
 
 
2000-2002    Feedback