Using <xsl:include> to Insert an XSLT File

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Using <xsl:include> to Insert an XSLT File

Many programming languages allow you to logically include the contents of one program (or portion of a program) within the scope of another, without doing so physically. For example, in the C programming language, you can use the #include directive for this purpose. Web sites developed using Microsoft FrontPage can take advantage of a similar feature, the Include Page component, which causes HTML code from one document to be embedded within another.

The XSLT counterpart of this feature is the <xsl:include> element. This is a top-level element within the style sheet—that is, it must be a child of the style sheet's root <xsl:stylesheet> element. The syntax is as follows:

<xsl:include href="uri" />

In this syntax, uri is the Uniform Resource Identifier (URI) of the style sheet to be embedded at that point.

The referenced style sheet must be a valid style sheet itself. That is, it must include a root <xsl:stylesheet> element. With the exception of the start and end tags, all the root element's content is logically inserted into the including style sheet. The effect is the same as if the template rules, named templates, variable declarations, and so on were physically included at that point.

Although <xsl:include> must be a top-level element, it can be placed any where among the style sheet's other top-level elements.

The examples in this topic work with jamming.xml and jamming.xsl, in Initial Example for Importing Style Sheets.

The markup_code.xsl file (shown later in this topic) is included into the main XSLT file, jamming.xsl with the help of <xsl:include> To incorporate the template rule for processing the <markup_code> elements in jamming.xml, the production department of the fiction group added an <xsl:include> element to jamming.xsl.

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

<xsl:include href="markup_code.xsl"/>

<xsl:template match="novel">
   [and so on]

This saves a few lines of code, and ensures that the <markup_code> elements in Synchronized Jamming are treated the same as those in a real computer reference text. A few more lines of code are saved by the fact that Scootney's "ScootMusic" line of books had already established a style sheet for <chord> elements, chord.xsl. This style sheet is shown later in this topic as an imported XSLT file.

A reference to chord.xsl was added to jamming.xsl, as follows:

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

<xsl:include href="markup_code.xsl"/>
<xsl:include href="chord.xsl"/>

<xsl:template match="novel">
...

As a result, the first chapter of Synchronized Jamming is displayed in a manner consistent with all three publishing lines, as shown in the formatted output at the end of this topic.

To simplify jamming.xsl, its designers could have included not only markup_code.xsl and chord.xsl, but the entire fiction.xsl style sheet from which jamming.xsl is otherwise derived—and then removed all of the specific template rules which jamming.xsl provides. The resulting style sheet would look as follows:

Alternative XSLT File (jamming_importonly.xsl)

Note   This file is not used in the full example.
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns="http://www.w3.org/TR/REC-html40">

<xsl:include href="markup_code.xsl"/>
<xsl:include href="chord.xsl"/>
<xsl:include href="fiction.xsl"/>

</xsl:stylesheet>

Example

This example builds on the example from Initial Example for Importing Style Sheets.

While designing the style sheet for Synchronized Jamming, the fiction branch of Scootney Publishing discovered that a style sheet, markup_code.xsl, had already been established in the computer-reference division for the <markup_code> element. They also learned that the computer-reference division had broken out all their separate styles into separate style sheet modules, such as chord.xsl. These modules could be included in other style sheets as needed.

XML File (jamming.xml)

Use jamming.xml, in Initial Example for Importing Style Sheets. Change the href attribute to reference jamming2.xsl.

Included XSLT File (markup_code.xsl)

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

<xsl:template match="markup_code">
   <div style="width:75%; margin-left:20; padding:10">
      <pre><xsl:value-of select="."/></pre>
   </div>
</xsl:template>

</xsl:stylesheet>

Included or Imported XSLT File (chord.xsl)

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

<xsl:template match="chord">
   <div style="width:75%; margin-left:20; padding:10">
      <table border="0">
         <tr>
            <th style="background-color: yellow">
               <xsl:attribute name="colspan"><xsl:value-of select="count(note)"/></xsl:attribute>
               Chord Notes
            </th>
         </tr>
         <tr>
            <xsl:for-each select="note">
               <td><b><xsl:value-of select="."/></b></td>
            </xsl:for-each>
         </tr>
         <tr>
            <td style="background-color: yellow">
               <xsl:attribute name="colspan"><xsl:value-of select="count(note)"/></xsl:attribute>
               Duration: 
                  <xsl:value-of select="@duration"/><xsl:text> </xsl:text>
                  <xsl:value-of select="@units"/>
            </td>
         </tr>
         <tr>
            <td style="background-color: yellow">
               <xsl:attribute name="colspan"><xsl:value-of select="count(note)"/></xsl:attribute>
               Voice: <xsl:value-of select="@voice"/>
            </td>
         </tr>
         <tr>
            <td style="background-color: yellow">
               <xsl:attribute name="colspan"><xsl:value-of select="count(note)"/></xsl:attribute>
               Vibrato: <xsl:value-of select="@vibrato"/>
            </td>
         </tr>
      </table>
   </div>
</xsl:template>

</xsl:stylesheet>

XSLT File (jamming2.xsl)

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

<xsl:include href="markup_code.xsl"/>
<xsl:include href="chord.xsl"/>

<xsl:template match="novel">
   <HTML>
      <HEAD>
         <TITLE>Excerpt:
            "<xsl:value-of select="title"/>,"
            by
            <xsl:value-of select="author"/>
         </TITLE>
         <STYLE>
            * {font-family: Arial,Helvetica,Univers,sans-serif}
            pre {font-family: Courier New,Courier,monospace}
         </STYLE>
      </HEAD>
      <BODY>
         <h2>Excerpt:<br/>
         <em><xsl:value-of select="title"/></em></h2>
         <h3>by <xsl:value-of select="author"/></h3>
         <h6>Copyright <xsl:value-of select="@copyright"/>
            by <xsl:value-of select="author"/></h6>
         <h6>Scootney Publishing / ISBN
            <xsl:value-of select="@isbn"/></h6>
         <hr size="2" width="50%" align="left"/>
         <xsl:apply-templates />
      </BODY>
   </HTML>
</xsl:template>

<xsl:template match="novel/title">
</xsl:template>
         
<xsl:template match="novel/author">
</xsl:template>
         
<xsl:template match="chapter">
   <div style="width:50%">
      <h2>Chapter <xsl:value-of select="@number"/>:<br/>
         <xsl:value-of select="title"/></h2>
      <xsl:apply-templates/>
   </div>
</xsl:template>
         
<xsl:template match="chapter/title">
</xsl:template>
         
<xsl:template match="epigraph">
   <div style="width:50%; float:right; margin:5; padding:5;
 background-color:silver; border-style:double; border-left-width:0;
 border-right-width:0">
      <xsl:apply-templates/>
   </div>
</xsl:template>
         
<xsl:template match="epigraph/text">
   <div>
      <em><value-of select="."/></em>
      <xsl:apply-templates/>
   </div>
</xsl:template>
         
<xsl:template match="epigraph/author">
   -- <xsl-value-of select="."/>
   <xsl:apply-templates/>
</xsl:template>
         
<xsl:template match="epigraph/source">
   (<em><xsl:value-of select="."/></em>)
</xsl:template>
         
<xsl:template match="para">
   <p><xsl:value-of select="."/></p>
</xsl:template>

</xsl:stylesheet>

Formatted Output