XSLT 字符串解析

标签 xslt string

对于下面的“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&amp;d f'"/>
     </xsl:call-template>
   </xsl:template>

    <xsl:template match="testmap:*">
      <xsl:param name="arg1"/>

      <xsl:choose>
       <xsl:when test="$arg1 = '&amp;'">%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/

相关文章:

python - 从字符串创建子字符串列表

java - Regex java正则表达式提取除最后一个数字以外的字符串

html - 页面中的div排列

python - 有没有办法禁用 lxml 中 anchor 属性的 urlencoding

xml - 转换内容在 CDATA 中的 xml 元素

c# - 我正在为字符串实现 CaseAccentInsensitiveEqualityComparer。我不确定如何实现 GetHashCode

java - 替换文本文件中的特定字符串

string - 递归查找文件中的文本 (PowerShell)

java - xslt - 无法使用属性选择器访问当前节点

xslt - 在转换 xml 时处理命名空间问题