XSLT

2010-01-28 3 views
6

में कोई सूची/सरणी बनाएं मेरे पास निम्न परिदृश्य है। मैं देशों (जैसे, KSA, संयुक्त अरब अमीरात, एजी)XSLT

मैं एक 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> 

नोट: मैं XSLT 1.0 का उपयोग कर रहा ।

+0

उस सूची आपके XML इनपुट पर अंतर्गत आता है:

"सूची" से यदि आप टोकन के एक अल्पविराम से अलग स्ट्रिंग मतलब है? –

+0

इनपुट एक्सएमएल कैसा है? क्या देश कोड टेक्स्ट नोड बच्चे हैं या तत्व या उदा। जिम्मेदार बताते हैं? – jelovirt

उत्तर

1

, की तरह कुछ की कोशिश करो अपने देश सूची आपके XML इनपुट पर संबंधित है तो:

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

या, कि यदि स्थिर है, तो आप के साथ जा सकते हैं:

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

'' अनावश्यक है - यह '' । :-) – Tomalak

+0

अच्छी टिप Tomalak, ty –

4

संपादित

पढ़ने पर आपकी पोस्ट फिर से, मुझे लगता है कि मेरे उत्तर का मूल संस्करण (नीचे) यह नहीं है।

आप एक सूची पहले से ही है - अपने चर घोषणा का चयन करता है एक नोड-सेट सभी <country> नोड्स <Request> के बच्चों (एक नोड-सेट एक सरणी के XSLT बराबर/एक सूची है) कर रहे हैं की:

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

लेकिन बिंदु यह है कि, आप भी की आवश्यकता नहीं है जो एक अलग चर के रूप में सूचीबद्ध है; आप सभी की जरूरत है:

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

कहाँ Request[country=$country] के रूप में लिखा है "<Request> के भीतर, हर <country> को देखने और उसका चयन अगर यह $country के बराबर है।" जब अभिव्यक्ति एक गैर-खाली नोड-सेट लौटाती है, तो $country सूची में है।

वास्तव में, रूबेंस फरियास ने शुरुआत से क्या कहा था। :)


मूल उत्तर, रिकॉर्ड के लिए रखा गया।

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