删除元素上的命名空间

Remove Namespace on Element

我想删除输出结构中的命名空间。我准备了 XSLT 代码 但它在这个元素上给出了命名空间

我的输入XML是这个

<?xml version='1.0' encoding='UTF-8'?> 
<n0:Messages xmlns:n0="http://sap.com/xi/XI"> 
<n0:Message> 
        <ContactData>   
        <Data>
          <information>
                  <Name>A</Name>
                  <Phone>123456</Phone>   
          </information>
        </Data> 
        </ContactData> 
</n0:Message>
</n0:Messages>

已实施 XSLT 代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:n0="http://sap.com/xi/XI" exclude-result-prefixes="n0">

<!-- Output -->
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
     <xsl:copy-of select= "//ContactData"/>
</xsl:template>
<xsl:template match="//*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>
</xsl:stylesheet>

当前输出:

<?xml version='1.0' encoding='UTF-8'?>  
        <ContactData xmlns:n0="http://sap.com/xi/XI">   
        <Data>
          <information>
                  <Name>A</Name>
                  <Phone>123456</Phone>   
          </information>
        </Data> 
        </ContactData> 

预期输出

<?xml version='1.0' encoding='UTF-8'?>  
            <ContactData>   
            <Data>
              <information>
                      <Name>A</Name>
                      <Phone>123456</Phone>   
              </information>
            </Data> 
            </ContactData> 

请帮助解决此代码 非常感谢。

使用模板 *@ 试试这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:n0="http://sap.com/xi/XI"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs math n0" version="1.0">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <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="/">
        <xsl:apply-templates select="/*:Messages/ContactData"/>
    </xsl:template>

</xsl:stylesheet>

如果您能够使用 XSLT 2.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:strip-space elements="*"/>

<xsl:template match="/">
    <xsl:copy-of select="*/*/*" copy-namespaces="no"/>
</xsl:template>

</xsl:stylesheet>

演示https://xsltfiddle.liberty-development.net/3NSSEuK


XSLT 1.0 中,需要做更多的工作:

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

<xsl:template match="/">
    <xsl:apply-templates select="*/*/*" />
</xsl:template>

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

</xsl:stylesheet>

演示https://xsltfiddle.liberty-development.net/3NSSEuK/1