如何避免XSLT中所有子节点的名称空间声明



我使用这个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/">
    <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>
</xsl:stylesheet>
进口XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <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>
</xsl:stylesheet>

我得到这个XML结果(输出转换):

<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header/>
  <soap:Body/>
  <wsb:ConsultarSC xmlns:wsb="'http://tempuri.org/'">
    <wsb:codigoOperacion xmlns:wsb="http://tempuri.org/">1010</wsb:codigoOperacion>
    <wsb:numeroOperacion xmlns:wsb="http://tempuri.org/">36</wsb:numeroOperacion>
    <wsb:codigoBanco xmlns:wsb="http://tempuri.org/">11</wsb:codigoBanco>
    <wsb:codigoConvenio xmlns:wsb="http://tempuri.org/">404</wsb:codigoConvenio>
    <wsb:numeroReferenciaDeuda>20205221868</wsb:numeroReferenciaDeuda>
    <wsb:codigoOficina xmlns:wsb="http://tempuri.org/">CN</wsb:codigoOficina>
    <wsb:codigoOficina xmlns:wsb="http://tempuri.org/">1</wsb:codigoOficina>
    <wsb:fechaOperacion xmlns:wsb="http://tempuri.org/">20130327</wsb:fechaOperacion>
    <wsb:horaOperacion xmlns:wsb="http://tempuri.org/">123020</wsb:horaOperacion>
    <wsb:datosEmpresa/>
  </wsb:ConsultarSC>
</soap:Envelope>

如何"避免"(在所有子元素中)"重复"命名空间声明:

xmlns:wsb="http://tempuri.org/"

我只需要一个声明(在父元素中),像这样:

<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Header/>
      <soap:Body/>
      <wsb:ConsultarSC xmlns:wsb="http://tempuri.org/">
        <wsb:codigoOperacion>1010</wsb:codigoOperacion>
        <wsb:numeroOperacion>36</wsb:numeroOperacion>
        <wsb:codigoBanco>11</wsb:codigoBanco>
        <wsb:codigoConvenio>404</wsb:codigoConvenio>
        <wsb:numeroReferenciaDeuda>20205221868</wsb:numeroReferenciaDeuda>
        <wsb:canalOperacion>CN</wsb:canalOperacion>
        <wsb:codigoOficina>1</wsb:codigoOficina>
        <wsb:fechaOperacion>20130327</wsb:fechaOperacion>
        <wsb:horaOperacion>123020</wsb:horaOperacion>
        <wsb:datosEmpresa/>
      </wsb:ConsultarSC>
    </soap:Envelope>

提前感谢,

名称空间将被添加到所有子节点,因为您为wsb:ConsultarSC元素使用的名称空间是'http://tempuri.org/'而不是http://tempuri.org/。换句话说,当前父节点和子节点以不同的命名空间输出。

代替这一行:

<wsb:ConsultarSC xmlns:wsb="'http://tempuri.org/'">
使用

:

<wsb:ConsultarSC xmlns:wsb="http://tempuri.org/">

,它应该按照你想要的方式运行

最新更新