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

<xsl:stylesheet> Element

Specifies the document element of an XSLT file, which contains all other XSLT elements. This element is a synonym for the <xsl:transform> element.

<xsl:stylesheet
  id = id
  extension-element-prefixes = NCNames
  exclude-result-prefixes = NCNames
  version = number>
</xsl:stylesheet>

Attributes

id
A unique identifier to facilitate embedding XSLT files.
extension-element-prefixes
The namespace to be used as an extension namespace. The value is a white-space-separated list of namespace prefixes. The namespace bound to each of the prefixes is designated as an extension namespace. The default namespace (as declared by xmlns) can be designated as an extension namespace by including #default in the list of namespace prefixes. The designation of a namespace as an extension namespace is effective within the subtree of the style sheet rooted at the element bearing the extension-element-prefixes; a subtree rooted at an <xsl:stylesheet> element does not include any style sheets imported or included by children of that <xsl:stylesheet> element.
exclude-result-prefixes
The namespace Uniform Resource Identifier (URI) as an excluded namespace. The value is a white-space separated list of namespace prefixes. The namespace bound to each of the prefixes is designated as an excluded namespace. The default namespace (as declared by xmlns) may be designated as an excluded namespace by including #default in the list of namespace prefixes. The designation of a namespace as an excluded namespace is effective within the subtree of the style sheet rooted at the element bearing the exclude-result-prefixes; a subtree rooted at an <xsl:stylesheet> element does not include any style sheets imported or included by children of that <xsl:stylesheet> element.
version
Required. The version of XSLT that the XSLT file requires. Value should be set to "1.0" for this version of XSLT.

Element Information

Number of occurrences One
Parent elements (No parent elements)
Child elements xsl:attribute-set, xsl:import, xsl:include, xsl:output, xsl:param, xsl:template, xsl:variable, msxsl:script

Remarks

Every XSTL file must declare <xsl:stylesheet> as its document element.

A style sheet can use a series of template rule declarations (<xsl:template>) to prescribe explicit transformations on a data set. It can use <xsl:include> and/or <xsl:import> to incorporate other style sheets; this allows you to reuse existing, tested template rules. The style sheet can also support global variables (<xsl:param>) and named constants (<xsl:variable>), to be used throughout the transformations. The <xsl:param> element can be used to pass parameters into a style sheet. By using the extension element, <msxsl:script>, a style sheet can even allow custom functions to be implemented, although this practice is generally discouraged.

The following example shows an <xsl:stylesheet> element with several common namespaces.

An XSLT style sheet must declare at least the namespace for the XSL transformation. In MXSML versions 4.0 and later, this namespace must be declared as follows:

<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

If elements or data types from other namespaces are processed, the style sheet must declare the related namespaces as well. For example, the following XSLT style sheet declaration stipulates that additional XML vocabularies, which are defined for Microsoft extensions and XSL Formatting Objects (XSL-FO), are to be used.

<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">

Example

This example shows a complete XSLT file that contains a set of templates. The root template (match="/") defines the structure of the overall output document, and the other templates define the structure of the <name>, <address>, and <phone> elements. This HTML output file was produced by using msxml.exe at the command prompt.

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 (customers.xml)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="customers.xsl"?>
<customers>
   <customer>
      <name>John Smith</name>
      <address>123 Elm St.</address>
      <phone>(123) 456-7890</phone>
   </customer>
   <customer>
      <name>Mary Jones</name>
      <address>456 Oak Ave.</address>
      <phone>(156) 789-0123</phone>
   </customer>
</customers>

XSLT File (customers.xsl)

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

<xsl:template match="/">
  <HTML>
    <BODY>
      <TABLE>
        <xsl:for-each select="customers/customer">
          <TR>
            <xsl:apply-templates select="name" />
            <xsl:apply-templates select="address" />
            <xsl:apply-templates select="phone" />
          </TR>
        </xsl:for-each>
      </TABLE>
    </BODY>
  </HTML>
</xsl:template>

<xsl:template match="name">
  <TD STYLE="font-size:14pt font-family:serif">
    <xsl:apply-templates />
  </TD>
</xsl:template>

<xsl:template match="address">
  <TD> <xsl:apply-templates /> </TD>
</xsl:template>

<xsl:template match="phone">
  <TD> <xsl:apply-templates /> </TD>
</xsl:template>

<xsl:template match="text()">
  <xsl:value-of select='.'/>
</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

This is the processor output:

<HTML>
<BODY>
<TABLE>
<TR>
<TD STYLE="font-size:14pt font-family:serif">John Smith</TD>
<TD>123 Elm St.</TD>
<TD>(123) 456-7890</TD>
</TR>
<TR>
<TD STYLE="font-size:14pt font-family:serif">Mary Jones</TD>
<TD>456 Oak Ave.</TD>
<TD>(156) 789-0123</TD>
</TR>
</TABLE>
</BODY>
</HTML>

See Also

<xsl:transform> Element | Working with Namespaces and Prefix Aliases