2010-01-28 18 views
6

Ho il seguente scenario. Ho una lista di paesi (per esempio, KSA, UAE, AG)Creare un elenco/array in XSLT

devo controllare un input XML se contenute in questo elenco o no:

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

Nota: sto usando XSLT 1.0 .

+0

quella lista appartiene sul vostro input XML? –

+0

Qual è l'input XML come? I codici Paese sono nodi di testo o elementi o ad es. attributi? – jelovirt

risposta

1

Provare qualcosa di simile, se la vostra lista paese appartiene sul vostro input XML:

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

Oppure, se è statica, si potrebbe andare con:

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

'' è ridondante - equivale a '' . :-) – Tomalak

+0

bel consiglio Tomalak, ty –

4

EDIT

Upon lettura il tuo post di nuovo, penso che la versione originale della mia risposta (sotto) non lo è.

È hai un elenco già - la vostra dichiarazione di variabile seleziona un nodo-set di tutti <country> nodi che sono figli di <Request> (un set di nodi è l'equivalente XSLT di un array/una lista):

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

Ma il punto è che non hai nemmeno bisogno di elencare come variabile separata; tutto ciò che serve è:

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

Dove Request[country=$country] recita "Entro <Request>, guardare ogni <country> e selezionarlo se è uguale a $country." Quando l'espressione restituisce un set di nodi non vuoto, $country si trova nell'elenco.

Che è, infatti, quello che ha detto Rubens Farias dall'inizio. :)


Risposta originale, conservata per la cronaca.

Se per "lista" intendi una stringa separato da virgole di gettoni:

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

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