↑
Main Page
XSLT support in IE
</xsl:template>
</xsl:stylesheet>
As you can see, XSLT is actually another language based on XML. The document element is
<xsl:stylesheet/>
, which also specifies the version of XSLT being used (1.0) and the namespace URL.
Without this information, an XSLT processor can’t properly use the style sheet.
The next line contains the
<xsl:output/>
element, which specifies the rules by which the output
should be handled. For the
method
attribute, three possible values exist: html, xml, and text. When you
use
“html”
, the parser treats the output as HTML, meaning that the strict XML rules are not applied;
“xml”
forces all XML rules to be applied to the output, whereas
“text”
only outputs the content con-
tained outside of elements.
Next come the templates. The first template matches the document element, as indicated by
match=”/”.
;
the
/
XPath expression always refers to the document element. There is HTML code inside the template,
right up until the
<xsl:apply-templates/>
element, which tells the parser to apply any matching
templates to the child nodes (which is an XPath expression for any child node). Because a template is
defined that matches that pattern, processing continues.
Inside the second template, notice the
<li>
element. Immediately following is the
<xsl:value-of/>
element, which is used to output a value from the source XML. The
select
attribute is another XPath
expression,
“name”
, which tells the transformer to output the text value of the
<name/>
element (which
is the text contained inside of it). After that, there’s a comma, then the opening
<em>
tag, followed by
another
<xsl:value-of/>
element. This time, the
select
attribute points to the
title
attribute of
<employee/>
and so the transformer outputs that value.
When this XSLT style sheet is run against the XML file, the result is the HTML shown previously.
Although this is a simple example, it does show some of the unique capabilities of XSLT.
If you’d like to learn more about XSLT, consider picking up XSLT 2.0: Programmer’s Reference, 3rd
Edition (Wiley Publishing, Inc., ISBN 0-7645-6909-0).
XSLT support in IE
Beginning with MSXML 3.0, Internet Explorer fully supports XSLT 1.0. If you are still using Internet
Explorer 5.0 or 5.5, you should install a new version of MSXML manually; if you are using IE 6.0, then
you already have at least MSXML 3.0.
The simplest way to conduct an XSLT transformation is to load the source XML and the XSLT file each
into their own DOMs and then use the proprietary
transformNode()
method:
oXmlDom.load(“employees.xml”);
oXslDom.load(“employees.xslt”);
var sResult = oXmlDom.transformNode(oXslDom);
This example loads a DOM with XML and a DOM with the XSLT style sheet (note that you can load XSLT
into an XML DOM because it is just another form of XML). Then, the third line calls the
transformNode()
method on the document, passing in the DOM containing the XSLT code as its only argument. The vari-
able
sResult
is then filled with a string resulting from the transformation.
473
XML in JavaScript
18_579088 ch15.qxd 3/28/05 11:42 AM Page 473
Free JavaScript Editor
Ajax Editor
©
→