Copied to clipboard

Flag this post as spam?

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


  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Oct 14, 2011 @ 12:32
    Bo Damgaard Mortensen
    0

    Render comma separated string into several uls

    Hi all,

    I'm having a bit of trouble here to get the right output from an xslt file.

    I have a comma separated string which I want to split into multiple unordered lists. After ever 5th value I want to make a new <ul>

    Has anyone done this before? :-) If so, I'd be greatful to get a hint!

    I've tried with Chriztians example to split values into rows and columns for a table, but with no luck :-/

    Thanks a lot in advance!

  • Rich Green 2246 posts 4006 karma points
    Oct 14, 2011 @ 12:43
    Rich Green
    0

    Hey Bo,

    Here's some XSLT which you should be able to tweak to get working, it's a concept here as I had some other non related stuff going on, but I have it working perfectly.

    Hope it helps

     

    <xsl:variable name="maxItems" value="5">

        <xsl:template>
        <ul>
            <xsl:apply-templates select="$currentPage/ancestor-or-self::*[@level=1]/descendant::Country[@isDoc]">
            </xsl:apply-templates>
        </ul>
      </xsl:template>
     
      <xsl:template match="Country[@isDoc]">

       

        <li>
          <a href="{umbraco.library:NiceUrl(@id)}">
            <xsl:value-of select="(pageTitle | @nodeName[not(normalize-space(../pageTitle))])[1]" /> 
          </a>
        </li>
     
        <xsl:if test="position() mod $maxItems = 0 and position() != last()">
          <xsl:text disable-output-escaping="yes"><![CDATA[</ul>]]></xsl:text>
    <xsl:choose>
    <xsl:when test="position() != last()">
    <xsl:text disable-output-escaping="yes"><![CDATA[<ul>]]></xsl:text>
    </xsl:when>
    </xsl:choose>
        </xsl:if>
     </xsl:template>
     
    Rich

  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Oct 14, 2011 @ 14:04
    Bo Damgaard Mortensen
    0

    You're the man Rich! :) Worked perfect!

    Thanks a lot.

    / Bo

  • Chriztian Steinmeier 2726 posts 8320 karma points MVP 4x admin c-trib
    Oct 14, 2011 @ 14:42
    Chriztian Steinmeier
    1

    Hi Bo,

    Just for the benefit of having a "real" XSLT solution to this :-) - here's a self-contained stylesheet to demonstrate how it's done:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:variable name="data" select="'one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve'" />
        <xsl:variable name="groupSize" select="5" />
    
        <xsl:template match="/">
            <xsl:variable name="values" select="umb:Split($data, ',')//value" />
    
            <xsl:apply-templates select="$values[(position() mod $groupSize) = 1]" mode="group" />
        </xsl:template>
    
        <xsl:template match="value" mode="group">
            <ul>
                <xsl:apply-templates select=". | following-sibling::*[position() &lt; $groupSize]" mode="item" />
            </ul>
        </xsl:template>
    
        <xsl:template match="value" mode="item">
            <li>
                <xsl:value-of select="." />
            </li>
        </xsl:template>
    
    </xsl:stylesheet>
    

    /Chriztian

  • Rich Green 2246 posts 4006 karma points
    Oct 14, 2011 @ 15:18
    Rich Green
    0

    As I was posting I knew that Chriztian would come along with a 'real' solution :)

    Nice solution too!

    Rich

  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Oct 17, 2011 @ 19:26
    Bo Damgaard Mortensen
    0

    Mr. XSLT to the rescue! ;) Thanks a lot both of you!

  • 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