Create a list/array in XSLT
Asked Answered
C

4

9

I have the following scenario. I have a list of countries (EG, KSA, UAE, AG)

I need to check an XML input if it is contained in this list or not:

<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>

Note: I am using XSLT 1.0.

Creamcups answered 28/1, 2010 at 18:53 Comment(2)
that list belongs on your XML input?Goodwife
What is the input XML like? Are the country codes text node children or <country> elements or e.g. attributes?Rhizomorphous
T
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>
Titfer answered 24/2, 2012 at 13:28 Comment(0)
L
5

EDIT

Upon reading your post again, I think the original version of my answer (below) is not it.

You have a list already - your variable declaration selects a node-set of all <country> nodes that are children of <Request> (a node-set is the XSLT equivalent of an array/a list):

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

But the point is, you don't even need that list as a separate variable; all you need is:

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

Where Request[country=$country] reads as "Within <Request>, look at every <country> and select it if it is equal to $country." When the expression returns a non-empty node-set, $country is in the list.

Which is, in fact, what Rubens Farias said from the start. :)


Original answer, kept for the record.

If by "list" you mean a comma-separated string of tokens:

<!-- 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>

Liebig answered 29/1, 2010 at 10:17 Comment(0)
G
2

Try something like, if your country list belongs on your XML input:

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

Or, if that's static, you could go with:

<xsl:when test="$country = 'EG' or $country = 'KSA' or ...">
Goodwife answered 28/1, 2010 at 19:16 Comment(1)
<xsl:when test="count(/yourlist[country=$country]) &gt; 0"> is redundant - it's equivalent to <xsl:when test="/yourlist[country=$country]">. :-)Liebig
G
0

I am modifying sam's answer,

you can create a variable which is pipe separated as a string (not a list but act as list), country codes are unique so contains method works fine

but for the similar name variables consider 'Product', 'ProductType' and 'ProductItem' present as countries parameter then it will not work ie if condition fails as contains method check string is present in the provided string,

So my suggestion is to add separators also along with the name ie concat to work contains as search criteria.

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

    <xsl:when test="contains($countries,concat('|',$country,'|')">
        your logic goes here....
    </xsl:when>
Gilemette answered 1/9, 2020 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.