簡體   English   中英

XSLT for-each循環,基於變量的過濾

[英]XSLT for-each loop, filter based on variable

我有以下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>

基本上,每個產品都包含3個具有true / false值的屬性。

value1 = true
value2 = false
value3 = true

現在,我將一個參數傳遞給樣式表,該參數將是這些值之一,即value3。

我想選擇將value3設置為true的所有節點。 類似於以下內容:

<xsl:for-each 
    select="$products/Product [$type = 'true' (or $type = '1' in XSLT terms)]">

關於如何實現這一目標的任何想法?

我相信您正在尋找這樣的東西:

<xsl:for-each select=
    "$products/Product[@*[name()=$type and (.='true' or .='1')]]">

請注意,很少需要使用for-each 考慮以下樣式表:

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

應用於此輸入:

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

產生:

3
4
6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM