List / Array Creation in XSLT

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

I need to check the XML input if it is 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.

+6
xslt
source share
3 answers

EDIT

After reading the post again, I think the original version of my answer (below) is wrong.

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

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

But the fact is that you do not need this list as a separate variable; anything you need:

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

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

This, in fact, is what Rubens Farias said from the very beginning. :)


Original response saved for recording.

If by β€œlist” you mean a string of tokens, separated by commas:

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

+4
source share

Try something like if your list of countries belongs to your XML input:

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

Or, if it's static, you can go with:

 <xsl:when test="$country = 'EG' or $country = 'KSA' or ..."> 
+2
source share
 <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
source share

All Articles