我有两个包含逗号分隔值的变量:-
<xsl:variable name="Include-Cities" select="'London, Paris, Washington, Tokyo'"/>
<xsl:variable name="Exclude-Cities" select="'Paris, Tokyo'"/>
我需要删除$Include-Cities
中与$Exclude-Cities
中的值匹配的值,因此在某种程度上,我从$Include-Cities
变量中减去这些值并输出结果。
我在网上查找了一下,发现以下示例提供了搜索和替换功能,如果$Exclude cities中的城市顺序与$Include Citizens中的顺序匹配,则该示例有效,但如果值的顺序不同,则失败。
我被卡住了,因为两个列表中的值每天都会变化,我永远不知道这些值是什么,所以我认为执行排序(如果可能的话)是行不通的。
我发现的例子:-
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
然后我调用模板使用:-
<xsl:call-template name="replace-string">
<xsl:with-param name="text" select="$Include-Cities"/>
<xsl:with-param name="replace" select="$Exclude-Cities" />
<xsl:with-param name="with" select="''"/>
</xsl:call-template>
我也看过将价值观标记化并以这种方式进行比较的例子,但没有任何乐趣。
我知道2.0中有可用的字符串比较函数,但我仅限于使用XSLT1.0。
我是一个XSLT专家,有人能帮忙吗?
任何帮助都将不胜感激
我还看了标记价值观和比较的例子那样的话,却没有任何快乐。
标记是的正确方法。如果您的处理器支持EXSLT str:tokenize
扩展功能,您可以执行以下操作:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="Include-Cities" select="'London, Paris, Washington, Tokyo'"/>
<xsl:variable name="Exclude-Cities" select="'Paris, Tokyo'"/>
<xsl:template match="/">
<xsl:variable name="incl" select="str:tokenize($Include-Cities, ', ')"/>
<xsl:variable name="excl" select="str:tokenize($Exclude-Cities, ', ')"/>
<output>
<xsl:copy-of select="$incl[not(.=$excl)]"/>
</output>
</xsl:template>
并得到:
结果
<?xml version="1.0" encoding="UTF-8"?>
<output>
<token>London</token>
<token>Washington</token>
</output>
否则,您将不得不使用递归命名模板来进行标记化,将结果转换为节点集,然后进行如上所示的比较。