I would like to modify an attribute of a very long xml like this:
<element index="0">
<subelement bla="asdf" />
<subelement bla="asdf" />
</element>
<element index="1">
<subelement bla="asdf" />
<subelement bla="asdf" />
</element>
...
I need to add N the value of each index attribute. Say N=5. The result would be:
<element index="5">
<subelement bla="asdf" />
<subelement bla="asdf" />
</element>
<element index="6">
<subelement bla="asdf" />
<subelement bla="asdf" />
</element>
...
What's the easiest way to do this? I presume it would be with XSLT, but I don't know how to do it.
Thanks!
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- copy everything verbatim -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- except "element" nodes -->
<xsl:template match="element">
<xsl:copy>
<xsl:attribute name="index">
<xsl:value-of select="@index + 5"/>
</xsl:attribute>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With