Java Reference
In-Depth Information
<xsl:template match= " / " > , the XPath from the root node to the ID node in
Listing 15-8 is " MemberInfo/ID ". On the other hand, if you use <xsl:template
match= " MemberInfo " > to match the document node, the XPath is simply " ID ".
The only other XSL used in this example is the xsl:value-of expression. This expression returns the
value of the node selected using the XPath expression defined in the select attribute. For example, to
get the vehicle's color, use the following xsl:value-of expression:
<xsl:value-of select="Color"/>
This returns the value "Red" from the corresponding node in the XML document of Listing 15-8 :
<Color>Red</Color>
In addition to selecting values from XML documents, XSLT allows you to use your XML data in
calculations or to create and manipulate strings. An example of string manipulation to create an image
URL is shown in the following code snippet:
<xsl:variable name="imageUrl"
select="string('http://192.168.0.2/servlet/BlobServlet?id=')"/>
<xsl:variable name="id" select="ID"/>
<img>
<xsl:attribute name="src">
<xsl:value-of select="concat($imageUrl,$id)"/>
</xsl:attribute>
</img>
This code snippet illustrates how you can define a String variable and concatenate the value of an XML
element to the string to create a URL. This URL is then set as the value of the src attribute of an HTML
img tag. The complete stylesheet is shown in Listing 15-9 . Note how the stylesheet freely combines
XSL and HTML tags as required.
Listing 15-9: XSL stylesheet
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html"/>
<xsl:preserve-space elements="*"/>
<xsl:template match="VehicleData">
<HTML>
<HEAD>
<TITLE>Detail Page</TITLE>
<BASEFONT FACE="Arial"/>
</HEAD>
<BODY>
<P/>
<TABLE BORDER="1" WIDTH="480" CELLPADDING="4">
<TR>
<TD ALIGN="CENTER" VALIGN="TOP">
<xsl:variable name="imageUrl"
Search WWH ::




Custom Search