XSLT

2010-01-28 28 views
6

'de bir liste/dizi oluşturma Aşağıdaki senaryo var. Ben bu listede yer alan veya değilse ben bir XML girişini kontrol etmek gerekirXSLT

ülkede (EG, KSA, BAE, AG) bir listesi var:

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

Not: XSLT 1.0 kullanıyorum .

+0

o liste XML girişi aittir:

"listesine" tarafından Eğer jeton virgülle ayrılmış dize demek? –

+0

XML girişi nasıldır? Ülke, metin düğümü çocukları veya öğelerini veya ör. Öznitellikler? – jelovirt

cevap

1

Ülkeniz liste XML girişi aitse, böyle bir şey deneyin:

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

Veya, o statik eğer, birlikte gidebiliriz: okuma üzerine

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

'" gereksizdir - bu, ile eşdeğerdir . :-) – Tomalak

+0

güzel bahşiş Tomalak, ty –

4

EDIT'e

Gönderin tekrar, cevabımın orijinal versiyonunun (aşağıda) olduğunu düşünüyorum.

Sen zaten bir listesi var - sizin değişken ifadesi seçen bir düğüm kümesi <Request> çocukları (bir düğüm kümesi/listesidir bir dizinin XSLT eşdeğerdir) tüm <country> düğümlerin:

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

Ancak nokta, 'un ayrı bir değişken olarak listelenen'a bile gerek duymaz; tek ihtiyacınız olan: Request[country=$country] olarak okur

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

"Her <country> bakmak ve onu $country eşitse seçin <Request> içinde." İfade boş olmayan bir düğüm kümesi döndürdüğünde, listede $country bulunur. Aslında, Rubens Farias'ın başlangıçta ne dediği aslında budur.


Orijinal cevap, kayıt için tutuldu.

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