Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 27th, 2010, 11:11 AM
Registered User
 
Join Date: Sep 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Grouping with Multiple Dates

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="widgets.xsl"?>

<events>
    <event>
        <title>Garrys Event</title>
        <date>2010-09-10</date>
        <date>2010-12-10</date>
    </event>
    
    <event>
        <title>Nicks Event</title>
        <date>2010-09-10</date>
        <date>2010-12-10</date>
    </event>

</events>
I want to group these events by each of the dates they have specified with the event.

So using the example above it would output

10th October 2010
Garrys Event
Nicks Event

10th December 2010
Garrys Event
Nicks Event


Thanks, sorry if I have not been specific enough.

Thanks

Garry
 
Old September 27th, 2010, 11:38 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is an XSLT 2.0 stylesheet:
Code:
<xsl:stylesheet
  version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  
  <xsl:output method="text"/>
  
  <xsl:template match="events">
    <xsl:for-each-group select="event" group-by="date/xs:date(.)">
      <xsl:sort select="current-grouping-key()"/>
      <xsl:value-of select="format-date(current-grouping-key(), '[D1o] [MNn] [Y]')"/>
      <xsl:text>
</xsl:text>
      <xsl:value-of select="current-group()/title" separator="
"/>
      <xsl:text>
</xsl:text>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>
You can use Saxon 9 or AltovaXML tools or XQSharp to run XSLT 2.0 stylesheets.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 27th, 2010, 01:09 PM
Registered User
 
Join Date: Sep 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Thankyou for your feedback.

Garrys Event
2010-09-10
2010-12-10



Nicks Event
2010-09-10
2010-12-10

This is the output im getting....is there some changes that can be made to make it output the following:

10th October 2010
Garrys Event
Nicks Event

10th December 2010
Garrys Event
Nicks Event

so that the events are grouped by the multiple dates instead of the event title?

Thanks
 
Old September 27th, 2010, 01:20 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Which XSLT 2.0 processor did you use? Did you use the code I posted? When I use AltovaXML Tools I get
Code:
10th December 2010
Garrys Event
Nicks Event
10th September 2010
Garrys Event
Nicks Event
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 27th, 2010, 01:20 PM
Registered User
 
Join Date: Sep 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry I am using just an ordinary web browser... I need it to work in a browser.
 
Old September 27th, 2010, 01:32 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

All of today's browsers support only XSLT 1.0. Grouping is much trickier in XSLT 1.0. You'll need to read up about Muenchian grouping - it's well covered in all textbooks.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 27th, 2010, 01:37 PM
Registered User
 
Join Date: Sep 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have tried to do a grouping from tutorials but i cant get it the way i want it...is there any advice you can offer me on how to do it?
 
Old September 27th, 2010, 01:53 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I have tried to do a grouping from tutorials but i cant get it the way i want it...is there any advice you can offer me on how to do it?

Only by writing a new tutorial, which would probably be no better than the ones you are failing to understand. Best to show us what you have tried, then we can see where you went wrong.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old September 27th, 2010, 01:59 PM
Registered User
 
Join Date: Sep 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="widgets.xsl"?>

<events>
	<event>
		<date>13th October 2010</date>
		<title>Drum and Bass Night</title>
	</event>
	<event>
		<date>13th October 2010</date>
		<title>Another Event</title>
	</event>
	<event>
		<date>12th December 2010</date>
		<title>Chirstmas Event</title>
	</event>
</events>
and the xsl

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="events-by-date" match="event" use="date" />
<xsl:template match="events">

<html>
<body>

	<xsl:for-each select="event[count(. | key('events-by-date', date)[1]) = 1]">
		<xsl:sort select="date" />
		<h2><xsl:value-of select="date" /></h2>
		<xsl:for-each select="key('events-by-date', date)">
			<p><xsl:value-of select="title" /></p>
		</xsl:for-each>
	</xsl:for-each>

</body>
</html>

</xsl:template>
</xsl:stylesheet>
This is very nearly there, but as soon as I add another <date> into an <event> it doesnt work.
 
Old September 27th, 2010, 03:40 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You want to group the dates, not the events. Something like this (untested)

Code:
          <xsl:for-each select="date[count(. | key('dates', .)[1]) = 1]">
		<xsl:sort select="." />
		<h2><xsl:value-of select="." /></h2>
		<xsl:for-each select="key('dates', .)">
			<p><xsl:value-of select="../title" /></p>
		</xsl:for-each>
	</xsl:for-each>
where the key has name="dates" match="date" use=".".
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference

Last edited by mhkay; September 27th, 2010 at 03:41 PM.. Reason: typo





Similar Threads
Thread Thread Starter Forum Replies Last Post
Grouping on Multiple Field nisargmca XSLT 2 May 10th, 2010 02:42 AM
grouping multiple xml files in one file bcogney XSLT 3 April 21st, 2006 03:58 AM
Multiple Grouping in List ROCXY XSLT 0 January 7th, 2006 05:16 AM
Grouping multiple paragraph styles under 1 parent ROCXY XML 0 January 4th, 2006 09:42 AM
access calendars multiple dates RobertCoats Access 0 June 23rd, 2005 02:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.