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

    Join Date
    Jun 2011
    Rep Power

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

    XSLT File
    <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 -->
        use="substring(@start_date, 1, 4)"
      <xsl:template match="/">
        <xsl:apply-templates select="theme/styles/style[@id='styleid']/elements/element[@id='elementid1']/events">
      <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" />
                <xsl:value-of select="substring(@start_date, 1, 4)" />
                <xsl:for-each select="key('eventsByYear', substring(@start_date, 1, 4))">
                  <xsl:sort select="title" />
                      <xsl:value-of select="title" />
    Thanks for your help in advance...!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Rep Power

    I'm a little rusty, but without using keys I think it's something like:
    <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