Link to home
Start Free TrialLog in
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

Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

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

Avatar of springy143
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.
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
<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
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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