2013-04-02 18 views
1

節點我使用這個文件的XSL:如何避免爲所有兒童空間聲明在XSLT

校長:

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

謝謝事先,

問候

回答

0

命名空間是越來越添加到所有的子節點,因爲你使用的wsb:ConsultarSC ELE命名空間代替http://tempuri.org/。換句話說,當前父節點和子節點正在以不同的命名空間輸出。

而是這行:

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

使用本:

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

,它應該表現你想要的方式。

+0

謝謝「JLRishie」。我很尷尬,我沒有意識到,我在變量的值和名稱空間聲明的值之間犯了一個錯誤。 謝謝 – gecs