XSLT apply templates in different order of xml reading.

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-05-21T17:21:52Z Indexed on 2010/05/21 17:30 UTC
Read the original article Hit count: 349

Filed under:
|

I am new to this, so please bear with me...

If we have the following xml fragment:

<docXML>
<PARRAFO orden='1' tipo='parrafo'>
    <dato>
      <etiqueta>Título</etiqueta>
      <tipo>TextBox</tipo>
      <valor>¿Cuándo solicitar el consejo genético?</valor>
      <longitud>1500</longitud>
      <comentario></comentario>
      <enlace></enlace>
      <target_enlace>I</target_enlace>
    </dato>
    <dato>
      <etiqueta>Texto</etiqueta>
      <tipo>Resumen</tipo>
      <valor>Resumen text</valor>
      <longitud>8000</longitud>
      <comentario></comentario>
      <enlace></enlace>
      <target_enlace></target_enlace>
    </dato>
    <dato>
      <etiqueta>Imagen</etiqueta>
      <tipo>TextBox</tipo>
      <valor>http://url/Imagenes/7D2BE6480CF4486CA288A75932606181.jpg</valor>
      <longitud>1500</longitud>
      <comentario></comentario>
      <enlace></enlace>
      <target_enlace>I</target_enlace>
    </dato>
  </PARRAFO>
  <PARRAFO orden='1' tipo='parrafo'>
    <dato>
      <etiqueta>Título</etiqueta>
      <tipo>TextBox</tipo>
      <valor>TextBox text</valor>
      <longitud>1500</longitud>
      <comentario></comentario>
      <enlace></enlace>
      <target_enlace>I</target_enlace>
    </dato>
    <dato>
      <etiqueta>Texto</etiqueta>
      <tipo>Resumen</tipo>
      <valor>Resumen text</valor>
      <longitud>8000</longitud>
      <comentario></comentario>
      <enlace></enlace>
      <target_enlace></target_enlace>
    </dato>
  </PARRAFO>
</docXML>

.. I am going to apply templates to each section depending on the value of the label "etiqueta" per node "dato" in "PARRAFO" by using the following XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xsl" exclude-result-prefixes="msxsl">

  <xsl:output method="html" encoding="iso-8859-1"/>

