Microsoft HomeproductssearchsupportshopWrite Us   Microsoft Home
Magazine
 |  Community
 |  Workshop
 |  Tools & Samples
 |  Training
 |  Site Info

Workshop  |  XML (Extensible Markup Language)

XSL Tutorial
Lesson 15: Using Script in XSL


November 4, 1998

What is an script in XSL?

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.

How can I use scripting within XSL?

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 it!

Try writing an XSL stylesheet for the following XML:

<items>
   <item>work boots</item>
   <item>shovel</item>
   <item>fertilizer</item>
   <item>hoe</item>
</items>
that produces the following HTML string:

<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!

Back to topBack to top

© 1998 Microsoft Corporation. All rights reserved. Terms of use.

 

Magazine Home
Ask Jane
DHTML Dude
Extreme XML
For Starters
More or Hess
Servin' It Up
Site Lights
Web Men Talking
Member Community Home
Benefits: Freebies & Discounts
Benefits: Promote Your Site
Benefits: Connect with Your Peers
Benefits at a Glance
Online Special-Interest Groups
Your Membership
SBN Stores
Join Now
Workshop Home
Essentials
Content & Component Delivery
Component Development
Data Access & Databases
Design
DHTML, HTML & CSS
Extensible Markup Language (XML)
Languages & Development Tools
Messaging & Collaboration
Networking, Protocols & Data Formats
Reusing Browser Technology
Security & Cryptography
Server Technologies
Streaming & Interactive Media
Web Content Management
Workshop Index
Tools & Samples Home
Tools
Samples, Headers, Libs
Images
Sounds
Style Sheets
Web Fonts
Training Home
SBN Live Seminars
SBN Live Chats
Courses
Peer Support
CD-ROM Training
Books & Training Kits
Certification
SBN Home
New to SBN?
What's New on SBN
Site Map
Site Search
Glossary
Write Us
About This Site