XSLT for-each 循环,基于变量过滤 [英] XSLT for-each loop, filter based on variable
问题描述
我有以下 XSLT
<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="轮播"><xsl:for-each select="$products/Product [根据/macro/type 只选择具有属性 value1、value2 或 value3 的产品]"><li id="p-{@id}"><xsl:variable name="title"><xsl:when test="string-length(productHeading) > 0"><xsl:value-of select="productHeading"/></xsl:when><xsl:否则><xsl:value-of select="@nodeName"/></xsl:否则></xsl:选择></xsl:变量><a href="{umbraco.library:NiceUrl(@id)}"><!-- 图片--><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><!-- 标题--><span><xsl:value-of select="$title" disable-output-escaping="yes"/></span></a></xsl:for-each>
</xsl:if></xsl:模板>
基本上,每个产品都包含 3 个具有 true/false 值的属性.
value1 = true值 2 = 假值 3 = 真
现在我将一个参数传递给我的样式表,该参数将是这些值之一,即 value3.
我想选择所有 value3 设置为 true 的节点.类似的东西:
关于如何实现这一点的任何想法?
我相信你正在寻找这样的东西:
请注意,很少需要使用 for-each
.考虑以下样式表:
<xsl:param name="type" select="'value3'"/><xsl:template match="/"><xsl:应用模板select="/*/product[@*[name()=$type and (.='true' or .='1')]]"/></xsl:模板><xsl:template match="product"><xsl:value-of select="@id"/><xsl:text>
</xsl:text></xsl:模板></xsl:stylesheet>
应用于此输入:
<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"/></产品>
产生:
346
I have the following 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.
value1 = true
value2 = false
value3 = true
Now I'm passing a parameter to my stylesheet which will be one of these values, i.e. value3.
I would like to select all nodes that have value3 set to 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. 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
这篇关于XSLT for-each 循环,基于变量过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!