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

Removing namespace from nodes and after that copy that node into new node using xslt

我有 xml 有名字space 所以我想要

  1. 删除名称space 2) 删除名称space 后将该节点复制到不同的节点下,这是我的代码

谁能告诉我如何在单个 xsl 中实现这两个目标。在上面的代码之后,我只得到删除的名称 space。 我看过很多文章,但没有找到与此相关的内容。 输入:

<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>
'''


如果您想将任何字符串包装到新节点中,请使用

<?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>