Copied to clipboard

Flag this post as spam?

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


  • Wasim 14 posts 34 karma points
    Feb 12, 2011 @ 16:23
    Wasim
    0

    How to Group & Sort nodes alphabetically

    Hi,

    A total newbie to Umbraco and XSLT.
    I searched the forum and found this http://wifi.umbraco.org/forum/developers/xslt/5975-String-manipulation
    However my situation is slightly different from the one in that post. And also I literally copied the exact code as Criztian had put but didn't work for me. So it's best I just create a fresh post regarding my situation.

    Right, to cut the long story short. Here is what I want. I have a node called Educations which has child nodes of the various courses/educations. I simply want to show all the courses in groups of alphabet and also sort them. So something like this:

    A
     Course Name
     Course Name
     Course Name

    B
     Course Name
     Course Name
     Course Name

    ...

    and the list continues with all the alphabets however if there is no course that starts with a certain alphabet like say "X" then it shouldn't show an empty group. I'm pretty sure this is possible with xslt but have no idea how to go about doing it. So you gurus out there, pleeeeeeease help a newbie out. :)

    Thanks,
    Wasim

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Feb 12, 2011 @ 21:19
    Chriztian Steinmeier
    0

    Hi Wasim,

    This should work for you - you need to change the educations variable to point to your Educations node.

    You should also change "Course" to the name of the Document Type of your individual courses.

    If you're not using @nodeName for the course names you should change those too.

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="umbraco.library"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
    
        <xsl:variable name="educations" select="$currentPage/PATH-TO/Educations" />
    
        <!-- Index Course documents by 1st letter in @nodeName (or other property, e.g. courseTitle) -->
        <xsl:key name="nodes" match="Educations/Course" use="substring(@nodeName, 1, 1)" />
    
        <xsl:template match="/">
            <xsl:for-each select="$educations/Course[count(. | key('nodes', substring(@nodeName, 1, 1))[1]) = 1]">
                <xsl:sort select="@nodeName" />
    
                <!-- Header -->
                <h2><xsl:value-of select="substring(@nodeName, 1, 1)" /></h2>
    
                <ul>
                    <!-- Items -->
                    <xsl:apply-templates select="key('nodes', substring(@nodeName, 1, 1))" mode="item">
                        <xsl:sort select="@nodeName" />
                    </xsl:apply-templates>
                </ul>
            </xsl:for-each>
        </xsl:template>
    
        <!-- Template for individual items -->
        <xsl:template match="Course" mode="item">
            <li>
                <xsl:value-of select="@nodeName" />
            </li>
        </xsl:template>
    
    
    </xsl:stylesheet>
    

    /Chriztian

  • Wasim 14 posts 34 karma points
    Feb 14, 2011 @ 10:07
    Wasim
    0

    Hi Chriztian,

    I tried your code but still there is no ouptut. Here is my code:

     

    version="1.0" encoding="UTF-8"?>
    DOCTYPE xsl:stylesheet [ ENTITY nbsp " "> ]>
    <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:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary PS.XSLTsearch ">

    <xsl:output method="xml" omit-xml-declaration="yes" />

    <xsl:param name="currentPage"/>

    <xsl:variable name="educations" select="$currentPage/ancestor-or-self::root//* [@nodeName='Educations']" />
       

    <xsl:key name="nodes" match="Educations/Textpage" use="substring(@nodeName, 1, 1)" />

    <xsl:template match="/">

    <xsl:for-each select="$educations/Textpage[count(. | key('nodes', substring(@nodeName, 1, 1))[1]) = 1]">  
      <xsl:sort select="@nodeName" />    
         
      <h2><xsl:value-of select="substring(@nodeName, 1, 1)" />h2>  
      <ul>
        
        <xsl:apply-templates select="key('nodes', substring(@nodeName, 1, 1))" mode="item">   
          <xsl:sort select="@nodeName" />  
        xsl:apply-templates>
      ul>        
    xsl:for-each>

    xsl:template>

    <!-- Template for individual items -->
    <xsl:template match="Textpage" mode="item">                
      <li
        <xsl:value-of select="@nodeName" />           
      </li>      
    </xsl:template>



    xsl:stylesheet>

    What I don't understand is where you have  <xsl:key name="nodes" match="Educations/Textpage" use="substring(@nodeName, 1, 1)" /> what values are in "match" is it ParentNodeName/ChildDocType?

    The Educations node and the child nodes(inidividual courses) are of document type "Textpage" so wherever you had put Courses I replaced it with Textpage as per your instructions

    Hope I can get a quick response.

    Thanks,
    Wasim 

  • Wasim 14 posts 34 karma points
    Feb 19, 2011 @ 16:39
    Wasim
    0

    Finally got it to work. Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft