xsltsharepoint-2013dataviewwebpart

XSL:for-each - How to add AND condition?


I have a SharePoint DVWP that is using a List as its Data Source. Within this list I two columns - LinkSection and SubSection

Within this DVWP I have "Sections" that group the items in this list. Up until now, each Section has only filtered by one column - see example of one section:

    <xsl:template name="dvt_1">
    <xsl:variable name="dvt_StyleName">Table</xsl:variable>
    <xsl:variable name="Rows1" select="/dsQueryResponse/Rows/Row[contains(@LinkSection,'Training')]"/>
        <div class="page_home" style=" height: 120px; overflow: hidden; z-index: 0;border:1px solid #CCCCCC;">
        <div class="cat_heading" style="padding: 5px 10px; text-align: left; ">
        <h2 style="font-weight:bold">
        <a title="Section 1">Section 1</a>
        </h2>
        </div>
            <div class="cat_imglinks" style=" height: 70px; overflow: hidden;">
            <div class="cat_img" style="text-align: left; vertical-align: middle;">
            <a> 
                 <span style="width: 78%; float: left;">Section 1 Text</span>
            </a>    
            </div> 
            <div class="panel cat_links">
                <ul>                                
                <xsl:for-each select="$Rows1">          
                <li><a href="{@Link}" title="{@Title}">
                <xsl:value-of select="@Title" disable-output-escaping="yes"/></a></li>          
                </xsl:for-each>
                </ul>
                </div>
            </div>
        </div>                  
    </xsl:template>

I have a request now to filter by two columns. I do not have much experience with XSLT so I am trying things I have seen on forums such as this with no luck.

So, I want to create an AND filter for two Rows - see one of the ways I have tried below. I have tried using the "|" operator in the xsl:for-each line but this seems to display all items meeting the $Rows1 result.

    <xsl:template name="dvt_1">
    <xsl:variable name="dvt_StyleName">Table</xsl:variable>
    <xsl:variable name="Rows1" select="/dsQueryResponse/Rows/Row[contains(@LinkSection,'Training')]"/>
    <xsl:variable name="Rows2" select="/dsQueryResponse/Rows/Row[contains(@SubSection,'Education')]"/>
        <div class="page_home" style=" height: 120px; overflow: hidden; z-index: 0;border:1px solid #CCCCCC;">
        <div class="cat_heading" style="padding: 5px 10px; text-align: left; ">
        <h2 style="font-weight:bold">
        <a title="Section 1">Section 1</a>
        </h2>
        </div>
            <div class="cat_imglinks" style=" height: 70px; overflow: hidden;">
            <div class="cat_img" style="text-align: left; vertical-align: middle;">
            <a> 
                 <span style="width: 78%; float: left;">Section 1 Text</span>
            </a>    
            </div> 
            <div class="panel cat_links">
                <ul>                                
                <xsl:for-each select="$Rows1 | $Rows2">         
                <li><a href="{@Link}" title="{@Title}">
                <xsl:value-of select="@Title" disable-output-escaping="yes"/></a></li>          
                </xsl:for-each>
                </ul>
                </div>
            </div>
        </div>                  
    </xsl:template>

Any help would be greatly appreciated. Thanks!


Solution

  • You currently have 2 variables:

    <xsl:variable name="Rows1" select="/dsQueryResponse/Rows/Row[contains(@LinkSection,'Training')]"/>
    <xsl:variable name="Rows2" select="/dsQueryResponse/Rows/Row[contains(@SubSection,'Education')]"/>
    

    Instead of adding 'Rows2', just change 'Rows1' to:

    <xsl:variable name="Rows1" select="/dsQueryResponse/Rows/Row[contains(@LinkSection,'Training')][contains(@SubSection,'Education')]"/>
    

    Then change your for-each back to:

    <xsl:for-each select="$Rows1">