I'm trying to do something that seems like it should be very simple, but I can't get it working, and I can't seem to find any examples that don't involve lots of irrelevant things. I want to update the text content of a specific xml tag to a specific value (passed in as a parameter, this XSLT will be used from ant). A simple example :
I want to transform
<foo>
<bar>
baz
</bar>
</foo>
To
<foo>
<bar>
something different
</bar>
</foo>
This is the stylesheet that I tried, which results in just the tags, no text at all
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- identity transformation, to keep everything unchanged except for the stuff we want to change -->
<!-- Whenever you match any node or any attribute -->
<xsl:template match="node()|@*">
<!-- Copy the current node -->
<xsl:copy>
<!-- Including any attributes it has and any child nodes -->
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- change the text of the bar node, in the real template the value won't be specified inline -->
<xsl:template match="/foo/bar/">
<xsl:param name="baz" value="something different"/>
<xsl:value-of select="$baz"/>
</xsl:template>
</xsl:stylesheet>
Thanks in advance!
A slightly different approach:
There are a number of problems with the provided code which lead to compile-time errors:
The match pattern specified on this template is syntactically illegal -- an XPath expression cannot end with the
/
character.xsl:param
cannot have an unknown attribute such asvalue
Solution:
When this transformation is applied on the provided XML document:
the wanted, correct result is produced: