Avatar of springy143
springy143
 asked on

XSLT Grouping distinct values

I have some xml and I want to group distinct by year and weekno. All the the attributes are in the same elements like this example.

<items>
<item id="2" name="kent" type="region">
      <item  id="07/05/2007" type="datesInfo" weekNo="19" year="2007" />
      <item  id="09/05/2007" type="datesInfo" weekNo="19" year="2007" />
      <item  id="02/02/2011" type="datesInfo" weekNo="5" year="2011" />
      <item  id="04/02/2011"  type="datesInfo" weekNo="5" year="2011" />
      <item  id="22/07/2011"  type="datesInfo" weekNo="29" year="2011" />
      <item  id="02/02/2013"  type="datesInfo" weekNo="5" year="2013" />

Open in new window

</items>

Output would look like this
2007 - 19
2011 - 5, 29
2013 - 5

I am trying to use the Muenchian method. Here is what I have so far. Below script just gets the unique years. I cant get the weeknumbers in a distinct group by.

  <xsl:key name="years" match="item" use="@year"/> 
 


<xsl:template match="//item">
    <ul>
    <xsl:for-each select="item[generate-id() = generate-id(key('years',@year)[1])]">
      <li>
      <xsl:value-of select="@year"/>
      </li>
    </xsl:for-each>
    </ul>
 </xsl:template>

Open in new window

XMLWeb Languages and StandardsJavaScript

Avatar of undefined
Last Comment
Gertone (Geert Bormans)

8/22/2022 - Mon
Gertone (Geert Bormans)

Well, you started well, but then you need a nested Muenchian
For performance I put a compound key, rather than a predicate on the key result

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:key name="years" match="item" use="@year"/> 
    <xsl:key name="weeks" match="item" use="concat(@year,@weekNo)"/> 
    
    
    
    <xsl:template match="items">
        <ul>
            <xsl:for-each select="item/item[generate-id() = generate-id(key('years',@year)[1])]">
                <li>
                    <xsl:variable name="this-year" select="@year"/>
                    <xsl:value-of select="$this-year"/>
                    <xsl:text>: </xsl:text>
                    <xsl:for-each select="ancestor::items/item/item[generate-id() = generate-id(key('weeks',concat($this-year,@weekNo))[1])]">
                        <xsl:if test="not(position() = 1)">
                            <xsl:text>, </xsl:text>
                        </xsl:if>
                        <xsl:value-of select="@weekNo"/>
                    </xsl:for-each>
                        
                </li>
            </xsl:for-each>
        </ul>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window

springy143

ASKER
Thank you. XSL is very difficult. Ive now just got to put date in there aswell. Is it just another nested for-each loop.
Gertone (Geert Bormans)

you can add yet another key and yet another for-each indeed,

look at how I did the variable to fix one degree of freedom in the concatenated key
you will need to do a similar thing in the deepest nesting
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
springy143

ASKER
<xsl:key name="years" match="item" use="@year"/>
  <xsl:key name="weeks" match="item" use="concat(@year,@weekNo)"/>
  <xsl:key name="dates" match="item" use="concat(@year, @weekNo, @id)"/>

 <xsl:template match="//item">
    <ul>
      <xsl:for-each select="item[generate-id() = generate-id(key('years',@year)[1])]">
        <li>
          <xsl:variable name="this-year" select="@year"/>
          <xsl:value-of select="$this-year"/>
          <xsl:text>: </xsl:text>
          <xsl:for-each select="ancestor::items/item/item[generate-id() = generate-id(key('weeks',concat($this-year,@weekNo))[1])]">
            <xsl:if test="not(position() = 1)">
              <xsl:text>, </xsl:text>
            </xsl:if>
            <xsl:variable name="this-week" select="@weekNo"/>
            <xsl:value-of select="$this-week"/>
            <xsl:text>  </xsl:text>
            <xsl:for-each select="ancestor::items/item/item[generate-id() = generate-id(key('dates',concat($this-week, @id))[1])]">
              <xsl:if test="not(position() = 1)">
                <xsl:text>, </xsl:text>
              </xsl:if>
              <xsl:value-of select="@id"/>
              
            </xsl:for-each>
          </xsl:for-each>
        </li>
      </xsl:for-each>
    </ul>
  </xsl:template>

Open in new window


This is what I have going on your assistance. Im very close. Is my compound key correct?

 <xsl:key name="dates" match="item" use="concat(@year, @weekNo, @id)"/>

Open in new window


Cheers.
ASKER CERTIFIED SOLUTION
Gertone (Geert Bormans)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Gertone (Geert Bormans)

on the match attribute of your template

   <xsl:template match="//item">
should be

   <xsl:template match="item">

but since you are dealing with the parent item
I would do this
   <xsl:template match="item[item]">
(an item that has an item)

but I would go from the <items> as in my first example
springy143

ASKER
Thank you for all your wonderful help. Orignally was doing it with nested elements, I ran into trouble with my breadcrumb so felt this way is the best approach. I stumbled upon for-each-group for xslt 2.0 and found there was little support in the big five browser for it which I thought was odd because if there is one technology I would say has a big place in future it would be xslt. Im sure the browsers will support it natively eventually. Thanks again.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Gertone (Geert Bormans)

welcome

I think there is little chance that browsers will eventually support XSLT2. XSLT2 is pretty complex, and it took them ages to get the XSLT1 support in line with the specs. There are various reasons why I think it will not happen.
But for now, at least we have XSLT1 reasonably working cross browser, so that is a good point

Your task would have been easier with for-each-group, that is true.
If you ever run into performance issues, you can do some server side XSLT that does grouping early on....could save some memory on handhelds if necessary