如何使用 <xsl:copy-of> 从 XSLT 2.0 中的输入 XML 中删除命名空间?

How to remove namespaces from input XML in XSLT 2.0 using <xsl:copy-of>?

我正在尝试使用 xsl:copy-of 函数将一个节点从输入 XML 复制到输出 XML。我正在使用 copy-namespaces="no" 模式来避免复制输入名称空间。但是在 product 标签中仍然有一个默认命名空间被复制为 xmlns="".

我的问题是:
如何使用 xsl:copy-of 函数删除这些默认名称空间?而且,我怎样才能从子节点中删除命名空间。

输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns:result xmlns:ns="http://example.com">
   <product ku="00001">
      <ku>00001</ku>
      <custom-attributes>
         <custom-attribute xmlns:dt="http://example.com-dt" dt:dt="string" name="Instrument" />
      </custom-attributes>
   </product>
   <product ku="00002">
      <ku>00002</ku>
      <custom-attributes>
         <custom-attribute xmlns:dt="http://example.com-dt" dt:dt="string" name="Instrument">
            <value>112</value>
         </custom-attribute>
      </custom-attributes>
   </product>
</ns:result>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:t="http://test.com/dataservice/dop" xmlns:fc="http://example.com" exclude-result-prefixes="t fc ">
    <xsl:output indent="yes" encoding="UTF-8"/>
    <xsl:template match="*">
        <final xmlns="http://www.example.com/ns/core/tetrex"
            xmlns:dt="http://www.example.com/ns/core/tetrex-dt"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" branch="enterprise" build="4.0.5"
            family="fine" major="6" minor="1"
            xsi:schemaLocation="http://www.example-dt.com dt.xsd">
            <xsl:for-each select="//fc:result/product">
                <xsl:copy-of select="current()" copy-namespaces="no"  />
            </xsl:for-each>
        </final>
    </xsl:template>
</xsl:stylesheet>

当前输出XML:

<?xml version="1.0" encoding="UTF-8"?>
<final xmlns="http://www.example.com/ns/core/tetrex"
       xmlns:dt="http://www.example.com/ns/core/tetrex-dt"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       branch="enterprise"
       build="4.0.5"
       family="fine"
       major="6"
       minor="1"
       xsi:schemaLocation="http://www.example-dt.com dt.xsd">
   <product xmlns="" ku="00001">
      <ku>00001</ku>
      <custom-attributes>
         <custom-attribute xmlns:dt="http://example.com-dt" dt:dt="string" name="Instrument"/>
      </custom-attributes>
   </product>
   <product xmlns="" ku="00002">
      <ku>00002</ku>
      <custom-attributes>
         <custom-attribute xmlns:dt="http://example.com-dt" dt:dt="string" name="Instrument">
            <value>112</value>
         </custom-attribute>
      </custom-attributes>
   </product>
</final>

期望输出:

<?xml version="1.0" encoding="UTF-8"?>
<final xmlns="http://www.example.com/ns/core/tetrex"
       xmlns:dt="http://www.example.com/ns/core/tetrex-dt"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       branch="enterprise"
       build="4.0.5"
       family="fine"
       major="6"
       minor="1"
       xsi:schemaLocation="http://www.example-dt.com dt.xsd">
   <product ku="00001">
      <ku>00001</ku>
      <custom-attributes>
         <custom-attribute dt:dt="string" name="Instrument"/>
      </custom-attributes>
   </product>
   <product ku="00002">
      <ku>00002</ku>
      <custom-attributes>
         <custom-attribute dt:dt="string" name="Instrument">
            <value>112</value>
         </custom-attribute>
      </custom-attributes>
   </product>
</final>

您显示的输出可以通过应用以下样式表获得:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
xpath-default-namespace="http://example.com"
xmlns="http://www.example.com/ns/core/tetrex"
xmlns:dt="http://example.com-dt"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/result">
    <final
        branch="enterprise" 
        build="4.0.5"
        family="fine" 
        major="6" 
        minor="1"
        xsi:schemaLocation="http://www.example-dt.com dt.xsd">
        <xsl:apply-templates/>
    </final>
</xsl:template>

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

</xsl:stylesheet>

请注意,这不会“从输入 XML 中删除命名空间”。相反:它采用输入 XML 中无名称空间中的元素,并将它们放置在 http://www.example.com/ns/core/tetrex 名称空间中,该名称空间是输出 XML.[=13= 的默认名称空间]


演示https://xsltfiddle.liberty-development.net/ehW12fF