How can I "reuse" code in XSL

I am using XSLT node. My question is about XSL. I searched on the Internet, but I only found XML processing information to show it on a web page. The information I'm looking for is the "reuse" of code in this scenario:

My XSL:

<?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 of the created elements are “repeated” (the same code), but they have different parents ( ConsultarSC and NotificarPago ). So my question is how can I reuse this “re-code”, I don’t know if there is some kind of “tag” that I can use as a “link” (for example, ESQL).

+4
source share
3 answers

In your example, the "named" templates are different here. For example, if you want to create the same two elements in different places, create a named template this way

  <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, simply call the template as follows:

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

And this!

Notice, like functions, you can also pass parameters to templates.

 <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 use a static name, just write the element name directly. So instead

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

Just do it

  <wsb:numeroOperacion> <xsl:value-of select="STDR2_DATOS_CONTROL/STDR2_NUMOPEBCO" /> </wsb:numeroOperacion> 
+4
source

Since your result follows the same template with some additional materials added in certain situations, you can have one template for the main layout, and then call other conditional templates for additional material:

  <xsl:import href="HubGenerico.xsl" /> <xsl:template match="/RCECSTD2"> <soap:Envelope> <soap:Header /> <soap:Body /> <xsl:apply-templates select="STDR2_DATOS_CONTROL/STDR2_CODOPE" /> </soap:Envelope> </xsl:template> <!-- Override the default handling of text nodes --> <xsl:template match="text()" /> <xsl:template match="STDR2_CODOPE[. = 1010]"> <wsb:ConsultarSC> <xsl:call-template name="Content" /> </wsb:ConsultarSC> </xsl:template> <xsl:template match="STDR2_CODOPE[. = 2010]"> <wsb:NotificarPago> <xsl:call-template name="Content" /> </wsb:NotificarPago> </xsl:template> <xsl:template name="Content"> <xsl:call-template name="Basic" /> <xsl:apply-templates select="." mode="otrosDatos" /> <xsl:call-template name="NumeroDeuda" /> <xsl:apply-templates select="." mode="deudaPagado" /> <xsl:call-template name="Operacion" /> </xsl:template> <!-- Context node is STDR2_CODOPE --> <xsl:template name="Basic"> <xsl:variable name="empresa" select="../../STDR2_DATOS_EMPRESA" /> <wsb:codigoOperacion> <xsl:value-of select="."/> </wsb:codigoOperacion> <wsb:numeroOperacion> <xsl:value-of select="../STDR_NUMOPEBCO" /> </wsb:numeroOperacion> <wsb:codigoBanco> <xsl:value-of select="$empresa/STDR2_CODBCO"/> </wsb:codigoBanco> <wsb:codigoConvenio> <xsl:value-of select="$empresa/STDR2_CODCONTR"/> </wsb:codigoConvenio> </xsl:template> <xsl:template name="NumeroDeuda"> <wsb:numeroReferenciaDeuda> <xsl:value-of select="normalize-space(../../STDR2_DATOS_TRANSACCION/STDR2_NUMREF_DEUDA)"/> </wsb:numeroReferenciaDeuda> </xsl:template> <xsl:template name="Operacion"> <xsl:variable name="operacion" select="../../STDR2_DATOS_OPERACION" /> <wsb:canalOperacion> <xsl:value-of select="$operacion/STDR2_CANAL" /> </wsb:canalOperacion> <wsb:codigoOficina> <xsl:value-of select="$operacion/STDR2_CODOFI" /> </wsb:codigoOficina> <wsb:fechaOperacion> <xsl:value-of select="$operacion/STDR2_FECOPE" /> </wsb:fechaOperacion> <wsb:horaOperacion> <xsl:value-of select="$operacion/STDR2_HOROPE" /> </wsb:horaOperacion> <wsb:datosEmpresa /> </xsl:template> <xsl:template match="STDR2_CODOPE[. = 2010]" mode="otrosDatos"> <wsb:otrosDatosEmpresa /> </xsl:template> <xsl:template match="STDR2_CODOPE[. = 2010]" mode="deudaPagado"> <xsl:variable name="transaccion" select="../../STDR2_DATOS_TRANSACCION" /> <wsb:NumDocDeuda>1</wsb:NumDocDeuda> <wsb:formaPago> <xsl:value-of select="$transaccion/STDR2_FORPAG" /> </wsb:formaPago> <wsb:codigoMoneda> <xsl:value-of select="$transaccion/STDR2_CODMON" /> </wsb:codigoMoneda> <wsb:importeTotalPagado> <xsl:call-template name="string-replace"> <xsl:with-param name="text" select="string($transaccion/STDR2_IMPTOT_PAG)" /> <xsl:with-param name="pattern" select="'.'" /> <xsl:with-param name="replace-with" select="''" /> </xsl:call-template> </wsb:importeTotalPagado> <wsb:detallePagoBBVA /> </xsl:template> 
0
source

Thanks to "JLRishe" and "Tim C". My XSL files lead to:

Head:

