从节点中删除命名空间,然后使用 xslt 将该节点复制到新节点中

问题描述

我有一个具有命名空间的 xml,所以我想要

  1. 删除命名空间 2)删除不同节点下的命名空间复制该节点后,这是我的代码

有人可以告诉我如何在单个 xsl 中实现两者。在上面的代码之后,我只得到了删除名称空间。 我看过很多文章,但没有找到与此相关的内容。 输入:

<UWInitial xmlns:d3p1="http:someUrl">
<d3p1:string>ABC</d3p1:string>
<d3p1:string>EFG</d3p1:string>
<d3p1:string>EHG</d3p1:string>
<d3p1:string>EFD</d3p1:string>
<d3p1:string>ESF</d3p1:string>
</UWInitial>
'''

output what i want :

'''
<UWInitial>
<NewNode>
   <string>ABC</string>
</NewNode>
<NewNode>
   <string>EFG</string>
</NewNode>
<NewNode>
   <string>EHG</string>
</NewNode>
<NewNode>
   <string>EFD</string>
</NewNode>
<NewNode>
   <string>ESF</string>
</NewNode>
</UWInitial>
'''

XSLT that i use:

'''
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:d3p1="someUrl">

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>

     <xsl:template match="d3p1:string" >

        <xsl:element name="NewNode">
                      <xsl:copy>                  
                        <xsl:copy-of select="@*|node()"/>
                      </xsl:copy>
        </xsl:element>
        
    </xsl:template>
        <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
        <xsl:call-template name="Test" />
    </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/>
    </xsl:template>
</xsl:stylesheet>
'''


output which i get:
'''
<UWInitial>
<string>ABC</string>
<string>EFG</string>
<string>EHG</string>
<string>EFD</string>
<string>ESF</string>
</UWInitial>
'''


解决方法

如果要将任何字符串包装到 NewNode 中,请使用

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

  <xsl:strip-space elements="*"/>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*">
      <xsl:element name="{local-name()}">
          <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>
  
  <xsl:template match="d3p1:string">
      <NewNode>
          <xsl:element name="{local-name()}">
              <xsl:apply-templates/>
          </xsl:element>
      </NewNode>
  </xsl:template>

</xsl:stylesheet>