Text Value Templates
Text Value Templates
Text Value Templates (TVT) allow XPath expressions inside {...} in text content, similar to how Attribute Value Templates (AVT) work in attribute values.
Enabling TVT
Add expand-text="yes" to the stylesheet root element:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
expand-text="yes">
<xsl:template match="person">
<p>Hello {name}, you are {age} years old.</p>
</xsl:template>
</xsl:stylesheet>Without expand-text="yes", the text {name} would be output literally.
Where TVT applies
When expand-text="yes" is active, expressions in {...} are evaluated in:
- Text content of literal result elements:
<p>Value: {$x}</p> - Text content of
xsl:text:<xsl:text>Total: {sum(item/price)}</xsl:text> - Any text node in the template body
Escaping braces
Use doubled braces to output literal { and }:
<xsl:template match="data">
<code>map{{key: {$value}}}</code>
<!-- Output: map{key: 42} -->
</xsl:template>Overriding per element
Individual elements can override the inherited expand-text setting:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
expand-text="yes">
<!-- TVT active here -->
<xsl:template match="data">
<p>Computed: {$value}</p>
</xsl:template>
<!-- TVT disabled for this variable (e.g. to preserve JSON braces) -->
<xsl:variable name="json" expand-text="no">{"key": "value"}</xsl:variable>
</xsl:stylesheet>On literal result elements, use the xsl:expand-text attribute (in the XSL namespace):
<pre xsl:expand-text="no">function() { return {x: 1}; }</pre>AVT vs TVT
| Feature | Attribute Value Templates (AVT) | Text Value Templates (TVT) |
|---|---|---|
| Location | Attribute values on literal elements | Text content |
| Syntax | {expr} |
{expr} |
| Activation | Always active on literal element attributes | Requires expand-text="yes" |
| Example | <div class="item-{@id}"> |
<p>Hello {$name}!</p> |