在XSLT中创建一个列表/数组

时间:2010-01-28 18:53:39

标签: xslt

我有以下情况。我有一份国家名单(EG,KSA,UAE,AG)

如果XML输入包含在此列表中,我需要检查它:

<xsl:variable name="$country" select="Request/country" >

<!-- now I need to declare the list of countries here -->

<xsl:choose>
 <!-- need to check if this list contains the country -->
 <xsl:when test="$country='??????'">
   <xsl:text>IN</xsl:text>
 </xsl:when>
 <xsl:otherwise>
   <xsl:text>OUT</xsl:text>
 </xsl:otherwise>
</xsl:choose>

注意:我使用的是XSLT 1.0。

4 个答案:

答案 0 :(得分:4)

修改

再次阅读你的帖子后,我认为我的答案的原始版本(如下)不是它。

已经一个列表 - 您的变量声明选择了<country>个子节点的所有<Request>个节点的节点集(节点集是XSLT等价物)数组/列表):

<xsl:variable name="$country" select="Request/country" >

但关键是,你甚至不需要将该列表作为单独的变量;你所需要的只是:

<xsl:when test="Request[country=$country]"><!-- … --></xsl:when>

其中Request[country=$country]读为“在<Request>内”,请查看每个<country>,如果它等于$country则选择它。“当表达式返回非空节点集时,$country位于列表中。

事实上,这是鲁本斯法里亚斯从一开始就说的。 :)


原始答案,保留备案。

如果用“list”表示逗号分隔的标记字符串:

<!-- instead of a variable, this could be a param or dynamically calculated -->
<xsl:variable name="countries" select="'EG, KSA, UAE, AG'" />
<xsl:variable name="country"   select="'KSA'" />

<xsl:choose>
  <!-- concat the separator to start and end to ensure unambiguous matching -->
  <xsl:when test="
    contains(
      concat(', ', normalize-space($countries), ', ')
      concat(', ', $country, ', ')
    )
  ">
    <xsl:text>IN</xsl:text>
  </xsl:when>
  <xsl:otherwise>
    <xsl:text>OUT</xsl:text>
  </xsl:otherwise>
</xsl:choose>

答案 1 :(得分:3)

<xsl:variable name="$country" select="Request/country"/>
<xsl:variable name="countries">|EG|KSA|UAE|AG|</xsl:variable>

<xsl:when test="contains($countries,$country)">...</xsl:when>

答案 2 :(得分:2)

如果您的国家/地区列表属于您的XML输入,请尝试类似:

<xsl:when test="/yourlist[country = $country]'">

或者,如果那是静态的,你可以选择:

<xsl:when test="$country = 'EG' or $country = 'KSA' or ...">

答案 3 :(得分:0)

我正在修改sam的答案,

您可以创建一个变量,该变量通过管道用字符串分隔(不是列表,而是充当列表),国家/地区代码是唯一的,因此包含方法的效果很好

但是对于类似名称的变量,将'Product','ProductType'和'ProductItem'作为国家/地区参数存在,则它将不起作用,即,如果条件失败,因为所提供的字符串中存在包含方法检查字符串,

所以我的建议是添加分隔符以及名称concat 来作为搜索条件。

String