gpt4 book ai didi

xml - 如何使用 XSLT 更新 XML 中的单个值?

转载 作者:数据小太阳 更新时间:2023-10-29 02:02:23 26 4
gpt4 key购买 nike

我有一个巨大的 XML 文件,我想在其中更新单个值。有没有一种方法可以编写一个 XSLT 文件,通过简单的更改就可以生成现有 XML 文件的精确副本?

例如,假设我有以下 XML,我想将员工 Martin 的职位编号更改为 100。我该怎么做?

<?xml version="1.0" encoding="utf-8"?>
<Employees>
<!-- ... -->

<Employee name="Martin">
<Position number="50" />
</Employee>

<!-- ... -->
</Employees>

最佳答案

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<!--Identity template that will copy every
attribute, element, comment, and processing instruction
to the output-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<!--more specific template match on @number that will
change the value to 100-->
<xsl:template match="Employee[@name='Martin']/Position/@number">
<xsl:attribute name="number">100</xsl:attribute>
</xsl:template>

</xsl:stylesheet>

关于xml - 如何使用 XSLT 更新 XML 中的单个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9745141/

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