Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSLT:使用条件参数应用模板?_Xslt_Param_Apply Templates - Fatal编程技术网

XSLT:使用条件参数应用模板?

XSLT:使用条件参数应用模板?,xslt,param,apply-templates,Xslt,Param,Apply Templates,我想根据条件转换的结果应用具有不同参数的模板。大概是这样的: <xsl:choose> <xsl:when test="@attribute1"> <xsl:apply-templates select='.' mode='custom_template'> <xsl:with-param name="attribute_name" tunnel="yes">Attribute no. 1</x

我想根据条件转换的结果应用具有不同参数的模板。大概是这样的:

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

属性1
属性2
错误
没有匹配的属性
首先,我怀疑这可以以一种更好的方式解决。(我对XSLT完全陌生,因此请提出改进建议,并原谅臃肿的代码。)


现在是问题:我如何基于这个条件设置参数,并且仍然在
xsl:apply templates
中使用它们?我试图用
xsl:apply-templates
start-/end标记包装整个
xsl:choose
,但这显然是不合法的。有什么线索吗?

您的方法没有问题,但您也可以将条件附加到
xsl:template match
属性中。这将导致只有一个
xsl:apply templates
,但有几个
xsl:template
元素

另一种方法是将xsl:choose语句放在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>

属性1
属性2
错误
没有匹配的属性

通过将条件提取到谓词中,您可以摆脱所有这些逻辑和模式。您不会说您正在处理的元素的名称是什么,但是假设它被称为
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>


谢谢你,蒂姆·C!那似乎更好。谢谢鲁本斯,但我不完全确定你的目标是什么。。。我还是太没经验了,没有任何具体的例子就无法理解你。