XSLT:전달 변수로 템플릿을 사용할 때 적용 템플릿
-
21-12-2019 - |
문제
나는 확실하지 않으면 이것은 너무 많이 요구하의 XSLT,하지만 난 후에 비슷한 방법 mode
작품을 제외하고,이 중 하나에서 템플릿을 갖기보다는 복제는 그것으로 나는 지금:
<!-- Start -->
<xsl:apply-templates select="airports/airport" mode="start" />
<!-- End -->
<xsl:apply-templates select="airports/airport" mode="end" />
<!-- Template -->
<xsl:template match="airports/airport" mode="start">
<option value="{@iata}" data-alternative-spellings="{.},{@iata}">
<xsl:if test="@iata = 'LGW'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="@iata"/> - <xsl:value-of select="."/>
</option>
</xsl:template>
<xsl:template match="airports/airport" mode="end">
<option value="{@iata}" data-alternative-spellings="{.},{@iata}">
<xsl:if test="@iata = 'LAX'">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="@iata"/> - <xsl:value-of select="."/>
</option>
</xsl:template>
템플릿을 적용합 selected
특성하는 경우 값은 뭔가 특정합니다.이 값에 따라 무엇을 호출합니다.다음과 같은 두 가지 경우가 있습니다, start
고 end
모두 다른 기준입니다.
가장 쉬운 방법을 설명하는 내가 무엇을 하려고 나를 위해 그것을 쓰는 다른 언어:)
<!-- Start -->
getAirports(start);
<!-- End -->
getAirports(end);
<!-- Template -->
var getAirports = function(position)
{
var selected = '';
switch(position)
{
case 'start':
if(iata == 'LGW')
{
var selected = 'selected="selected"';
}
break;
case 'end':
if(iata == 'LAX')
{
var selected = 'selected="selected"';
}
break;
default:
break;
}
return '<option value="'+iata+'" data-alternative-spellings="'+iata+','+name+'" '+selected+'>'+iata+' - '+name+'</option>';
}
이것이 가능한 xslt,또는 나에 충실해야 복사한 템플릿 및 사용 mode
?
감사합니다!
해결책
동료 트위터 사용자에게 솔루션을 찾을 수 있습니다에서 이 Gist.
무 with-param
, 뭔가 결코 전에 사용:
<!-- Start -->
<xsl:apply-templates select="airports/airport">
<xsl:with-param name="position" select="'start'" />
</xsl:apply-templates>
<!-- End -->
<xsl:apply-templates select="airports/airport">
<xsl:with-param name="position" select="'end'" />
</xsl:apply-templates>
<xsl:template match="airports/airport">
<xsl:param name="position" />
<option value="{@iata}" data-alternative-spellings="{.},{@iata}">
<xsl:if test="(@iata = 'LGW' and $position = 'start') or (@iata = 'LAX' and $position = 'end')">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="@iata"/> - <xsl:value-of select="." />
</option>
</xsl:template>
제휴하지 않습니다 StackOverflow