gpt4 book ai didi

xslt - 如何使用 XSLT 添加或替换元素

转载 作者:行者123 更新时间:2023-12-01 05:01:14 25 4
gpt4 key购买 nike

我正在使用 XSLT 应用配置转换。我想添加一个元素,如果它不存在,或者如果它存在则覆盖它。

期望的结果:

<foo>
<bar value="baz" />
</foo>

我可以使用什么模板来使两个输入都具有此输出

<foo>
<bar value="123" />
</foo>

<foo>
</foo>

最佳答案

这个转换:

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

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

<xsl:template match="foo/bar/@value[.='123']">
<xsl:attribute name="value">baz</xsl:attribute>
</xsl:template>

<xsl:template match="foo[not(bar)]">
<foo>
<bar value="baz"/>
</foo>
</xsl:template>
</xsl:stylesheet>

应用于第一个提供的 XML 文档时:

<foo>
<bar value="123" />
</foo>

产生想要的、正确的结果:

<foo>
<bar value="baz"/>
</foo>

应用于第二个提供的 XML 文档时:

<foo>
</foo>

它也产生相同的正确结果

说明:正确使用和覆盖 identity rule .

关于xslt - 如何使用 XSLT 添加或替换元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10333059/

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