我'm using XSLT to transform complex XML output of a content management system into XHTML. I'使用 <xsl:apply-templates/>
来获取XML输入描述的XHTML片段 . XML输入带有一个非常复杂的结构,可以描述许多不同的情况,由几个XSLT模板元素处理 . 而且这种结构将来可能会经常发生变化 .
以前,该转换的结果片段直接发送到XSLT输出 . 现在需求已经改变,我需要捕获结果,偶尔修改它以在片段的值的某个位置插入一些其他格式良好的XHTML片段 .
为了演示,考虑 <xsl:apply-templates/>
创建了一些在变量 container 中捕获的不透明XHTML片段 .
<xsl:variable name="container">
<xsl:apply-templates />
</xsl:variable>
接下来在变量 snippet 中有第二个XHTML片段:
<xsl:variable name="snippet">
<xsl:call-template name="get-snippet" />
</xsl:variable>
要求说 $snippet 中的节点集在 $container 的值结尾处的任何可选包含句点之前插入 . 这个ain 't problematic unless XHTML fragments in both variables have to be kept as fragments. Thus one can' t对任一变量的字符串值进行操作 .
有没有机会在XSLT中实现该要求而不会失去 <xsl:apply-templates/>
在 $container 中检索XHTML片段的能力和灵活性?
BTW:我已经知道如何使用以下方法访问 $container
中的最后一个文本节点:
node-set($container)//child::text[last()]
但我错过了在该文本节点的中间插入一些东西,我认为XSLT无法为我想做的事情提供适当的支持 .
1 回答
I. XSLT 1.0 solution :
When this transformation is applied (to any XML document -- not used), the wanted, correct result is produced :
Explanation :身份规则由递归命名模板覆盖,以查找字符串中的最后一个
'.'
.II. XSLT 2.0 solution :
When this XSLT 2.0 transformation is performed, again the wanted correct result is produced :
Explanation :使用标准XPath 2.0函数
string-to-codepoints()
,index-of()
,substring()
和operatoris
.