【发布时间】:2014-10-01 16:36:08
【问题描述】:
假设我有这个临时文件:
<xsl:variable name="var">
<root>
<sentence>Hello world</sentence>
<sentence>Foo foo</sentence>
</root>
</xsl:variable>
我正在使用分析字符串来查找“世界”字符串并用名为<match>的元素包装它
<xsl:function name="my:parse">
<xsl:param name="input"/>
<xsl:analyze-string select="$input" regex="world">
<xsl:matching-substring>
<match><xsl:copy-of select="."/></match>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:copy-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:function>
这个函数将返回:
Hello <match>world</match>Foo foo
我当然想要这个输出:
<root>
<sentence>Hello <match>world</match></sentence>
<sentence>Foo foo</sentence>
</root>
尽管如此,我知道我的函数为什么要这样做,但我就是不知道如何复制元素并为它们注入新内容。我知道上下文项存在问题,但我尝试了很多其他方法,但对我没有任何作用。
另一方面,使用匹配//text() 的模板可以正常工作。但要求是使用函数(因为我正在研究多相变换并且我想使用代表每个步骤的函数)。我想知道这个问题有解决方案吗?我错过了一些基本的东西吗?
【问题讨论】: