0

I need an XSL solution to replace XML nodes with new nodes.

Say I have the following existing XML structure:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:MT_AvanzarEventoIdCase2_Req_Out xmlns:ns0="http://colpensiones.gov.co/AvanzarEventoIdCase2">
   <configuracion>
      <tipoSistemaExterno>test4</tipoSistemaExterno>
      <tipoProcesamiento>test5</tipoProcesamiento>
      <idCorrelacion>test6</idCorrelacion>
   </configuracion>
   <informacionEventoCasoBPM>
      <numeroRadicadoCaso>test</numeroRadicadoCaso>
      <nombreEvento>test2</nombreEvento>
      <informacionRelacionada>test3</informacionRelacionada>
   </informacionEventoCasoBPM>
</ns0:MT_AvanzarEventoIdCase2_Req_Out>

and I need to tranform to this structure:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:per="http://www.colpensiones.gov.co/contracts/1.0/personas" 
xmlns:per1="http://www.colpensiones.gov.co/schemas/1.0/personas" 
xmlns:com="http://www.colpensiones.gov.co/contracts/1.0/comun" 
xmlns:com1="http://www.colpensiones.gov.co/schemas/1.0/comun">
   <soapenv:Header>
      <per:Configuracion>
         <per1:tipoSistemaExterno>?</per1:tipoSistemaExterno>
         <per1:tipoProcesamiento>?</per1:tipoProcesamiento>
         <per1:idCorrelacion>?</per1:idCorrelacion>
      </per:Configuracion>
   </soapenv:Header>
   <soapenv:Body>
      <com:InformacionEventoCasoBPM>
         <com1:numeroRadicadoCaso>?</com1:numeroRadicadoCaso>
         <com1:nombreEvento>?</com1:nombreEvento>
         <com1:informacionRelacionada>?</com1:informacionRelacionada>
      </com:InformacionEventoCasoBPM>
   </soapenv:Body>
</soapenv:Envelope>

I'he been trying for 1 week and I couldn't, This was my structure but it didn't work:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:per="http://www.colpensiones.gov.co/contracts/1.0/personas" 
 xmlns:per1="http://www.colpensiones.gov.co/schemas/1.0/personas" 
 xmlns:com="http://www.colpensiones.gov.co/contracts/1.0/comun" 
 xmlns:com1="http://www.colpensiones.gov.co/schemas/1.0/comun"
  exclude-result-prefixes="soapenv per per1 com com1">
  
  <xsl:template match = "/"> 
  <soapenv:envelope>
   <soapenv:header>
       <per:Configuracion>
          <per1:tipoSistemaExterno><xsl:value-of select = "tipoSistemaExterno"/></per1:tipoSistemaExterno>
          <per1:tipoProcesamiento><xsl:value-of select = "tipoProcesamiento"/></per1:tipoProcesamiento>
          <per1:idCorrelacion><xsl:value-of select = "idCorrelacion"/></per1:idCorrelacion>         
       </per:Configuracion>  
   </soapenv:header>
    <soapenv:body> 
             <com:InformacionEventoCasoBPM>
                 <com1:numeroRadicadoCaso><xsl:value-of select = "numeroRadicadoCaso"/></com1:numeroRadicadoCaso>
                 <com1:nombreEvento><xsl:value-of select = "nombreEvento"/></com1:nombreEvento>
                 <com1:informacionRelacionada><xsl:value-of select = "informacionRelacionada"/></com1:informacionRelacionada>               
             </com:InformacionEventoCasoBPM>
         </soapenv:body> 
  </soapenv:envelope>
  </xsl:template>
</xsl:stylesheet>

Thank for your help.

1 Answers1

0

I just assumed that you want exactly that output. The code for that would be:

<xsl:template match="/">
    <xsl:element name="soapenv-Envelope">
        <xsl:element name="soapenv-Header">
            <xsl:apply-templates select="//configuracion"/>
        </xsl:element>  
        <xsl:element name="soapenv-Body">
            <xsl:apply-templates select="//informacionEventoCasoBPM"/>
        </xsl:element>  
    </xsl:element>  
</xsl:template>

<xsl:template match="configuracion">
    <xsl:element name="per-Configuration">
        <xsl:apply-templates/>
    </xsl:element>  
</xsl:template>

<xsl:template match="*[ancestor::configuracion]">
    <xsl:element name="{concat('per1-', local-name())}">
        <!-- copy the text within the node -->
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<xsl:template match="informacionEventoCasoBPM">
    <xsl:element name="com-InformacionEventoCasoBPM">
        <xsl:apply-templates/>
    </xsl:element>  
</xsl:template>

<xsl:template match="*[ancestor::informacionEventoCasoBPM]">
    <xsl:element name="{concat('com1-', local-name())}">
        <!-- copy the text within the node -->
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

I replaced the : with - though because I did not want to declare new namespaces.

Christian Mosz
  • 543
  • 2
  • 12
  • Thank you for the answer, now i'm getting this error: Multiple annotations found at this line: - The processing instruction target matching "[xX][mM] [lL]" is not allowed. - No grammar constraints (DTD or XML schema) detected for the document. – Oscar Florez Apr 13 '18 at 14:48
  • Looks for me like you did replace your WHOLE document with the code. Past the code instead of your templates. So paste it inside the tag. I do not really know this error, but this seems to be promising: https://stackoverflow.com/questions/19889132/error-the-processing-instruction-target-matching-xxmmll-is-not-allowed?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Christian Mosz Apr 17 '18 at 07:50