gpt4 book ai didi

XSLT:如何更改父标签名称并从 XML 文件中删除属性?

转载 作者:行者123 更新时间:2023-12-02 11:41:34 25 4
gpt4 key购买 nike

我有一个 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>

请帮我将父标签名称从“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>

关于XSLT:如何更改父标签名称并从 XML 文件中删除属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1769526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com