2009-10-21 5 views
2

조건부 결과에 따라 다른 매개 변수가있는 템플릿을 적용하고 싶습니다. 다음과 같은 것 :XSLT : 조건부 매개 변수를 사용하여 템플릿을 적용 하시겠습니까?

<xsl:choose> 
    <xsl:when test="@attribute1"> 
     <xsl:apply-templates select='.' mode='custom_template'> 
      <xsl:with-param name="attribute_name" tunnel="yes">Attribute no. 1</xsl:with-param> 
      <xsl:with-param name="attribute_value" tunnel="yes"><xsl:value-of select="@attribute1"/></xsl:with-param> 
     </xsl:apply-templates> 
    </xsl:when> 
    <xsl:when test="@attribute2"> 
     <xsl:apply-templates select='.' mode='custom_template'> 
      <xsl:with-param name="attribute_name" tunnel="yes">Attribute no. 2</xsl:with-param> 
      <xsl:with-param name="attribute_value" tunnel="yes"><xsl:value-of select="@attribute1"/></xsl:with-param> 
     </xsl:apply-templates> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:apply-templates select='.' mode='custom_template'> 
      <xsl:with-param name="attribute_name" tunnel="yes">Error</xsl:with-param> 
      <xsl:with-param name="attribute_value" tunnel="yes">No matching attribute </xsl:with-param> 
      </xsl:apply-templates> 
    </xsl:otherwise> 
</xsl:choose> 

우선, 나는이 방법이 훨씬 더 나은 방법으로 해결 될 수 있다고 생각합니다. (저는 XSLT에 대해 완전히 새로운 기능을 제공하므로 개선을 제안하고 부 풀린 코드를 용서하십시오.)

이제 질문 :이 조건을 기반으로 매개 변수를 설정하고 어떻게 여전히 xsl:apply-templates에 사용할 수 있습니까? 전체 xsl:choosexsl:apply-templates 시작/끝 태그로 감싸려고했지만 분명히 유효하지 않습니다. 모든 단서?

답변

9

는 또 다른 방법은 XSL을 넣어하는 것입니다 다음 XSL 내에서 문을 선택합니다 PARAM 요소를

<xsl:apply-templates select="." mode="custom_template"> 
    <xsl:with-param name="attribute_name" tunnel="yes"> 
     <xsl:choose> 
     <xsl:when test="@attribute1">Attribute no. 1</xsl:when> 
     <xsl:when test="@attribute2">Attribute no. 2</xsl:when> 
     <xsl:otherwise>Error</xsl:otherwise> 
     </xsl:choose> 
    </xsl:with-param> 
    <xsl:with-param name="attribute_value" tunnel="yes"> 
     <xsl:choose> 
     <xsl:when test="@attribute1"><xsl:value-of select="@attribute1"/></xsl:when> 
     <xsl:when test="@attribute2"><xsl:value-of select="@attribute1"/></xsl:when> 
     <xsl:otherwise>No matching attribute </xsl:otherwise> 
     </xsl:choose> 
    </xsl:with-param> 
</xsl:apply-templates> 
+0

+1. 나를 때려. ;-) – Tomalak

+0

감사합니다, 팀 C! 그게 더 좋아 보인다. – conciliator

3

귀하의 방법에는 아무런 문제가 없지만 xsl:template match 속성에 귀하의 조건을 추가 할 수도 있습니다. 이것은 단지 하나의 xsl:apply-templates으로 이어질 것입니다,하지만 몇 가지 xsl:template 요소

+0

감사 루벤스는, 그러나 당신이있어 무엇을 완전히 확실하지 않다 : 당신은 당신이 다루고 있지만, 가정하고있는 요소의 이름은 다음과 같은 일이 충분해야 foo라고 무슨 말을하지 않습니다 목표는 ... 나는 아직 구체적인 예도없이 당신을 따라가는 데 너무 미숙하다. – conciliator

3

당신은 모든 논리를 제거 할 수 있으며 귀하의 조건을 술어로 추출하여 모드.

<xsl:template match="foo[@attribute1]"> 
    <!-- 
     do stuff for the case when attribute1 is present 
     (and does not evaluate to false) 
    --> 
</xsl:template> 

<xsl:template match="foo[@attribute2]"> 
    <!-- 
     do stuff for the case when attribute2 is present 
     (and does not evaluate to false) 
    --> 
</xsl:template> 

<xsl:template match="foo"> 
    <!-- 
     do stuff for the general case 
     (when neither attribute1 nor attribute 2 are present) 
    --> 
</xsl:template>