#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    1
    Rep 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...!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Posts
    577
    Rep Power
    59
    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))]>

IMN logo majestic logo threadwatch logo seochat tools logo