2014-07-08 1 views
0

record 노드는 "미국"과 같이 USAlabel과 같은 @code입니다.xslt를 사용하여 드롭 다운의 기본값을 지정하려고합니까?

<xsl:for-each select="/output/module/countries/data/record"> 
    <xsl:call-template name="option"> 
    <xsl:with-param name="value" select="@code"/> 
    <xsl:with-param name="label" select="@name"/> 
    <!-- 
     <xsl:with-param name="select" select="/output/module/formdata/data/record/billing_info/country"/> 
    --> 
    <xsl:param name="value" value="USA" /> 
    </xsl:call-template> 
</xsl:for-each> 

USA을 기본값으로 설정하려고합니다. 그래도 with:param name="select" select="USA"을 시도했지만 아무런 의미가 없습니다. 흠?

이상적으로 코멘트에 지정된 다른 노드에 값이없는 경우 USA을 기본값으로 설정하고 싶습니다.

+0

버전은 무엇? –

답변

1

XSLT 2.0에서는 selectif를 사용할 수 있습니다

<xsl:with-param name="select" select="if (x) then x else 'USA'"/> 

은 그냥 XPath는 ( /output/module/formdata/data/record/billing_info/country)와 x의 두 인스턴스를 교체합니다. 당신이 당신의 option 템플릿에 xsl:choose을 추가 할 수 있습니다 XSLT 1.0

는 전달 된 select PARAM의 값을 테스트하는 식으로 뭔가 :. 당신은 그 xsl:choose을 넣을 수

<xsl:choose> 
    <xsl:when test="string($select)"> 
     <xsl:value-of select="$select"/> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:text>USA</xsl:text> 
    </xsl:otherwise> 
</xsl:choose> 

xsl:variable에 쉽게 있다면 (속성 값이나 값에 여러 번 액세스해야하는 경우와 같이) 사용할 수 있습니다. 여기

1

는 XSLT의 모든 버전에서이 작업을 수행하는 방법은 다음과 같습니다 XSLT의

<xsl:variable name="countryVal" 
       select="/output/module/formdata/data/record/billing_info/country" /> 
<xsl:variable name="countryOrDefault" 
      select="concat($countryVal, 
         substring('USA', 1, 3 * not(normalize-space($countryVal)))" /> 
<xsl:for-each select="/output/module/countries/data/record"> 
    <xsl:call-template name="option"> 
    <xsl:with-param name="value" select="@code"/> 
    <xsl:with-param name="label" select="@name"/> 
    <xsl:with-param name="select" select="$countryOrDefault"/> 
    </xsl:call-template> 
</xsl:for-each> 
관련 문제