`

 <xsl:import href="HubGenerico.xsl" /> <xsl:variable name="myns" select="'http://tempuri.org/'" /> <xsl:template match="/RCECSTD2"> <soap:Envelope> <soap:Header /> <soap:Body /> <xsl:variable name="codOperacion" select="STDR2_DATOS_CONTROL/STDR2_CODOPE" /> <xsl:choose> <xsl:when test="$codOperacion = 1010"> <wsb:ConsultarSC xmlns:wsb="'http://tempuri.org/'"> <xsl:call-template name="STDR2_DATOS_CONTROL"> <xsl:with-param name="ns" select="$myns" /> </xsl:call-template> <xsl:call-template name="STDR2_DATOS_EMPRESA"> <xsl:with-param name="ns" select="$myns" /> </xsl:call-template> <wsb:numeroReferenciaDeuda> <xsl:value-of select="normalize-space(STDR2_DATOS_TRANSACCION/STDR2_NUMREF_DEUDA)" /> </wsb:numeroReferenciaDeuda> <xsl:call-template name="STDR2_DATOS_OPERACION"> <xsl:with-param name="ns" select="$myns" /> </xsl:call-template> <wsb:datosEmpresa /> </wsb:ConsultarSC> </xsl:when> <xsl:when test="$codOperacion = 2010"> <wsb:NotificarPago xmlns:wsb="$ns"> <xsl:call-template name="STDR2_DATOS_CONTROL"> <xsl:with-param name="ns" select="$myns" /> </xsl:call-template> <xsl:call-template name="STDR2_DATOS_EMPRESA"> <xsl:with-param name="ns" select="$myns" /> </xsl:call-template> <wsb:otrosDatosEmpresa /> <wsb:numeroReferenciaDeuda> <xsl:value-of select="normalize-space(STDR2_DATOS_TRANSACCION/STDR2_NUMREF_DEUDA)" /> </wsb:numeroReferenciaDeuda> <wsb:NumDocDeuda> <xsl:text>1</xsl:text> </wsb:NumDocDeuda> <wsb:formaPago> <xsl:value-of select="STDR2_DATOS_TRANSACCION/STDR2_FORPAG" /> </wsb:formaPago> <wsb:codigoMoneda> <xsl:value-of select="STDR2_DATOS_TRANSACCION/STDR2_CODMON" /> </wsb:codigoMoneda> <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" /> </wsb:importeTotalPagado> <wsb:detallePagoBBVA /> <xsl:call-template name="STDR2_DATOS_OPERACION"> <xsl:with-param name="ns" select="$myns" /> </xsl:call-template> <wsb:datosEmpresa /> </wsb:NotificarPago> </xsl:when> </xsl:choose> </soap:Envelope> </xsl:template> 

`

Imported XSL:

`

 <xsl:template name="string-replace"> <xsl:param name="text" /> <xsl:param name="pattern" /> <xsl:param name="replace-with" /> <xsl:choose> <xsl:when test="contains($text, $pattern)"> <xsl:value-of select="substring-before($text, $pattern)" /> <xsl:value-of select="$replace-with" /> <xsl:call-template name="string-replace"> <xsl:with-param name="text" select="substring-after($text, $pattern)" /> <xsl:with-param name="pattern" select="$pattern" /> <xsl:with-param name="replace-with" select="$replace-with" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text" /> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- Datos de Control --> <xsl:template name="STDR2_DATOS_CONTROL"> <xsl:param name="ns" /> <xsl:variable name="datControl" select="STDR2_DATOS_CONTROL" /> <xsl:element name="wsb:codigoOperacion" namespace="{$ns}"> <xsl:value-of select="$datControl/STDR2_CODOPE" /> </xsl:element> <xsl:element name="wsb:numeroOperacion" namespace="{$ns}"> <xsl:value-of select="$datControl/STDR2_NUMOPEBCO" /> </xsl:element> </xsl:template> <!-- Datos de Empresa --> <xsl:template name="STDR2_DATOS_EMPRESA"> <xsl:param name="ns" /> <xsl:variable name="datEmpresa" select="STDR2_DATOS_EMPRESA" /> <xsl:element name="wsb:codigoBanco" namespace="{$ns}"> <xsl:value-of select="$datEmpresa/STDR2_CODBCO" /> </xsl:element> <xsl:element name="wsb:codigoConvenio" namespace="{$ns}"> <xsl:value-of select="$datEmpresa/STDR2_CODCONTR" /> </xsl:element> </xsl:template> <!-- Datos de Operacion --> <xsl:template name="STDR2_DATOS_OPERACION"> <xsl:param name="ns" /> <xsl:variable name="datOperacion" select="STDR2_DATOS_OPERACION" /> <xsl:element name="wsb:canalOperacion" namespace="{$ns}"> <xsl:value-of select="$datOperacion/STDR2_CANAL" /> </xsl:element> <xsl:element name="wsb:codigoOficina" namespace="{$ns}"> <xsl:value-of select="$datOperacion/STDR2_CODOFI" /> </xsl:element> <xsl:element name="wsb:fechaOperacion" namespace="{$ns}"> <xsl:value-of select="$datOperacion/STDR2_FECOPE" /> </xsl:element> <xsl:element name="wsb:horaOperacion" namespace="{$ns}"> <xsl:value-of select="$datOperacion/STDR2_HOROPE" /> </xsl:element> </xsl:template> 

`

0
source

All Articles