简体   繁体   中英

XSL - How to remove this specific Namespace Prefix?

Have this XML, and I need to remove all the prefixes, I done this before, but there is a specific NS2 prefix that Im unable to remove.

check:

Input XML

     <NS1:Envelope xmlns:NS1="http://schemas.xmlsoap.org/soap/envelope/">
 <NS1:Header>
 <NS2:responseHeader xmlns:NS2="http://test.com">
  <systemId>SI</systemId> 
  <messageId>000001</messageId> 
  <timestamp>2015-01-16T10:10:09.872983</timestamp> 
<responseStatus>
  <statusCode>Success</statusCode> 
  </responseStatus>
  </NS2:responseHeader>
  </NS1:Header>
 <NS1:Body>
  <NS2:modificarSolicitudDeCreditoResponse xmlns:NS2="http://test2.com/21"/> 
   </NS1:Body>
  </NS1:Envelope>

My XSLT:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
 <xsl:template match="NS2:modificarSolicitudDeCreditoResponse">
  <modificarSolicitudDeCreditoResponse>
    <xsl:apply-templates select="@*|node()"/>
  </modificarSolicitudDeCreditoResponse>
</xsl:template>
<xsl:template match="NS1:Header">
  <Header>
    <xsl:apply-templates select="@*|node()"/>
  </Header>
  </xsl:template>
  <xsl:template match="NS2:responseHeader">
  <responseHeader>
    <xsl:apply-templates select="@*|node()"/>
  </responseHeader>
  </xsl:template>
  <xsl:template match="NS1:Body">
  <Body>
    <xsl:apply-templates select="@*|node()"/>
  </Body>
  </xsl:template>
         <xsl:template match="/*">
    <Envelope>
    <xsl:apply-templates select="node()" />
    </Envelope>
  </xsl:template>
     </xsl:stylesheet>

And this is my output:

<?xml version='1.0' ?>
<Envelope xmlns:NS1="http://schemas.xmlsoap.org/soap/envelope/"
 <Header>
 <responseHeader>
  <systemId>AW0856</systemId> 
  <messageId>0000012</messageId> 
  <timestamp>2015-01-16T10:10:09.872983</timestamp> 
<responseStatus>
  <statusCode>Success</statusCode> 
  </responseStatus>
  </responseHeader>
  </Header>
 <Body>
  <NS2:modificarSolicitudDeCreditoResponse xmlns:NS2="http://test2.com/21"> 
  </Body>
  </Envelope>

Im having problems removing the NS2 after the body tag, Does anyone knows how I can achieve this, thanks

Here is how you remove all prefixes of the input XML: copy the elements while replacing their name() with local-name() (name without namespace).

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

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

</xsl:stylesheet>

Output is

<?xml version="1.0"?>
<Envelope>
    <Header>
        <responseHeader>
            <systemId>SI</systemId> 
            <messageId>000001</messageId> 
            <timestamp>2015-01-16T10:10:09.872983</timestamp> 
            <responseStatus>
                <statusCode>Success</statusCode> 
            </responseStatus>
        </responseHeader>
    </Header>
    <Body>
        <modificarSolicitudDeCreditoResponse/>             
    </Body>
</Envelope>

But this also removes all the namespace definitions. Currently I don't know how you like to handle these.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM