Transform XML document:
xsltproc -o output.xml update-attribute.xslt input.xml
| <?xml version="1.0"?> | |
| <root> | |
| <parent> | |
| <child id="a" value="true"/> | |
| <child id="b" value="true"/> | |
| <child id="c" value="true"/> | |
| </parent> | |
| </root> |
| <?xml version="1.0"?> | |
| <root> | |
| <parent> | |
| <child id="a" value="false"/> | |
| <child id="b" value="null"/> | |
| <child id="c" value="true"/> | |
| </parent> | |
| </root> |
| <?xml version="1.0"?> | |
| <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common"> | |
| <!-- Update the specified children. --> | |
| <xsl:variable name="update"> | |
| <child id="a" value="false"/> | |
| <child id="b" value="null"/> | |
| </xsl:variable> | |
| <!-- Match "value" attribute. --> | |
| <xsl:template match="/root/parent/child/@value"> | |
| <!-- Get child ID. --> | |
| <xsl:variable name="id" select="current()/parent::node()/@id"/> | |
| <!-- Get matching update. --> | |
| <xsl:variable name="match" select="exsl:node-set($update)/*[@id = $id]"/> | |
| <!-- If there is a match, update value; otherwise, keep value. --> | |
| <xsl:choose> | |
| <xsl:when test="$match"> | |
| <xsl:attribute name="{name()}"> | |
| <xsl:value-of select="$match/@value"/> | |
| </xsl:attribute> | |
| </xsl:when> | |
| <xsl:otherwise> | |
| <xsl:copy/> | |
| </xsl:otherwise> | |
| </xsl:choose> | |
| </xsl:template> | |
| <!-- Copy all nodes and attributes unless another rule indicates otherwise. --> | |
| <xsl:template match="@*|node()"> | |
| <xsl:copy> | |
| <xsl:apply-templates select="@*|node()"/> | |
| </xsl:copy> | |
| </xsl:template> | |
| </xsl:stylesheet> |