繁体   English   中英

如何使用XSLT 1.0比较两个逗号分隔的列表并删除匹配的值

[英]How to compare two comma-separated lists and removing matching values using XSLT 1.0

我有两个包含逗号分隔值的变量:

  <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-Cities中的顺序匹配,则该示例有效,但如果值的顺序不同则失败。

我很困,因为两个列表中的值每天都会更改,我永远也不知道这些值是什么,因此我认为执行排序(如果可能的话)不会起作用。

我发现的例子:

<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中提供了字符串比较功能,但仅限于使用XSLT 1.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>

否则,您将必须使用递归命名模板进行标记,将结果转换为节点集,然后进行比较,如上所示。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM