我有一个XML文件,我需要从中删除一个名为"Id"的属性(它必须在任何地方删除)并且我还需要重命名父标记,同时保持其属性和子元素不变.你能不能请帮我修改代码.一次,我只能实现两个要求中的一个.我的意思是我可以完全从文档中删除该属性,或者我可以更改父标记.这是我的代码,删除属性"Id":
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@Id[parent::*]">
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
请帮我将父标签名称从"Root"更改为"Batch".
所提供的解决方案都没有真正解决问题:它们只是重命名名为"Root"的元素(甚至只是顶部元素),而不验证此元素是否具有"Id"属性.
wwerner最接近正确的解决方案,但重命名父级的父级.
这是一个具有以下属性的解决方案:
这是代码:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="vRep" select="'Batch'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@Id"/>
<xsl:template match="*[@Id]">
<xsl:element name="{$vRep}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)