Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I "reuse" code in XSL

Tags:

xslt

I'm using XSLT node. My question is about XSL. I have searched on Internet, but I've only found info about XML processing for show it on a web page. Info I'm searching is how "reuse" some code in this scenario:

My XSL is:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:wsb="http://tempuri.org/">

    <xsl:import href="HubGenerico.xsl" />

    <xsl:template match="/RCECSTD2">
        <xsl:element name="soap:Envelope">
            <xsl:element name="soap:Header" />
            <xsl:element name="soap:Body" />

            <xsl:choose>

                <xsl:when test="STDR2_DATOS_CONTROL/STDR2_CODOPE = 1010">
                    <xsl:element name="wsb:ConsultarSC">
                        <xsl:element name="wsb:codigoOperacion">
                            <xsl:value-of select="STDR2_DATOS_CONTROL/STDR2_CODOPE" />
                        </xsl:element>
                        <xsl:element name="wsb:numeroOperacion">
                            <xsl:value-of select="STDR2_DATOS_CONTROL/STDR2_NUMOPEBCO" />
                        </xsl:element>
                        <xsl:element name="wsb:codigoBanco">
                            <xsl:value-of select="STDR2_DATOS_EMPRESA/STDR2_CODBCO" />
                        </xsl:element>
                        <xsl:element name="wsb:codigoConvenio">
                            <xsl:value-of select="STDR2_DATOS_EMPRESA/STDR2_CODCONTR" />
                        </xsl:element>
                        <xsl:element name="wsb:numeroReferenciaDeuda">
                            <xsl:value-of
                                select="normalize-space(STDR2_DATOS_TRANSACCION/STDR2_NUMREF_DEUDA)" />
                        </xsl:element>
                        <xsl:element name="wsb:canalOperacion">
                            <xsl:value-of select="STDR2_DATOS_OPERACION/STDR2_CANAL" />
                        </xsl:element>
                        <xsl:element name="wsb:codigoOficina">
                            <xsl:value-of select="STDR2_DATOS_OPERACION/STDR2_CODOFI" />
                        </xsl:element>
                        <xsl:element name="wsb:fechaOperacion">
                            <xsl:value-of select="STDR2_DATOS_OPERACION/STDR2_FECOPE" />
                        </xsl:element>
                        <xsl:element name="wsb:horaOperacion">
                            <xsl:value-of select="STDR2_DATOS_OPERACION/STDR2_HOROPE" />
                        </xsl:element>
                        <xsl:element name="wsb:datosEmpresa" />
                    </xsl:element>
                </xsl:when>

                <xsl:when test="STDR2_DATOS_CONTROL/STDR2_CODOPE = 2010">
                    <xsl:element name="wsb:NotificarPago">
                        <xsl:element name="wsb:codigoOperacion">
                            <xsl:value-of select="STDR2_DATOS_CONTROL/STDR2_CODOPE" />
                        </xsl:element>
                        <xsl:element name="wsb:numeroOperacion">
                            <xsl:value-of select="STDR2_DATOS_CONTROL/STDR2_NUMOPEBCO" />
                        </xsl:element>
                        <xsl:element name="wsb:codigoBanco">
                            <xsl:value-of select="STDR2_DATOS_EMPRESA/STDR2_CODBCO" />
                        </xsl:element>
                        <xsl:element name="wsb:codigoConvenio">
                            <xsl:value-of select="STDR2_DATOS_EMPRESA/STDR2_CODCONTR" />
                        </xsl:element>
                        <xsl:element name="wsb:otrosDatosEmpresa" />
                        <xsl:element name="wsb:numeroReferenciaDeuda">
                            <xsl:value-of
                                select="normalize-space(STDR2_DATOS_TRANSACCION/STDR2_NUMREF_DEUDA)" />
                        </xsl:element>
                        <xsl:element name="wsb:NumDocDeuda">
                            <xsl:text>1</xsl:text>
                        </xsl:element>
                        <xsl:element name="wsb:formaPago">
                            <xsl:value-of select="STDR2_DATOS_TRANSACCION/STDR2_FORPAG" />
                        </xsl:element>
                        <xsl:element name="wsb:codigoMoneda">
                            <xsl:value-of select="STDR2_DATOS_TRANSACCION/STDR2_CODMON" />
                        </xsl:element>
                        <xsl:element name="wsb:importeTotalPagado">
                            <xsl:variable name="importeTotalPagado">
                                <xsl:call-template name="string-replace">
                                    <xsl:with-param name="text"
                                        select="string(STDR2_DATOS_TRANSACCION/STDR2_IMPTOT_PAG)" />
                                    <xsl:with-param name="pattern" select="'.'" />
                                    <xsl:with-param name="replace-with" select="''" />
                                </xsl:call-template>
                            </xsl:variable>
                            <xsl:value-of select="$importeTotalPagado" />
                        </xsl:element>
                        <xsl:element name="wsb:detallePagoBBVA" />
                        <xsl:element name="wsb:canalOperacion">
                            <xsl:value-of select="STDR2_DATOS_OPERACION/STDR2_CANAL" />
                        </xsl:element>
                        <xsl:element name="wsb:codigoOficina">
                            <xsl:value-of select="STDR2_DATOS_OPERACION/STDR2_CODOFI" />
                        </xsl:element>
                        <xsl:element name="wsb:fechaOperacion">
                            <xsl:value-of select="STDR2_DATOS_OPERACION/STDR2_FECOPE" />
                        </xsl:element>
                        <xsl:element name="wsb:horaOperacion">
                            <xsl:value-of select="STDR2_DATOS_OPERACION/STDR2_HOROPE" />
                        </xsl:element>
                        <xsl:element name="wsb:datosEmpresa" />
                    </xsl:element>
                </xsl:when>

            </xsl:choose>

        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

You see that some created elements are "repeated" (same code) but they have differents parents (ConsultarSC and NotificarPago). So my question is how can I reuse that "repeated code", I don't if exists some "tag" that can I use how a "reference" (like ESQL).

like image 618
gecs Avatar asked Mar 29 '13 21:03

gecs


1 Answers

In your example, "named" templates are you friend here. For example, if you wanted to create the same two elements in different places, create a named template like so

 <xsl:template name="exampleTemplate">
    <xsl:element name="wsb:codigoOperacion">
        <xsl:value-of select="STDR2_DATOS_CONTROL/STDR2_CODOPE" />
    </xsl:element>
    <xsl:element name="wsb:numeroOperacion">
        <xsl:value-of select="STDR2_DATOS_CONTROL/STDR2_NUMOPEBCO" />
    </xsl:element>
 </xsl:template>

Then, to create these two elements, just call the template like so:

<xsl:call-template name="exampleTemplate" />

And that's it!

Note, just like functions, you can pass parameters to templates too.

<xsl:template name="example">
    <xsl:param name="value" />
    Value is <xsl:value-of select="$value" />
</xsl:template>

<xsl:call-template name="example">
    <xsl:with-param name="value" select="element" />
</xsl:call-template>

As an aside, there is no need to use xsl:element to create an element if you are using a static name, just write out element name directly. So, instead of this

    <xsl:element name="wsb:numeroOperacion">
        <xsl:value-of select="STDR2_DATOS_CONTROL/STDR2_NUMOPEBCO" />
    </xsl:element>

Just do this

    <wsb:numeroOperacion>
        <xsl:value-of select="STDR2_DATOS_CONTROL/STDR2_NUMOPEBCO" />
    </wsb:numeroOperacion>
like image 168
Tim C Avatar answered Oct 07 '22 21:10

Tim C