<xsl:template match="/">
    <xsl:variable name="xml-doc-parrafo" select="documentoXML/PARRAFO"/>
   <!-- PARRAFOS -->
    <xsl:choose>
      <xsl:when test="count($xml-doc-parrafo)>0">
        <div class="seccion_1">
          <xsl:for-each select="$xml-doc-parrafo">
            <xsl:choose>
              <xsl:when test="self::node()[@tipo = 'parrafo']">
                <div class="parrafo">
                  <xsl:for-each select="self::node()[@tipo = 'parrafo']/dato">
                    <xsl:variable name="dato" select="self::node()[@tipo = 'parrafo']/dato"/>
                    <xsl:variable name="nextdato" select="following::dato[1]/@etiqueta"/>

                    <xsl:choose>
                      <xsl:when test="etiqueta = 'Título'">
                        <xsl:call-template name="imprimeTituloParrafo">
                          <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
                          <xsl:with-param name="valor" select="valor"></xsl:with-param>
                          <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
                          <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
                          <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
                          <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
                        </xsl:call-template>
                      </xsl:when>
                      <xsl:when test="etiqueta = 'Subtitulo'">
                        <xsl:call-template name="imprimeSubtituloParrafo">
                          <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
                          <xsl:with-param name="valor" select="valor"></xsl:with-param>
                          <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
                          <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
                          <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
                          <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
                        </xsl:call-template>
                      </xsl:when>

                      <xsl:when test="etiqueta = 'Imagen'">
                        <xsl:call-template name="imprimeImagenParrafo">
                          <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
                          <xsl:with-param name="valor" select="valor"></xsl:with-param>
                          <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
                          <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
                          <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
                          <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
                        </xsl:call-template>
                      </xsl:when>

                      <xsl:when test="etiqueta = 'Pie Imagen'">
                        <xsl:call-template name="imprimePieImagenParrafo">
                          <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
                          <xsl:with-param name="valor" select="valor"></xsl:with-param>
                          <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
                          <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
                          <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
                          <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
                        </xsl:call-template>
                      </xsl:when>

                      <xsl:when test="etiqueta = 'Texto'">
                        <xsl:call-template name="imprimeTextoParrafo">
                          <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
                          <xsl:with-param name="valor" select="valor"></xsl:with-param>
                          <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
                          <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
                          <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
                          <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
                        </xsl:call-template>
                      </xsl:when>

                    <xsl:when test="etiqueta = 'Pie Parrafo'">
                      <xsl:call-template name="imprimePieParrafo">
                        <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
                        <xsl:with-param name="valor" select="valor"></xsl:with-param>
                        <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
                        <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
                        <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
                        <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
                      </xsl:call-template>
                    </xsl:when>

                    </xsl:choose>
                  </xsl:for-each>
                </div>
              </xsl:when>
            </xsl:choose>
          </xsl:for-each>
        </div>
      </xsl:when>
      <!-- si no hay resultados -->
      <xsl:otherwise>
        <br></br>
        <p style="text-align:center;">El documento no contiene datos.</p>
      </xsl:otherwise>
    </xsl:choose>
 </xsl:template>

  <xsl:template name="imprimeTituloParrafo">
    <xsl:param name="etiqueta"></xsl:param>
    <xsl:param name="valor"></xsl:param>
    <xsl:param name="longitud"></xsl:param>
    <xsl:param name="enlace"></xsl:param>
    <xsl:param name="target_enlace"></xsl:param>

    <h2 class="titulo">
      <xsl:choose>
          <xsl:when test="string-length($enlace) > 0">
            <xsl:call-template name="imprimeEnlace">
              <xsl:with-param name="valor" select="valor"></xsl:with-param>
              <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
              <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
              <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
              <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$valor"/>
        </xsl:otherwise>
      </xsl:choose>
    </h2>
  </xsl:template>

  <xsl:template name="imprimeSubtituloParrafo">
    <xsl:param name="etiqueta"></xsl:param>
    <xsl:param name="valor"></xsl:param>
    <xsl:param name="longitud"></xsl:param>
    <xsl:param name="enlace"></xsl:param>
    <xsl:param name="target_enlace"></xsl:param>

    <h3 class="subtitulo">
      <xsl:choose>
        <xsl:when test="string-length($enlace) > 0">
          <xsl:call-template name="imprimeEnlace">
            <xsl:with-param name="valor" select="valor"></xsl:with-param>
            <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
            <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
            <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
            <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$valor"/>
        </xsl:otherwise>
      </xsl:choose>
    </h3>
  </xsl:template>

  <xsl:template name="imprimeTextoParrafo">
    <xsl:param name="etiqueta"></xsl:param>
    <xsl:param name="valor"></xsl:param>
    <xsl:param name="longitud"></xsl:param>
    <xsl:param name="enlace"></xsl:param>
    <xsl:param name="target_enlace"></xsl:param>

    <div class="texto">
      <p class="texto">
        <xsl:copy-of select="$valor/node()"/>
      </p>
    </div>
  </xsl:template>

  <xsl:template name="imprimeImagenParrafo">
    <xsl:param name="etiqueta"></xsl:param>
    <xsl:param name="valor"></xsl:param>
    <xsl:param name="longitud"></xsl:param>
    <xsl:param name="comentario"></xsl:param>
    <xsl:param name="enlace"></xsl:param>
    <xsl:param name="target_enlace"></xsl:param>

    <xsl:choose>
      <xsl:when test="string-length($enlace) = 0">
        <xsl:call-template name="imprimeImagen">
          <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
          <xsl:with-param name="valor" select="valor"></xsl:with-param>
          <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
          <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
          <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
          <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
        </xsl:call-template>        
      </xsl:when>
      <xsl:otherwise>
        <a>
          <xsl:choose>
            <xsl:when test="$target_enlace/node() = 'E'">
              <xsl:attribute name="target">
                <xsl:text>_blank</xsl:text>
              </xsl:attribute>
            </xsl:when>
            <xsl:when test="$target_enlace/node() = 'I'">
              <xsl:attribute name="target">
                <xsl:text>_self</xsl:text>
              </xsl:attribute>
            </xsl:when>
          </xsl:choose>
          <xsl:attribute name="href">
            <xsl:value-of select="$enlace"/>
          </xsl:attribute>

          <xsl:call-template name="imprimeImagen">
            <xsl:with-param name="etiqueta" select="etiqueta"></xsl:with-param>
            <xsl:with-param name="valor" select="valor"></xsl:with-param>
            <xsl:with-param name="longitud" select="longitud"></xsl:with-param>
            <xsl:with-param name="comentario" select="comentario"></xsl:with-param>
            <xsl:with-param name="enlace" select="enlace"></xsl:with-param>
            <xsl:with-param name="target_enlace" select="target_enlace"></xsl:with-param>
          </xsl:call-template>

        </a>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="imprimeImagen">
    <xsl:param name="etiqueta"></xsl:param>
    <xsl:param name="valor"></xsl:param>
    <xsl:param name="longitud"></xsl:param>
    <xsl:param name="comentario"></xsl:param>
    <xsl:param name="enlace"></xsl:param>
    <xsl:param name="target_enlace"></xsl:param>
    <div class="imagen_pie">
      <img>
        <xsl:attribute name="src">
          <xsl:value-of select="$valor"/>
        </xsl:attribute>
        <xsl:attribute name="alt">
          <xsl:value-of select="$comentario"/>
        </xsl:attribute>
      </img>
    </div>
  </xsl:template>


  <xsl:template name="imprimeEnlace">
    <xsl:param name="valor"></xsl:param>
    <xsl:param name="longitud"></xsl:param>
    <xsl:param name="comentario"></xsl:param>
    <xsl:param name="enlace"></xsl:param>
    <xsl:param name="target_enlace"></xsl:param>
    <a>
      <xsl:choose>
        <xsl:when test="$target_enlace/node() = 'E'">
          <xsl:attribute name="target">
            <xsl:text>_blank</xsl:text>
          </xsl:attribute>
        </xsl:when>
        <xsl:when test="$target_enlace/node() = 'I'">
        </xsl:when>
        <xsl:when test="$target_enlace/node() = 'D'">
        </xsl:when>
      </xsl:choose>
      <xsl:attribute name="href">
        <xsl:value-of select="enlace"/>
      </xsl:attribute>
      <xsl:value-of select="$valor"/>
    </a>
  </xsl:template>

 .... 

</xsl:stylesheet>

I need to first apply the template image (if exists in this "PARRAFO") "Imagen" just before the text "Texto"

Now apply the template text first and then the image because it is before the text node before the image as shown in xml

Thanks a lot!

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xslt