↑
Main Page
XSL processor
As you can see, the value passed in through JavaScript is correctly output to the HTML result. If you use
parameters in this way, you can make style sheets more extensible by incorporating different behaviors
based on parameters.
Another advanced feature of the XSL processor is the capability to set a mode of operation. In XSLT,
it’s possible to define a mode for a template. When a
mode
is defined, the template isn’t run unless
<xsl:apply-templates />
is specifically called with its
mode
attribute. For example:
<xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”>
<xsl:output method=”html” />
<xsl:param name=”message” />
<xsl:template match=”/”>
<html>
<head>
<title>Employees</title>
</head>
<body>
<ul>
<xsl:apply-templates select=”*” />
</ul>
<p>Message: <xsl:value-of select=”$message” /></p>
</body>
</html>
</xsl:template>
<xsl:template match=”employee”>
<li><xsl:value-of select=”name” />, <em><xsl:value-of select=”@title”
/></em></li>
</xsl:template>
<xsl:template match=”employee” mode=”position-first”>
<li><em><xsl:value-of select=”@title” /></em>, <xsl:value-of select=”name”
/></li>
</xsl:template>
</xsl:stylesheet>
This style sheet defines a template with its
mode
attribute set to
“position-first”
(note that you can
name a mode whatever you want; there are no predefined modes). Inside of this template, the employee’s
position is output first, and the employee name is output second. In order to use this template, the
<xsl:apply-templates/>
element must have its
mode
set
to “position-first”
as well. If you use
this style sheet, it has the same output as the previous one, displaying the employee name first and the
position second. If, however, you use this style sheet and set the mode to
“position-first”
using
JavaScript, it outputs the employee’s position first:
oProcessor.input = oXmlDom;
oProcessor.addParameter(“message”, “Hello World!”);
oProcessor.setStartMode(“position-first”);
oProcessor.transform();
476
Chapter 15
18_579088 ch15.qxd 3/28/05 11:42 AM Page 476
Free JavaScript Editor
Ajax Editor
©
→