Example of <xsl:call-template>

MSXML 5.0 SDK

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

Example of <xsl:call-template>

The following example uses <xsl:call-template> element to transform XSLT in a modular fashion. The example uses three main files:

  • The XML source file, topic.xml. This file represents a topic in a book publication.
  • The main XSLT file, topic.xsl. This file controls which information is displayed.
  • The called XSLT file, ui.xsl. This file determines how the information is rendered.
Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.

XML File (topic.xml)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="topic.xsl"?>
<topic name="My_topic"
       title="My Topic">
  <meta>
    <owner>
      <name>Jane</name>
      <email>[email protected]</email>
      <since></since>
    </owner>
    <history>
      <created-by>
        <name>John</name>
        <email>[email protected]</email>
        <date>Nov 5, 2001</date>
      </created-by>
      <modifiers>
      </modifiers>
    </history>
    <keyword></keyword>
    <refs></refs>
  </meta>

  <para name="para1" title="First Paragraph">
    The first para has both name and title.
  </para>
  <para title="Second Paragraph">
     the second para has a title but no name.
  </para>

  <para>
    Third para has neither name nor title.
  </para>
</topic>

Main XSLT File (topic.xsl)

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

  <xsl:import href="ui.xsl"/>
  <xsl:param name="editable" select="true"/>

  <xsl:template match="/topic">
    <xsl:if test="@title">
      <xsl:call-template name="topic_title">
         <xsl:with-param name="editable" select="$editable"/>
         <xsl:with-param name="value" select="@title"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

  <!-- Don't display meta information. -->
  <xsl:template match="meta"/>

  <xsl:template match="para">
    <P>
    <xsl:if test="@title">
      <xsl:call-template name="para_title">
         <xsl:with-param name="value" select="@title"/>
         <xsl:with-param name="editable" select="$editable"/>
      </xsl:call-template>
    </xsl:if>
    <xsl:apply-templates/>
    </P>
  </xsl:template>
  
  <xsl:template match="text()">
    <xsl:call-template name="text">
      <xsl:with-param name="value">
        <xsl:value-of select="."/>
      </xsl:with-param>
      <xsl:with-param name="editable">true</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

</xsl:stylesheet>

Component XSLT File (ui.xsl)

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

  <xsl:template name="topic_title">
    <xsl:param name="editable"/>
    <xsl:param name="value"/>
    <H2>
      <xsl:attribute name="CONTENTEDITABLE">
         <xsl:value-of select="$editable"/>
      </xsl:attribute>
      <xsl:value-of select="$value"/>
    </H2>
  </xsl:template>

  <xsl:template name="para_title">
    <xsl:param name="value"/>
    <xsl:param name="editable"/>
    <DIV STYLE="font-size:16;
                font-family:Arial;
                font-weight:bold;
                font-style:italic"
         CONTENTEDITABLE="{$editable}">
      <xsl:value-of select="$value"/>
    </DIV>
  </xsl:template>

  <xsl:template name="text">
    <xsl:param name="value"/>
    <xsl:param name="editable"/>
    <SPAN CONTENTEDITABLE="{$editable}">
      <xsl:value-of select="$value"/>
    </SPAN>
  </xsl:template>


</xsl:stylesheet>

Output

The following is the formatted output:

This is the processor output:

<H2 CONTENTEDITABLE="true">My Topic</H2>

<P>

<DIV STYLE="font-size:16;

font-family:Arial;

font-weight:bold;

font-style:italic"

CONTENTEDITABLE="true">First Paragraph<DIV>

<SPAN CONTENTEDITABLE="true">

The first para has both name and title.

</SPAN>

</P>

<P>

<DIV STYLE="font-size:16;

font-family:Arial;

font-weight:bold;

font-style:italic"

CONTENTEDITABLE="true">Second Paragraph<DIV>

<SPAN CONTENTEDITABLE="true">

The second para has a title but no name.

</SPAN>

</P>

<P>

<SPAN CONTENTEDITABLE="true">

The third para has neither name nor title.

</SPAN>

</P>

Remarks

The main XSLT file, topic.xsl, controls which information is displayed. It hides the content of <meta> element and controls the order of displayed items. It also calls template rules defined in the component XSLT file, ui.xsl.

The ui.xsl file contains only named template rules that can be called from the first XSLT file. Each named template rule acts like a regular function, taking two input parameters $value and $editable, and producing HTML output. The $value parameter passes the text to be displayed; $editable is used to determine whether the output text can be edited (when using Internet Explorer). However, unlike a regular function, the order of input parameters in the named template rule does not have to match the order specified in the calling template rule.

Notice that the template rules are independent of the nodes defined in the source XML document. The ui.xsl file is therefore an example of how you can write a general purpose UI library that can be invoked from any other XSLT files.

See Also

<xsl:template> Element | <xsl:with-param> Element

Other Resources Other Resources

XSLT Samples Viewer