Copied to clipboard

Flag this post as spam?

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


  • Sherry Ann Hernandez 320 posts 344 karma points
    Dec 21, 2010 @ 08:31
    Sherry Ann Hernandez
    0

    Help in grouping the node in xslt

    Hi guys,

    I have this xslt wherein I list all the available spa treatments. I wanted to list it according to group. I try using keys but id doesn't display any result. For now this is the sample xslt that I have.

     

    <xsl:variable name="nodeID" select="umbraco.library:RequestQueryString('nodeID')"/>
    <xsl:template match="/">

    <xsl:for-each select="umbraco.library:GetXmlNodeById($nodeID)/* [@isDoc]">
           <xsl:sort select="treatmentCategory" order="ascending" />
           <h2 class="subhead"><xsl:value-of select="treatmentCategory"/></h2>  
           <div class="contentSub">
         <div class="treatmentMain">
              <div class="treatment"><h3 class="treatmentHead"><xsl:value-of select="treatmentName"/></h3></div>
              <div class="treatmenttime"><xsl:value-of select="priceRange"/></div>
              <div class="clear">&nbsp;</div>
              <xsl:value-of select="treatmentDescription" disable-output-escaping="yes"/>
         </div>
           </div>  
    </xsl:for-each>

     

  • Rich Green 2246 posts 4008 karma points
    Dec 21, 2010 @ 09:41
    Rich Green
    0

    Hey Sherry,

    Is the problem that the data isn't displaying, or just that it's not sorting correctly?

    Rich

  • Sherry Ann Hernandez 320 posts 344 karma points
    Dec 21, 2010 @ 10:14
    Sherry Ann Hernandez
    0

    Actually the data is displaying and it is already sorted. But I wanted to display the <h2 class="subhead"><xsl:value-of select="treatmentCategory"/></h2> only once because it's how I want to group my listing.

    I want it to display like this

    Massages -> This is the treatment category

     Back Massage

     Foot Massage

     

    Because now it is displaying my data as

    Massages

       Back Massage

    Massages

       Foot Masssage

     

     

  • Rich Green 2246 posts 4008 karma points
    Dec 21, 2010 @ 10:18
    Rich Green
    0

    Hi Sherry,

    Just add something like this

    <xsl:if test="position()=1">
        <h2 class="subhead"><xsl:value-of select="treatmentCategory"/></h2>
    </xsl:if> 

    Rich

  • Sherry Ann Hernandez 320 posts 344 karma points
    Dec 21, 2010 @ 10:21
    Sherry Ann Hernandez
    0

    What if I have another grouping? Like this

    Massages

       Back Massage

    Massages

       Foot Masssage

    Treatment

       Facial Treatment

    Treatment

      Hair Spa

     

    And  I wanted it to display like

    Massages

       Back Massage

       Foot Massage

    Treatment

       Facial Treatment

       Hair Spa  

  • Rich Green 2246 posts 4008 karma points
    Dec 21, 2010 @ 10:38
    Rich Green
    0

    Hi Sherry,

    It would help if you posted an image of your content structure.

    Rich

  • Sherry Ann Hernandez 320 posts 344 karma points
    Dec 21, 2010 @ 12:45
    Sherry Ann Hernandez
    0

    This is the layout of my node.

    In my page there is a button for spa treatments. When the user clicks it I will display a lightbox that list all the treatments under serenity spa and group it according to the treatment category that I defined.

    The code I posted above just display all the treatments (e.g. Express back massage) sorted according to treatment category. But what I want is to display it on my page like

    Massages

         Express Back Massage

         Express Legs Massage

  • Rich Green 2246 posts 4008 karma points
    Dec 21, 2010 @ 12:49
    Rich Green
    0

    Hey Sherry,

    I'm not sure if it's too late for you, however it might have been better to have a structure like this:

     

    - Spa

    - Massage

    Back Massage

    Head Massage

    - Treatment

    - Mens Facial

    - Womes Facial

    I'm not sure how to solve it using the structure you have right now, where is the list of Treatment Categories coming from?

    Rich

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 21, 2010 @ 14:18
    Tom Fulton
    0

    Hi,

    You should be able to do this using keys to get a unique list of Categories, then use another loop to loop through all services of that category.  Here's a quick example:

    <xsl:key name="cat" match="* [@isDoc]" use="treatmentCategory"/>
        
    <xsl:template match="/">

    <!-- start writing XSLT -->
    <xsl:for-each select="$currentPage/* [@isDoc][generate-id() = generate-id(key('cat', treatmentCategory)[1])]">
      <h2>
        <xsl:value-of select="treatmentCategory"/>
      </h2>

      <xsl:for-each select="$currentPage/* [@isDoc][treatmentCategory = current()/treatmentCategory]">
        <xsl:value-of select="treatmentName"/>
        <xsl:value-of select="priceRange"/>
        .... other fields ....
      </xsl:for-each>
      
    </xsl:for-each>
    </xsl:template>

    If not using currentPage you'll need to adjust the xpath accordingly.

    Note there are probably cleaner ways to write this with xsl:templates but I haven't experimented yet :)

  • Sherry Ann Hernandez 320 posts 344 karma points
    Dec 21, 2010 @ 15:26
    Sherry Ann Hernandez
    0

    Thanks for that. :D

    Though one question, why is it it's not listing all the nodes? it just display 2 treatmentCategory but when I don't use this keys all the treatmentCategory displays accordingly.

    Here's my code.

     

    <xsl:variable name="nodeID" select="umbraco.library:RequestQueryString('nodeID')"/>
    <xsl:key name="cat" match="* [@isDoc]" use="treatmentCategory"/>
    <xsl:template match="/">
    <div class="header">List of Spa Treatments</div>
    <xsl:for-each select="umbraco.library:GetXmlNodeById($nodeID)/* [@isDoc][generate-id() = generate-id(key('cat', treatmentCategory)[1])]">
             <xsl:sort select="treatmentCategory" order="ascending" />     
     <h2 class="subhead"><xsl:value-of select="umbraco.library:GetPreValueAsString(treatmentCategory)"/></h2>
             <xsl:for-each select="umbraco.library:GetXmlNodeById($nodeID)/* [@isDoc][treatmentCategory = current()/treatmentCategory]">
     <div class="contentSub">
         <div class="treatmentMain">
              <div class="treatment"><h3 class="treatmentHead"><xsl:value-of select="treatmentName"/></h3></div>
              <div class="treatmenttime"><xsl:value-of select="priceRange"/></div>
              <div class="clear">&nbsp;</div>
              <xsl:value-of select="treatmentDescription" disable-output-escaping="yes"/>
         </div>
           </div>  
           </xsl:for-each>
    </xsl:for-each>
  • Tom Fulton 2030 posts 4998 karma points c-trib
    Dec 21, 2010 @ 15:32
    Tom Fulton
    0

    Hmm...are the other treatmentCategory's actually in use, like specified on some of the services nodes?  If not they won't show up

Please Sign in or register to post replies

Write your reply to:

Draft