且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

复制所有 xml 节点并使用 XSLT 重命名其中的几个节点

更新时间:2023-02-19 20:34:59

您需要一个 身份转换 增加了额外的模板以匹配您想要重命名的节点.像这样:

You would need an identity transformation augmented with additional templates to match nodes you want renamed. Like this:

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

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

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

</xsl:stylesheet>

应用于(更正结束 storybody 标签中的错字)时:

When applied to (corrected the typo in the closing storybody tag):

<mothertag>
     <atag>
        asdfasdfa
     </atag>

     <storybody>
        <p>sometext here<URI ref="http://google.com" /> some more text</p>
     </storybody>

</mothertag>

产生:

<mothertag>
   <atag>
      asdfasdfa

   </atag>
   <storybody>
      <p>sometext here
         <TAG ref="http://google.com"></TAG> some more text
      </p>
   </storybody>
</mothertag>