I need some help creating an XSLT to group RSS-like data by unique category (sorted alphabetically) and then sort each item by date under each sorted category. (Yes I realize this will produce a Cartesian product).
Here is a trimmed down version of the XML source...
You are definately on the right track with using the key element. Unfortunately I don't have time to create a sample this weekend, but keep at it, because it's the right solution. Could you post what you have now?
Well Morten, the example I have above was over-simplified. I too don't have time to change my stylesheet to work with the simplified XML above, so I'll just show you what I have working now (the actual version).
This is an example of the actual source XML:
<?xml version="1.0"?> <root> <newsletter id="7"> <title>Technology Review Newsletter</title> <subject>Technology Review Newsletter</subject> <openingComment>This is the opening comment</openingComment> <closingComment>This is the closing comment. Something that the end user can input.</closingComment> <headlines> <headline id="664"> <source url="http://hosted.ap.org/">AP Top Business News At 2:25 p.m. EDT</source> <title url="http://hosted.ap.org/dynamic/stories/O/OIL_PRICES?SITE=MATAU&SECTION=HOME&TEMPLATE=DEFAULT" typeID="1">Oil continues slide below $60</title> <description>SIOUX FALLS, S.D. (AP) -- Oil prices slipped further below $60 a barrel Tuesday, as better-than-expected economic and earnings data struggled to support the market's early gains....</description> <pubDate>2009-07-14T00:00:00</pubDate> <postDate>2009-07-14T18:25:38.817</postDate> <discoveryDate>2009-07-14T18:25:21</discoveryDate> <updateDate>2009-07-15T16:00:56.767</updateDate> <category catid="81">Technology</category> <category catid="83">Business</category> </headline> <headline id="665"> <source url="http://hosted.ap.org/">AP Top Business News At 2:25 p.m. EDT</source> <title url="http://hosted.ap.org/dynamic/stories/U/US_SEC_CHAIRMAN?SITE=MATAU&SECTION=HOME&TEMPLATE=DEFAULT" typeID="1">SEC head outlines enforcement, other changes</title> <description>WASHINGTON (AP) -- The Securities and Exchange Commission has been revamping itself, buttressing enforcement efforts and taking a series of initiatives to protect investors in the wake of the financial crisis and massive Madoff fraud, the agency's chairman said Tuesday....</description> <pubDate>2009-07-12T00:00:00</pubDate> <postDate>2009-07-14T18:25:38.817</postDate> <discoveryDate>2009-07-14T18:25:21</discoveryDate> <updateDate>2009-07-14T18:25:38.817</updateDate> <category catid="81">Technology</category> <category catid="80">Healthcare</category> </headline> <headline id="666"> <source url="http://hosted.ap.org/">AP Top Business News At 2:25 p.m. EDT</source> <title url="http://hosted.ap.org/dynamic/stories/U/US_EARNS_JOHNSON__JOHNSON?SITE=MATAU&SECTION=HOME&TEMPLATE=DEFAULT" typeID="1">J&J posts lower 2Q profit, but still beats view</title> <description>TRENTON, N.J. (AP) -- Health care products maker Johnson & Johnson on Tuesday said its second-quarter profit fell 3.5 percent, as the global recession, weak dollar and generic competition took their toll on sales, particularly for prescription drugs....</description> <impactStatement>This is a comment placed in the headline by the business process owner.</impactStatement> <pubDate>2009-07-13T00:00:00</pubDate> <postDate>2009-07-14T18:25:38.833</postDate> <discoveryDate>2009-07-14T18:25:21</discoveryDate> <updateDate>2009-07-15T14:43:17.223</updateDate> <category catid="81">Technology</category> <category catid="80">Healthcare</category> </headline> </headlines> <emailGUID>{4554c650-2d8b-47b3-91dd-6fbda669e4c4}</emailGUID> <subscribeURL>http://estelle/NewsRoom3Dev/Feeds/NewsletterSubscriptions.asp</subscribeURL>; <impactStatementLabel>Comments</impactStatementLabel> </newsletter> </root>
This is the XSL stylesheet prototype I've written (as a proof of concept) that organizes all headlines by category. This is used to build an HTML email message.
<xsl:if test="closingComment"> <tr> <td style="padding-top:15px;"> <xsl:value-of select="closingComment" disable-output-escaping="yes"/> </td> </tr> </xsl:if> <tr> <td style="padding-top:15px;font-size:8pt;text-align:center;"> <br/> <a> <xsl:attribute name="href"> <xsl:value-of select='concat(subscribeURL, "?a=u&guid=", emailGUID)'/> </xsl:attribute> <xsl:attribute name="target"> <xsl:text>_blank</xsl:text> </xsl:attribute> <xsl:text>Unsubscribe</xsl:text> </a> <xsl:text> | </xsl:text> <a> <xsl:attribute name="href"> <xsl:value-of select='concat(subscribeURL, "?a=s&guid=", emailGUID)'/> </xsl:attribute> <xsl:attribute name="target"> <xsl:text>_blank</xsl:text> </xsl:attribute> <xsl:text>Subscribe to this Newsletter</xsl:text> </a> </td> </tr> <tr> <td style="padding-top:15px;font-size:8pt;text-align:center;"> <xsl:text>This e-mail and any files transmitted with it may contain privileged or confidential information. It is solely for use by the individual for whom it is intended, even if addressed incorrectly. If you received this e-mail in error, please notify the sender; do not disclose, copy, distribute, or take any action in reliance on the contents of this information; and delete it from your system. Any other use of this e-mail is prohibited. Thank you for your compliance.</xsl:text> </td> </tr> </table> </body> </html>
You gave me just what I needed. I filled in the blanks.
Here's my working prototype that groups each headline by alphabetically sorted category and then sorts each headline under each category by date (against the actual data, not the simplified version).
Help grouping and sorting by category
I need some help creating an XSLT to group RSS-like data by unique category (sorted alphabetically) and then sort each item by date under each sorted category. (Yes I realize this will produce a Cartesian product).
Here is a trimmed down version of the XML source...
What I would like to produce is something similar to this...
Business
2009-07-14: test3
Healthcare
2009-07-16: test1
2009-07-14: test3
2009-07-12: test2
Technology
2009-07-16: test1
2009-07-14: test3
I think you could probably make use of the xsl:key function, which is documented here http://www.w3schools.com/xsl/el_key.asp
But unfortunately I have not made use if this function myself yet but it looks to be pretty straightforward.
For sorting you can use <xsl:sort select="datefield" order="descending" />
Try giving it a go :)
/Jan
Yes, I tried to implement Jeni's Muenchian technique using xsl:key, found here...
http://www.jenitennison.com/xslt/grouping/muenchian.html
...but I could not figure out how to get it to work.
You are definately on the right track with using the key element. Unfortunately I don't have time to create a sample this weekend, but keep at it, because it's the right solution. Could you post what you have now?
Well Morten, the example I have above was over-simplified. I too don't have time to change my stylesheet to work with the simplified XML above, so I'll just show you what I have working now (the actual version).
This is an example of the actual source XML:
This is the XSL stylesheet prototype I've written (as a proof of concept) that organizes all headlines by category. This is used to build an HTML email message.
The problem is, I need to sort the headlines under each Category by pubDate. But I can't get it to work.
try this:
hth, Thomas
I forgot to mention that I also need to sort the category headings alphabetically then sort the headlines under each category by date descending.
Thank you so much Thomas!
You gave me just what I needed. I filled in the blanks.
Here's my working prototype that groups each headline by alphabetically sorted category and then sorts each headline under each category by date (against the actual data, not the simplified version).
Glad to see you got your problem solved.
Please mark Thomas' post as the solution so we can get this one closed down :)
Clad I could help
Thomas
is working on a reply...