Html XSLT更改内部字符串
我在HTML中使用XSLT来处理提供给我的一些XML。要提供我需要做的一些背景知识: 我将收到一些XML格式的文件Html XSLT更改内部字符串,html,xslt,Html,Xslt,我在HTML中使用XSLT来处理提供给我的一些XML。要提供我需要做的一些背景知识: 我将收到一些XML格式的文件 <base> <string> "Hi, this is a String" </string> <redlist> <red> <start>3</start> <end>5<
<base>
<string>
"Hi, this is a String"
</string>
<redlist>
<red>
<start>3</start>
<end>5</end>
</red>
<red>
<start>9</start>
<end>11</end>
</red>
</redlist>
</base>
“嗨,这是一根绳子”
3.
5.
9
11
这将最终生成一些输出HTML,这些HTML将突出显示
标记中以红色表示的包含字符。所以这应该输出“Hi,这是一个字符串”,其中“th”和“is”部分为红色
我想我需要用子字符串做一些奇特的处理,但我真的不知道怎么做
有什么建议吗
更新:
我有以下几点-
<xsl:for-each select="base/redlist">
<span style="color:black">
<xsl:value-of select="substring(../string, 0, red/start)">
</span>
<span style="color:red">
<xsl:value-of select="substring(../string, red/start, red/end)">
</span>
</xsl:for-each>
但显然这不起作用,因为for-each循环的每次迭代中都有0。请注意,
substring()
的最后一个可选参数是子字符串的长度,而不是结束索引
类似于以下的内容可能是一种方法。您需要根据索引是基于零还是基于一,以及是否希望引号成为字符串的一部分来相应地调整它
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="base">
<span style="color:black">
<xsl:apply-templates select="redlist/red">
<xsl:with-param name="string" select="normalize-space(string)"/>
</xsl:apply-templates>
</span>
</xsl:template>
<xsl:template match="red">
<xsl:param name="string"/>
<xsl:if test="not(preceding-sibling::red)">
<xsl:value-of select="substring($string, 1, start - 1)"/>
</xsl:if>
<span style="color:red">
<xsl:value-of select="substring($string, start, end - start)"/>
</span>
<xsl:variable name="next" select="following-sibling::red"/>
<xsl:choose>
<xsl:when test="$next">
<xsl:value-of select="substring($string, end, $next/start - end)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($string, end)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
当然3-5可以确保只有th
变为红色,而不是这个
。谢谢,我中途换了号码,忘了更新。现在已经更新了主要问题子字符串
函数如何?我打算以类似的方式建议使用调用模板,但我非常喜欢您的解决方案,因为该模板是通过带有参数的红色列表应用到的,而不是相反的方式。