XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 3rd, 2011, 04:19 AM
s34n s34n is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 1 s34n User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 52 sec
Reputation Power: 0
Question XSLT Grouping based on params

Hi There,

I have an example which im very close on finishing. I have an XML file, and I want the event tags to be grouped and sorted by start_date. I have done this part!

What I want, is to pass in two paramters into the XSLT using C#, this will populate the two param tags I have in the XSLT (done this bit too)

What I need to do base my key match and template match on the param tags. I've read around, and I noticed that this can;t be done in the match attribute, is there another way I can achieve this? I have included some sample xml and xslt, thanks!

XML File
Code:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="styletest.xsl"?>
<theme>
	<styles>		
		<style id="styleid">
		<title>Test Style 1</title>
			<elements>
				<element id="elementid1">
					<title>Test Element 1</title>	
					<events>
						<event id="eventid1" start_date="2008-10-13">
							<title>Test Event 1</title>
						</event>						
						<event id="eventid2" start_date="2009-03-18">
							<title>Test Event 2</title>
						</event>
						<event id="eventid3" start_date="2009-02-26">
							<title>Test Event 3</title>
						</event>
							<event id="eventid4" start_date="2011-04-12">
							<title>Test Event 4</title>
						</event>
							<event id="eventid5" start_date="2010-01-01">
							<title>Test Event 5</title>
						</event>
							<event id="eventid6" start_date="2010-07-06">
							<title>Test Event 6</title>
						</event>
					</events>											
				</element>				
			</elements>				
		</style>	
	</styles>
</theme>



XSLT File
Code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt">
  <xsl:param name="pStyle">styleid</xsl:param>
  <xsl:param name="pElement">elementid1</xsl:param>


  <!-- Create a key based on the YYYY portion of the start_date attribute -->
  <xsl:key
    name="eventsByYear"
    match="theme/styles/style[@id='styleid']/elements/element[@id='elementid1']/events/event"
    use="substring(@start_date, 1, 4)"
  />


  <xsl:template match="/">
    <xsl:apply-templates select="theme/styles/style[@id='styleid']/elements/element[@id='elementid1']/events">
    </xsl:apply-templates>
  </xsl:template>


  <xsl:template match="events">
      <xsl:for-each select="event[count(. | key('eventsByYear',substring(@start_date, 1, 4))[1]) = 1]">
        <xsl:sort select="substring(@start_date, 1, 4)" order="descending" />
        <h2>
            <xsl:value-of select="substring(@start_date, 1, 4)" />
        </h2>
            <xsl:for-each select="key('eventsByYear', substring(@start_date, 1, 4))">
              <xsl:sort select="title" />
              <h3>
                  <xsl:value-of select="title" />
              </h3>
            </xsl:for-each>
      </xsl:for-each>
  </xsl:template>


</xsl:stylesheet>


Thanks for your help in advance...!

Reply With Quote
  #2  
Old June 3rd, 2011, 02:48 PM
jkmyoung jkmyoung is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Posts: 577 jkmyoung User rank is Sergeant Major (2000 - 5000 Reputation Level)jkmyoung User rank is Sergeant Major (2000 - 5000 Reputation Level)jkmyoung User rank is Sergeant Major (2000 - 5000 Reputation Level)jkmyoung User rank is Sergeant Major (2000 - 5000 Reputation Level)jkmyoung User rank is Sergeant Major (2000 - 5000 Reputation Level)jkmyoung User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 10 m 5 sec
Reputation Power: 58
http://www.jenitennison.com/xslt/grouping/muenchian.html

I'm a little rusty, but without using keys I think it's something like:
Code:
<xsl:template match="events">
      <xsl:for-each select="event[not (substring(@start_date, 1, 4) = substring(preceding-sibling::event/@start_date, 1, 4))]>

...
<xsl:for-each select="following-sibling::event[substring(@start_date, 1, 4) = substring(current-node()/@start_date, 1, 4))]>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > XSLT Grouping based on params

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap