Compartir a través de


Ejemplo de <xsl:call-template>

El siguiente ejemplo utiliza el elemento <xsl:call-template> para transformar XSLT de manera modular.El ejemplo utiliza tres archivos principales:

  • El archivo XML de origen, topic.xml.Este archivo representa un tema de un libro publicado.

  • El archivo XSLT principal, topic.xsl.Este archivo controla la información que se muestra.

  • El archivo XSLT llamado, ui.xsl.Este archivo determina cómo se va a presentar la información.

Archivo 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>

Archivo XSLT principal (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>

Archivo XSLT de componentes (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>

Resultados

A continuación se muestra el resultado con formato:

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.

Este es el resultado del procesador:

<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>

Comentarios

El archivo XSLT principal, topic.xsl, controla la información que se muestra.Oculta el contenido del elemento <meta> y controla el orden de los elementos mostrados.Además, invoca las reglas de plantillas definidas en el archivo XSLT de componentes, ui.xsl.

El archivo ui.xsl contiene solo las reglas de plantillas con nombre que pueden invocarse desde el primer archivo XSLT.Cada regla de plantilla con nombre actúa como una función normal: toma dos parámetros de entrada $value y $editable, y genera un resultado HTML.El parámetro $value para el texto que se va a mostrar; se utiliza $editable para determinar si puede editarse el texto resultante (al utilizar Internet Explorer).No obstante, a diferencia de una función normal, el orden de los parámetros de entrada de la regla de plantillas con nombre no coincide con el orden especificado en la regla de plantilla que llama.

Recuerde que las reglas de plantillas son independientes de los nodos que se definen en el documento XML de origen.El archivo ui.xsl es un ejemplo de cómo puede escribir una biblioteca de UI genérica que pueda ser invocada desde otros archivos XSLT.

Vea también

Referencia

Elemento <xsl:template>

Elemento <xsl:with-param>