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

前端之家收集整理的这篇文章主要介绍了如何使用XSLT更新XML中的单个值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个巨大的 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相关文章