Magazine |
| | Community |
| | Workshop |
| | Tools & Samples |
| | Training |
| | Site Info |
|
|
||||||||
|
November 4, 1998
Script can be used to augment the core functionality of XSL. This allows you to reformat or generate text, or calculate and insert values. In general, the use of script should be avoided where possible, since even a moderate use of script has a performance cost. Any active scripting language can be used, such as JScript and VBScript.
It is important to emphasize that scripting within XSL occurs at transformation time and not when the output is rendered by the browser. XSL scripts are entirely separate from DHTML scripts, and although the XSL template can include SCRIPT tags, these are treated as text by the XSL processor. There is no communication possible between the DHTML scripts and XSL scripts.
Script within XSL provides read-only access to the XML tree and a library of built-in functions for common tasks. For security reasons, ActiveX components cannot be instantiated.
Script calls are made with the xsl:eval element. The contents of this element are executed and the result inserted as text into the output tree. In the following example, the xsl:eval determines the index of the chapter, formats it in Roman numerals, and inserts it as text.
<xsl:for-each select="chapter"> <H2> Chapter <xsl:eval>formatIndex(childNumber(this), "I")</xsl:eval>: <xsl:value-of select="title"/> </H2> <xsl:apply-templates /> </xsl:for-each>
The formatNumber and childNumber functions are part of the XSL function library. For a complete list of functions, see the XSL Methods reference.
The "this" object is available within the xsl:eval element and represents the current node XSL is processing. In the above example, it is a "chapter" element.
The xsl:eval element contains a single line of script. If a multi-line function is needed, it can be defined in an xsl:script block. This is functionally equivalent to the HTML <SCRIPT> element.
The following example involves an xsl:script element in which the function chartBarWidth is defined. Notice that the xsl:attribute element is used to create a STYLE attribute with a calculated value.
<xsl:template match="chart-bar"> <xsl:script><![CDATA[ function chartBarWidth(e) { // extract the contents of the "percent" child of the element, // and format it as a percentage val = this.selectSingleNode("value"); total = this.selectSingleNode("ancestor(chart)/total"); return formatNumber(val / total, "#%"); }]]> ]]<![CDATA[></xsl:script> <DIV> <xsl:attribute name="STYLE">width: <xsl:eval>chartBarWidth(this)</xsl:eval></xsl:attribute> <xsl:apply-templates /> </DIV> </xsl:template>
Note that the contents of the script block are marked as character data. This allows characters such as < and & to appear in the script block and ensures that the white space is retained. If white space is collapsed, the JScript comments "//", which rely on the line break to terminate, will not work correctly. Although there are some scripts that will work correctly without enclosing them in CDATA, you should mark all scripts that you write in XSL as CDATA.
These examples show JScript used as the scripting language. This is the default language, but can be changed by placing the "language" attribute on the xsl:eval or xsl:script elements. It can also be changed globally by placing it on an xsl:stylesheet or xsl:template element.
Try writing an XSL stylesheet for the following XML:
<items>that produces the following HTML string:
<item>work boots</item>
<item>shovel</item>
<item>fertilizer</item>
<item>hoe</item>
</items>
<DIV id=items>
<DIV id=item1>work boots</DIV>
<DIV id=item2>shovel</DIV>
<DIV id=item3>fertilizer</DIV>
<DIV id=item4>hoe</DIV>
</DIV>
Did you find this article useful? Gripes? Compliments? Suggestions for other articles? Write us!
© 1998 Microsoft Corporation. All rights reserved. Terms of use.