[英]XSLT for-each loop, filter based on variable
I have the following XSLT 我有以下XSLT
<xsl:param name="productsId" select="/macro/productsId" />
<xsl:param name="type" select="/macro/type" /> <!-- value1, value2, value3 -->
<xsl:template match="/">
<xsl:if test="$productsId > 0">
<xsl:variable name="products" select="umbraco.library:GetXmlNodeById($productsId)" />
<div id="carousel-wrap">
<ul id="carousel">
<xsl:for-each select="$products/Product [select only Product with attribute value1, value2 or value3 based on /macro/type]">
<li id="p-{@id}">
<xsl:variable name="title">
<xsl:choose>
<xsl:when test="string-length(productHeading) > 0">
<xsl:value-of select="productHeading" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@nodeName" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<a href="{umbraco.library:NiceUrl(@id)}">
<!-- Image -->
<xsl:if test="productImage > 0">
<xsl:variable name="productImage" select="umbraco.library:GetMedia(productImage, 0)" />
<img src="/ImageGen.ashx?image={$productImage/umbracoFile}&height=131" />
</xsl:if>
<!-- Title -->
<span><xsl:value-of select="$title" disable-output-escaping="yes" /></span>
</a>
</li>
</xsl:for-each>
</ul>
</div>
</xsl:if>
</xsl:template>
Basically, each product contains 3 attributes with true/false values. 基本上,每个产品都包含3个具有true / false值的属性。
value1 = true
value2 = false
value3 = true
Now I'm passing a parameter to my stylesheet which will be one of these values, ie value3. 现在,我将一个参数传递给样式表,该参数将是这些值之一,即value3。
I would like to select all nodes that have value3 set to true . 我想选择将value3设置为true的所有节点。 Something along the lines of: 类似于以下内容:
<xsl:for-each
select="$products/Product [$type = 'true' (or $type = '1' in XSLT terms)]">
Any ideas on how this can be accomplished? 关于如何实现这一目标的任何想法?
I believe you're looking for something like this: 我相信您正在寻找这样的东西:
<xsl:for-each select=
"$products/Product[@*[name()=$type and (.='true' or .='1')]]">
Note that use of for-each
is rarely necessary. 请注意,很少需要使用for-each
。 Consider the following stylesheet: 考虑以下样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="type" select="'value3'" />
<xsl:template match="/">
<xsl:apply-templates
select="/*/product[@*[name()=$type and (.='true' or .='1')]]" />
</xsl:template>
<xsl:template match="product">
<xsl:value-of select="@id" />
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Applied to this input: 应用于此输入:
<products>
<product id="1" value1="false" value2="false" value3="false"/>
<product id="2" value1="false" value2="true" value3="false"/>
<product id="3" value1="false" value2="false" value3="true"/>
<product id="4" value1="false" value2="false" value3="true"/>
<product id="5" value1="true" value2="false" value3="false"/>
<product id="6" value1="false" value2="false" value3="true"/>
</products>
Produces: 产生:
3
4
6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.