Home > Blockchain >  removing namespace from output
removing namespace from output

Time:07-26

I have bugged my head for days now to get this... But I cannot seem to get the last bits into order... My skills in XSLT is rather non-existant, so please bear with me :-)

My input (or at least that is the output without the XSLT transformation):

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ORDERRESPONSE>
  <ORDERRESPONSE_HEADER>
    <ORDERRESPONSE_INFO>
      <ORDER_ID>65913603</ORDER_ID>
      <ORDERRESPONSE_DATE>2022-06-03</ORDERRESPONSE_DATE>
      <SUPPLIER_ORDER_ID>800332043</SUPPLIER_ORDER_ID>
    </ORDERRESPONSE_INFO>
  </ORDERRESPONSE_HEADER>
</ORDERRESPONSE>

I have this XSLT code:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 
<xsl:template match="ORDERRESPONSE">
     <ORDERRESPONSE xmlns="http://www.opentrans.org/XMLSchema/2.1"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             Version="2.1">
        <xsl:apply-templates/>
    </ORDERRESPONSE>
</xsl:template>

<xsl:template match="*">    
<xsl:element name="{local-name()}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>  
</xsl:template>

<xsl:template match="@*">    
<xsl:attribute name="{local-name()}"> 
    <xsl:value-of select="."/>
    </xsl:attribute>  
</xsl:template> 
<xsl:template match="comment() | text() | processing-instruction()"> 
<xsl:copy copy-namespaces="no"/>
</xsl:template>

</xsl:stylesheet>

and This is my output:

<?xml version="1.0" encoding="utf-8"?>
<ORDERRESPONSE Version="2.1" xmlns="http://www.opentrans.org/XMLSchema/2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ORDERRESPONSE_HEADER xmlns="">
    <ORDERRESPONSE_INFO>
      <ORDER_ID>65913603</ORDER_ID>
      <ORDERRESPONSE_DATE>2022-06-03</ORDERRESPONSE_DATE>
      <SUPPLIER_ORDER_ID>800332043</SUPPLIER_ORDER_ID>
    </ORDERRESPONSE_INFO>
  </ORDERRESPONSE_HEADER>
</ORDERRESPONSE>

but HOW do I get rid of the xmlns=""???

This is my desired output:

<?xml version="1.0" encoding="utf-8"?>
    <ORDERRESPONSE Version="2.1" xmlns="http://www.opentrans.org/XMLSchema/2.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <ORDERRESPONSE_HEADER xmlns="">
        <ORDERRESPONSE_INFO>
          <ORDER_ID>65913603</ORDER_ID>
          <ORDERRESPONSE_DATE>2022-06-03</ORDERRESPONSE_DATE>
          <SUPPLIER_ORDER_ID>800332043</SUPPLIER_ORDER_ID>
        </ORDERRESPONSE_INFO>
      </ORDERRESPONSE_HEADER>
    </ORDERRESPONSE>

CodePudding user response:

I presume the result you want is actually:

<?xml version="1.0" encoding="UTF-8"?>
<ORDERRESPONSE xmlns="http://www.opentrans.org/XMLSchema/2.1"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               Version="2.1">
   <ORDERRESPONSE_HEADER>
      <ORDERRESPONSE_INFO>
         <ORDER_ID>65913603</ORDER_ID>
         <ORDERRESPONSE_DATE>2022-06-03</ORDERRESPONSE_DATE>
         <SUPPLIER_ORDER_ID>800332043</SUPPLIER_ORDER_ID>
      </ORDERRESPONSE_INFO>
   </ORDERRESPONSE_HEADER>
</ORDERRESPONSE>

As I mentioned in my comment to your question, to get this result you must add a namespace to all processed elements, not remove it:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/ORDERRESPONSE">
    <ORDERRESPONSE xmlns="http://www.opentrans.org/XMLSchema/2.1"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             Version="2.1">
        <xsl:apply-templates/>
    </ORDERRESPONSE>
</xsl:template>

<xsl:template match="*">    
    <xsl:element name="{local-name()}" namespace="http://www.opentrans.org/XMLSchema/2.1">
        <xsl:apply-templates/>
    </xsl:element>  
</xsl:template>

</xsl:stylesheet>
  • Related