2010-01-28 8 views
6

다음과 같은 경우가 있습니다. 나는이 목록에 포함되지 않았거나 만약 내가 XML 입력을 확인해야XSLT에서 목록/배열 만들기

국가 (EG, KSA, 아랍 에미리트 연방, AG)의 목록을 가지고 :

<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

입력 XML은 어떤 형식입니까? 국가 코드 텍스트 노드 하위 또는 요소입니까? 속성? – jelovirt

답변

1

국가 목록이 XML 입력에 속하는 경우, 같은 것을보십시오 :

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

또는 그 정적이라면, 당신은 함께 갈 수 : 읽기시

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

''은 중복이다. . :-) – Tomalak

+0

멋진 팁 Tomalak, ty –

4

편집

귀하의 게시물을 다시, 나는 내 대답 (아래)의 원본 버전이 아닌 것 같아요.

당신 이미에게 목록을 가지고 - 당신의 변수 선언은 선택하는 노드 집합 <Request>의 아이들 (노드 - 세트/목록 배열의 XSLT에 해당합니다) 모든 <country> 노드 :

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

하지만 요점은 도 필요하지 않습니다.이 목록은 별도의 변수로 표시되는입니다. 당신이 필요로하는 모든입니다 Request[country=$country] 같이 읽어

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

"모든 <country>보고는 $country 동일한 경우 선택, <Request> 내." 표현식이 비어 있지 않은 노드 집합을 반환하면 $country이 목록에 있습니다.

사실 Rubens Farias가 처음부터 말한 것은 사실입니다. :)


원본 답변은 기록에 보관됩니다.

<!-- 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> 
관련 문제