Update the text of an element with XSLT based on param
xslt
Solution
There are a number of problems with the provided code which lead to compile-time errors:
<xsl:template match="/foo/bar/">
<xsl:param name="baz" value="something different"/>
<xsl:value-of select="$baz"/>
</xsl:template>
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 as `value`
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:param name="pReplacement" select="'Something Different'"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="foo/bar/text()">
<xsl:value-of select="$pReplacement"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<foo>
<bar>
baz
</bar>
</foo>
the wanted, correct result is produced:
<foo>
<bar>Something Different</bar>
</foo>
Problem
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!