Marko
Marko

Reputation: 72222

XSLT for-each loop, filter based on variable

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}&amp;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?

Upvotes: 1

Views: 10131

Answers (1)

Wayne
Wayne

Reputation: 60414

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>&#xA;</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

Upvotes: 7

Related Questions