(updated question from 'static replace' to 'dynamic replace')
<data>
<replacements>
<replace key="greeting" value='Hello' />
<replace key="name" value='Donald' />
</replacements>
<rules>
<rule key="GREETING" value="replacements/replace[@key='greeting']/@value" />
<rule key="NAME" value="replacements/replace[@key='name']/@value" />
</rules>
<text>
GREETING NAME duck.
</text>
</data>
I'd like to replace the text in the element text
with every occurrence of a key in all the rules. The replacement text is the evaluation of the attribute value
in that element.
Which approach should I take (using the newest Saxon HE)?
The result of the text should be “Hello Donald duck.”
Addition:
I think part of the problem can be solved with <xsl:evaluate>
, but this is obviously not working yet (no string replacements):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" >
<xsl:output indent="yes"/>
<xsl:template match="data">
<xsl:copy>
<xsl:evaluate xpath="/data/rules/rule[1]/@value" context-item="." />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The result is <data value="Hello"/>
. What I see is the dynamic evaluation of the xpath expression, and from there it should be possible to do the string replacement.
Now my step would be to add my approach to Martin's answer, but this would be mixing XSLT in XPath, which does not work that way I want.