对于下面的“PrimarySubject”,我是 XSLT 的新手,我需要将其包含的“&”字符替换为 %26,将“”字符替换为 %20。
<xsl:template name="BuildLink">
<xsl:param name="PrimarySubject" />
<xsl:text>?PrimarySubject=</xsl:text>
<xsl:value-of select="$PrimarySubject" />
</xsl:template>
我可以在 xslt 版本 1 中使用字符串替换功能吗? 谢谢,
最佳答案
使用 FXSL Library 可以最轻松地完成此操作,更确切地说是它的 str-map
模板。
这是一个简单的例子。
这个转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:testmap="testmap"
exclude-result-prefixes="xsl testmap"
>
<xsl:import href="str-map.xsl"/>
<!-- to be applied on any xml source -->
<testmap:testmap/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="vTestMap" select="document('')/*/testmap:*[1]"/>
<xsl:call-template name="str-map">
<xsl:with-param name="pFun" select="$vTestMap"/>
<xsl:with-param name="pStr" select="'abc&d f'"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="testmap:*">
<xsl:param name="arg1"/>
<xsl:choose>
<xsl:when test="$arg1 = '&'">%26</xsl:when>
<xsl:when test="$arg1 = ' '">%20</xsl:when>
<xsl:otherwise><xsl:value-of select="$arg1"/></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
应用于任何 XML 文档(未使用)时,会产生所需的结果:
abc%26d%20f
关于XSLT 字符串解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2426229/