Finding Element Contents

   



A Leading Slash ('/') Is Significant

Listing A.4 displays the contents of the XSL stylesheet titleAndAuthor2.xsl that shows you another way to extract the title and author of the book in the file book.xml.

Listing A.4 titleAndAuthor2.xsl

start example
<?xml version="1.0"?> <xsl:stylesheet      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"      version="1.0"> <xsl:template match="/">   <xsl:apply-templates/> </xsl:template> <xsl:template match="book/title">   <xsl:value-of select="."/> by <!-- make sure you include the leading "/" -->   <xsl:value-of select="/book/author"/> </xsl:template> <xsl:template match="text()"> </xsl:template> </xsl:stylesheet>
end example

Remarks

One crucial point about extracting data is the effect of including or excluding a leading '/' in your XSL code. Since both cases are legal, you won't see an error message; instead, you simply won't get the data that you expect. The distinction between the two is simple: including a '/' means an absolute path whereas its omission means a relative path. In Listing A.4 the highlighted '/' is necessary because you are in a template by virtue of having matched the path 'book/title.' You need to specify '/book/author' in order to extract the author of the book because the author element is a child of the root node; it is not a child of a node that matches the current template.

On the other hand, the following two lines achieve the same effect in the current XSL stylesheet:

<xsl:template match="book/title"> <xsl:template match="/book/title">

The first line specifies a relative path 'book/title' in a template that can only be reached after the match that occurred with the template that matches the root element. The full path is the concatenation of '/' and 'book/title,' or '/book/title,' which is the same as the full path specified in the second line. In other words, a relative path from the root node is the same as specifying the full path itself, which is why the preceding two lines have the same effect in this XSL stylesheet.



   



Fundamentals of SVG Programming. Concepts to Source Code
Fundamentals of SVG Programming: Concepts to Source Code (Graphics Series)
ISBN: 1584502983
EAN: 2147483647
Year: 2003
Pages: 362

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