The problem is that my output has the
element twice:
<?xml version="1.0"?>
<Config>
<Enabled>false</Enabled>
<Interval><Interval>77</Interval></Interval>
</Config>
Not true!
The output when this transformation is applied on the provided XML document is:
<Config>
<Enabled>false</Enabled>
<PollingInterval><Interval>77</Interval></PollingInterval>
</Config>
In case you want to get rid of either element, simply remove the corresponding <xsl:element>
instruction.
For example: Removing <xsl:element name="PollingInterval">
the transformation becomes:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Config/Interval">
<xsl:element name="Interval">77</xsl:element>
</xsl:template>
</xsl:stylesheet>
and the result from applying it on the provided XML document is:
<Config>
<Enabled>false</Enabled>
<Interval>77</Interval>
</Config>
I recommend to simplify the transformation further and to match on the text node child of Interval
. This is possibly the shortest and simplest solution:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Interval/text()">
<xsl:text>77</xsl:text>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<Config>
<Enabled>false</Enabled>
<Interval>5</Interval>
</Config>
the wanted result is produced:
<Config>
<Enabled>false</Enabled>
<Interval>77</Interval>
</Config>
In case you have many Interval
elements and want only to replace the value 5
by 77
then the only template except the identity rule should be:
<xsl:template match="Interval/text()[.=5]">
<xsl:text>77</xsl:text>
</xsl:template>