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

Muh*_*edy 7 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>
Run Code Online (Sandbox Code Playgroud)

注意:我使用的是XSLT 1.0.

小智 6

<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>
Run Code Online (Sandbox Code Playgroud)


Tom*_*lak 5

编辑

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

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

<xsl:variable name="$country" select="Request/country" >
Run Code Online (Sandbox Code Playgroud)

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

<xsl:when test="Request[country=$country]"><!-- … --></xsl:when>
Run Code Online (Sandbox Code Playgroud)

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

事实上,Rubens Farias 从一开始就是这么说的。:)


原始答案,留作记录。

如果“列表”是指以逗号分隔的令牌字符串:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)


Rub*_*ias 2

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

<xsl:when test="/yourlist[country = $country]'">
Run Code Online (Sandbox Code Playgroud)

或者,如果这是静态的,您可以选择:

<xsl:when test="$country = 'EG' or $country = 'KSA' or ...">
Run Code Online (Sandbox Code Playgroud)