Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Reference

<xsl:variable> Element

Specifies a value bound in an expression.

<xsl:variable
  name = QName
  select = Expression>
</xsl:variable>

Attributes

name
Required. The name of the variable.
select
The value of the attribute is an expression, and the value of the variable is the object that results from evaluating the expression. When this attribute is specified, the content of <xsl:variable> must be empty.

Element Information

Number of occurrences Unlimited
Parent elements xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:for-each, xsl:if, xsl:otherwise, xsl:param, xsl:processing-instruction, xsl:stylesheet xsl:template, xsl:variable, xsl:when, xsl:with-param, output elements
Child elements xsl:apply-templates, xsl:attribute, xsl:call-template, xsl:choose, xsl:comment, xsl:copy, xsl:copy-of, xsl:element, xsl:for-each, xsl:if, xsl:processing-instruction, xsl:text, xsl:value-of, xsl:variable, output elements

Remarks

The value of the variable may be an object of any type that can be returned by an expression. The <xsl:variable> element can specify the value of the variable in three alternative ways:

Example

This example shows how to define a variable based on the values of other variables.

Note   To test this example in Internet Explorer, you need to use a script. For more information, see Initiate XSLT in a Script.

XML File (books.xml)

Use the Sample XML File (books.xml). Below the line <?xml version='1.0'?>, add the following line:

<?xml-stylesheet type="text/xsl" href="sample.xsl"?>

XSLT File (sample.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>

<xsl:template match="/">
   <html>
   <body>
      <xsl:variable name="bookCount" select="count(//book)"/>
      <xsl:variable name="bookTotal" select="sum(//book/price)"/>
      <xsl:variable name="bookAverage" select="$bookTotal div $bookCount"/>

      <table border="1">
         <tr>
            <td>
               <b>Title</b>
            </td>
            <td>
               <b>Price</b>
            </td>
            <td>
               <b>Average</b>
            </td>
            <td>
               <b>Difference</b>
            </td>
         </tr>

         <xsl:for-each select="//book">
            <tr>
               <td>
                  <xsl:value-of select="title"/>
               </td>
               <td align="right">
                  <xsl:value-of select="price"/>
               </td>
               <td align="right">
                  <xsl:value-of select="format-number($bookAverage, '#.00')"/>
               </td>
               <td align="right">
                  <xsl:value-of select="format-number(price - $bookAverage, '#.00')"/>
               </td>
            </tr>
         </xsl:for-each>

      </table>
   </body>
   </html>
</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

See Also

<xsl:param> Element