문제

조건부 결과에 따라 다른 매개 변수가있는 템플릿을 적용하고 싶습니다. 이 같은:

<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:choose a xsl:apply-templates 시작/종료 태그이지만 그것은 합법적이지 않은 것 같습니다. 단서가 있습니까?

도움이 되었습니까?

해결책

대체 방법은 XSL을 선택하는 것입니다 : XSL : Param Elements 내에서 문을 선택하십시오

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

다른 팁

방법에는 아무런 문제가 없지만 조건부를 추가 할 수도 있습니다. xsl:template match 기인하다. 이것은 단 하나로 이어질 것입니다 xsl:apply-templates, 그러나 몇몇 xsl:template 집단

조건을 사전에 추출하여 모든 논리와 모드를 제거 할 수 있습니다. 당신은 당신이 다루는 요소의 이름이 무엇인지 말하지 않지만 그것이 foo 그러면 이와 같은 것이 충분해야합니다.

<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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top