Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fuji Kusaka 2203 posts 4220 karma points
    Mar 26, 2012 @ 20:47
    Fuji Kusaka
    0

    Displaying a Div only once in xslt

    Hi all,

    I cant get a way out of this here. I have a list of items and the max list to be displayed is 6. 

    So what i need to achieve here is display the 1st item in a left floating div with all the title, description, images and the other 5 items in another div container with id="listChild" under which the content will display in UL list.

    But can get this working since it its repeating the div id within it. How can i possilby write the div outside the ul list once only?

     <xsl:for-each select="umbraco.library:GetXmlNodeById($Items)/* [@isDoc and string(umbracoNaviHide) != '1']">
        <xsl:sort select="@createDate" order="descending"/>
        <xsl:if test="position() &lt;= $MaxItems" >
          <xsl:choose>      
          <xsl:when test="position() = 1">        
                 <div id="newsSum">
                    <h1<href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></h1>
                 </div>
          </xsl:when>
         
        <xsl:otherwise>
          <div id="listChild">
          <ul>
                <li class="newsTitSum"><href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></li>
                <li class="newsDate"><xsl:value-of select="umbraco.library:FormatDateTime(@createDate, 'dd MMMM yyy')"/></li>
                <li class="newsTxtSum"<xsl:value-of select="umbraco.library:TruncateString(ItemDesc, 100, '...')"/></li>
            </ul>
          </div>
        </xsl:otherwise     
    </xsl:choose>        
        </xsl:if>  
    </xsl:for-each>



    Here the div id childList should appear only once.

     

     

     

     

     

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Mar 27, 2012 @ 01:17
    Chriztian Steinmeier
    1

    Hi Fuji,

    This gets a little messy but it'll work all the while I'm sure you can understand what's happening:

    <!-- Always cache stuff ! -->
            <xsl:variable name="items" select="umbraco.library:GetXmlNodeById($Items)/*[@isDoc][not(umbracoNaviHide = 1)]" />
    
            <xsl:for-each select="$items">
                <xsl:sort select="@createDate" order="descending" />
                    <xsl:choose>
                        <xsl:when test="position() = 1">
                            <div id="newsSum">
                                <h1><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a></h1>
                            </div>
                        </xsl:when>
                        <xsl:when test="position() = 2">
                            <div id="listChild">
                                <xsl:for-each select="$items">
                                    <xsl:sort select="@createDate" order="descending" />
                                    <xsl:if test="position() &gt;= 2 and position() &lt;= $MaxItems">
                                        <ul>
                                            <li class="newsTitSum"><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a></li>
                                            <li class="newsDate"><xsl:value-of select="umbraco.library:FormatDateTime(@createDate, 'dd MMMM yyy')" /></li>
                                            <li class="newsTxtSum"><xsl:value-of select="umbraco.library:TruncateString(ItemDesc, 100, '...')" /></li>
                                        </ul>
                                    </xsl:if>
                                </xsl:for-each>
                            </div>
                        </xsl:when>
                    </xsl:choose>
                </xsl:if>
            </xsl:for-each>

    What you really should do, is to pre-sort the $items with a for-each inside a variable where you store the ids in simple elements. Then you can first ask for the 1st element and then for the rest - something like this:

      <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
        <xsl:variable name="items" select="umbraco.library:GetXmlNodeById($Items)/*[@isDoc][not(umbracoNaviHide = 1)]" />
    
        <!-- Build a sorted + filtered set -->
        <xsl:variable name="filteredProxy">
            <xsl:for-each select="$items">
                <xsl:sort select="@createDate" order="descending" />
                <xsl:if test="position() &lt;= $MaxItems">
                    <nodeId><xsl:value-of select="@id" /></nodeId>
                </xsl:if>
            </xsl:for-each>
        </xsl:variable>
        <xsl:variable name="filteredNodes" select="make:node-set($filteredProxy)" />
    
        <xsl:template match="/">
            <xsl:apply-templates select="$items[@id = $filteredNodes[1]]" mode="header" />
    
            <xsl:for-each select="$filteredNodes[position() &gt; 1]">
                <xsl:apply-templates select="$items[@id = current()]" mode="items" />
            </xsl:for-each>
        </xsl:template>
    
        <xsl:template match="*[@isDoc]" mode="header">
            <div id="newsSum">
                <h1><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a></h1>
            </div>
        </xsl:template>
    
        <xsl:template match="*[@isDoc]" mode="items">
            <ul>
                <li class="newsTitSum"><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName" /></a></li>
                <li class="newsDate"><xsl:value-of select="umbraco.library:FormatDateTime(@createDate, 'dd MMMM yyy')" /></li>
                <li class="newsTxtSum"><xsl:value-of select="umbraco.library:TruncateString(ItemDesc, 100, '...')" /></li>
            </ul>
        </xsl:template>

     

    /Chriztian

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies