Esempio di <xsl:call-template>
Nell'esempio seguente viene utilizzato l'elemento <xsl:call-template> per trasformare XSLT in modo modulare. Nell'esempio vengono utilizzati tre file principali:
Il file di origine XML, topic.xml. Questo file rappresenta un argomento in una pubblicazione editoriale.
Il file principale XSLT, topic.xml. Questo file controlla quali informazioni vengono visualizzate.
Il file XSLT chiamato, ui.xsl. Questo file determina il rendering delle informazioni.
File XML (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>jane@topicfactory.com</email>
<since></since>
</owner>
<history>
<created-by>
<name>John</name>
<email>john@topicfactory.com</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>
File XSLT principale (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>
File XSLT componente (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
L'output formattato è il seguente:
My Topic
First Paragraph
The first para has both name and title.
Second Paragraph
the second para has a title but no name.
Third para has neither name nor title.
L'output del processore è il seguente:
<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>
Note
Il file principale XSLT, topic.xml, controlla quali informazioni vengono visualizzate. Nasconde il contenuto dell'elemento <meta> e controlla l'ordine degli elementi visualizzati. Inoltre chiama le regole del modello definite nel file XSLT componente, ui.xsl.
Il file ui.xsl contiene solo regole del modello denominate che possono essere chiamate dal primo file XSLT. Ogni regola di modello denominata agisce come una normale funzione, accettando due parametri di input $value e $editable e generando un output HTML. Il parametro $value passa il testo da visualizzare, mentre il parametro $editable viene utilizzato per determinare se è possibile modificare il testo di output (quando si utilizza Internet Explorer). Tuttavia, a differenza di una normale funzione, non è necessario che l'ordine dei parametri di input nella regola di modello denominata corrisponda all'ordine specificato nella regola di modello chiamante.
Notare che le regole del modello sono indipendenti dai nodi definiti nel documento XML di origine. Pertanto, il file ui.xsl è un esempio di come è possibile scrivere una libreria UI generica che può essere richiamata da qualsiasi file XSLT.