Copied to clipboard

Flag this post as spam?

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


  • Daniel Horn 319 posts 344 karma points
    Dec 14, 2010 @ 15:13
    Daniel Horn
    0

    Count childs in <li> and make <li> if more than three

    Hi

    I'm trying to this:

    <ul>
    <li>display child content1 - display child content 2 - display child content 3</li>
    <li>Display child content 4 - display child content 5 - display child content 6</li>
    </ul>

    How do I do this in XSLT?

     

  • Daniel Horn 319 posts 344 karma points
    Dec 14, 2010 @ 15:17
    Daniel Horn
    0
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxml="urn:schemas-microsoft-com:xslt" 
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:umbraco.contour="urn:umbraco.contour" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets umbraco.contour PS.XSLTsearch ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
        <xsl:variable name="documentTypeAlias" select="string('Customer')"/>
    <xsl:template match="/">

      <div id="kundeliste"<class="buttons prev" href="#empty-anchor">left</a>
         <div class="viewport">
            <ul class="overview">
              <xsl:for-each select="$currentPage/* [name() = $documentTypeAlias and string(umbracoNaviHide) != '0']">
            <li>
            <span><span><img src="/imgs/kundelistePhone.png" width="58" height="84" /></span><strong><xsl:value-of select="customerHeadline"/></strong><xsl:value-of select="customerText"/></span>
            </li>
               </xsl:for-each>
      </ul></div>
        
    <class="buttons next" href="#empty-anchor"><span class="btn">right</span></a></div>
    </xsl:template>
    </xsl:stylesheet>

    This is my code untill now :) - now i just need some kind of logic counting childs and creating a new <li></li> with childs in when there is 3 childs.

    - Help :-)

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Dec 14, 2010 @ 15:59
    Chriztian Steinmeier
    0

    Shoot! I misread the whole thing, so I've removed the code I posted :-)

    Now I can see you're basically creating a 3-column table - take a look at my answer here:

    http://our.umbraco.org/forum/developers/xslt/11527-Problem-with-listing-HTLM-strcuture-with-XSLT

     

    /Chriztian 

  • Thijs Kuipers 43 posts 66 karma points
    Dec 14, 2010 @ 16:30
    Thijs Kuipers
    0

    Are you trying to create rows or columns? Either way, I would create an <ul> with a maximum of three <li> as long as there are nodes to process.

    The operator you're probably searching for is "mod" (often the % operator in other languages). It's the modulus, e.g. "4 mod 3" equals 1, "5 mod 3" equals 2, "6 mod 3" equals 0. So, every number divided by 3, for which the remainder is 0 ==> is a multiple of 3.

    Using the position() in XSLT, you can find out "where you are" in the nodeset (e.g. in the for-each loop) and insert an <ul> or <li> accordingly.

  • Daniel Horn 319 posts 344 karma points
    Dec 14, 2010 @ 16:34
    Daniel Horn
    0

    I just want a list of products with 4 nodes in each <li>.

    Chriztian: will check it out

    Thijs: I understand what you are saying, but i just can't figure out how to do it.. Will work on it :)

  • Thijs Kuipers 43 posts 66 karma points
    Dec 14, 2010 @ 16:50
    Thijs Kuipers
    0

    The ugly way:

    <ul>
    <xsl:for-each select="$currentPage/node">
        <!-- position() starts at 1, not 0 like a 'normal' index -->
        <xsl:if test="((position() - 1) mod 3) = 0">
            <!-- It's the 1st, 4th, 7th in the set-->
            <li>
        </xsl:if>
        <!-- Do your thing with the current node here -->
        <xsl:if test="(position() mod 3) = 0">
            <!-- It's the 3rd, 6th, 9th node in the set -->
            </li>
        </xsl:if>
    </xsl:for-each>
    </ul>
    

    Might give you errors on saving because it finds unclosed pairs of tags (the <li></li> pair that is). You could wrap them in CDATA sections to prevent those errors.

Please Sign in or register to post replies

Write your reply to:

Draft