value-of很可怕 当我需要将大量变量值插入文本节点时,它确实污染了XSL文件.
有没有办法能够使用属性表达式表示法,即text text {$variable}在输出文本节点的内部?或者至少比这更简洁value-of?
不在XSLT 1.0中.但是,在XSLT 3.0中,您可以使用TVT(文本值模板).它们的工作方式与AVT(属性值模板)相同.
要使用TVT,请将标准属性添加xsl:expand-text="yes"到元素中.这将导致处理器将该元素的后代文本节点视为TVT.
例:
XSLT 3.0
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:variable name="who" select="'Dan'"/>
<xsl:variable name="what" select="'BAM!'"/>
<result xsl:expand-text="yes">This is {$who}'s result: {$what}</result>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
输出(使用任何格式良好的XML作为输入)
<result>This is Dan's result: BAM!</result>
Run Code Online (Sandbox Code Playgroud)
注意:使用Saxon-PE 9.5进行测试.
这是一个更好的例子,显示正在评估的"后代"文本节点...
XML输入
<test>
<v1>one</v1>
<v2>two</v2>
<v3>three</v3>
</test>
Run Code Online (Sandbox Code Playgroud)
XSLT 3.0
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/*">
<result xsl:expand-text="yes">
<value>Value of v1: {v1}</value>
<value>Value of v2: {v2}</value>
<value>Value of v3: {v3}</value>
</result>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
产量
<result>
<value>Value of v1: one</value>
<value>Value of v2: two</value>
<value>Value of v3: three</value>
</result>
Run Code Online (Sandbox Code Playgroud)