如何使用 XSLT 更新 XML 中的单个值? [英] How to use XSLT to update a single value in an XML?
本文介绍了如何使用 XSLT 更新 XML 中的单个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个巨大的 XML 文件,我想在其中更新单个值.有没有办法编写一个 XSLT 文件,它可以通过简单的更改生成现有 XML 文件的精确副本?
I have a giant XML file in which I would like to update a single value. Is there a way to write an XSLT file which will produce an exact copy of the existing XML file with a simple change?
例如,假设我有以下 XML,并且我想将员工 Martin 的职位编号更改为 100.我该怎么做?
For instance, let's say I have the following XML and that I want to change the position number of employee Martin to 100. How can I do this?
<?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>
这篇关于如何使用 XSLT 更新 XML 中的单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文