【问题标题】:XSLT for-each loop, filter based on variableXSLT for-each 循环,基于变量过滤
【发布时间】:2011-04-18 21:17:01
【问题描述】:

我有以下 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 个具有真/假值的属性。

value1 = true
value2 = false
value3 = true

现在我将一个参数传递给我的样式表,该参数将是这些值之一,即 value3。

我想选择所有 value3 设置为 true 的节点。大致如下:

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

关于如何实现这一点的任何想法?

【问题讨论】:

    标签: xml xslt xpath umbraco


    【解决方案1】:

    我相信您正在寻找这样的东西:

    <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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多