Finished Example for Importing and Including

MSXML 5.0 SDK

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

Finished Example for Importing and Including

Example

XML File (jamming.xml)

The following is a brief excerpt from the first chapter of Synchronized Jamming. The structure of the book is a mixture of common elements of fiction, literal HTML code, and musical annotation.

<?xml version="1.0"?>
<?xml:stylesheet type="text/xsl" href="jamming.xsl"?>
<novel copyright="2001" isbn="555000000X">
   <title>Synchronized Jamming</title>
   <author>Kari Hensien</author>
   <chapter number="1">
      <title>The Bleeding Edge</title>
      <epigraph><text>Any sufficiently advanced technology is 
indistinguishable from noise.</text><author>Canopy Rose</author><source>The Fusion Manifesto</source></epigraph>
      <para>Politics. Technology. Rock'n'roll. All you needed to know 
about the underground band known as Canopy Rose was right there in the 
first couple grafs of their so-called "Fusion Manifesto":</para>
      <markup_code><![CDATA[
<html>
<head><title>Fused</title></head>
<body>
<p>Listen. You know as well as we do...</p>
<p>... the sound of one hand jammin':</p>
]]></markup_code>
      <chord duration="4.5" units="seconds" voice="steel" vibrato="on">
         <note>A</note>
         <note>C</note>
      </chord>
      <markup_code><![CDATA[
<p>Welcome to the block party. Welcome to the revolution.</p>
</body>
</html>
]]></markup_code>
   </chapter>
</novel>

XSLT File (jamming.xsl)

This is the final version of the file.

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

<xsl:import href="chord.xsl"/>
<xsl:include href="markup_code.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="75%" 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:75%">
      <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:apply-imports/></p>
</xsl:template>

<xsl:template match="chord">
   <div style="border-style:double; width:50%">
      <xsl:apply-imports/>
   </div>
</xsl:template>

</xsl:stylesheet>

Imported XSLT File (chord.xsl)

<?xml version="1.0"?>
<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>

Included XSLT File (markup_code.xsl)

<?xml version="1.0"?>
<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>

Formatted Output