Update element text with XSLT based on parameter

I am trying to do something similar, it should be very simple, but I cannot get it to work, and I cannot find examples that do not include many irrelevant things. I want to update the text content of a specific xml tag to a specific value (passed as a parameter, this XSLT will be used from ant). A simple example:

I want to convert

<foo> <bar> baz </bar> </foo> 

To

 <foo> <bar> something different </bar> </foo> 

This is the stylesheet I tried, which only leads to 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!

+6
source share
3 answers

There are a number of problems with the provided code that lead to errors during compilation:

 <xsl:template match="/foo/bar/"> <xsl:param name="baz" value="something different"/> <xsl:value-of select="$baz"/> </xsl:template> 
  • The matching pattern specified in this pattern is syntactically illegal - an XPath expression cannot end with a / .

  • xsl:param cannot have an unknown attribute, e.g. value

Decision

 <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 conversion is applied to the provided XML document:

 <foo> <bar> baz </bar> </foo> 

required, the correct result is obtained :

 <foo> <bar>Something Different</bar> </foo> 
+10
source
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output indent="yes" encoding="UTF-8" method="xml"/> <xsl:param name="param-from-ant" as="xs:string"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="/foo/bar"> <xsl:value-of select="$param-from-ant"/> </xsl:template> </xsl:stylesheet> 
0
source

A slightly different approach:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="replace"/> <xsl:template match="node()|@*"> <xsl:choose> <xsl:when test="text() and name(..)='bar' and name(../..)='foo'"> <xsl:value-of select="$replace"/> </xsl:when> <xsl:otherwise> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:otherwise> </xsl:choose> </xsl:template> 

0
source

Source: https://habr.com/ru/post/914798/


All Articles