Donar Posted January 25, 2011 Share Posted January 25, 2011 Good day forum!There I am thinking I understood xsl:for-each and xsl:sort. A day later, after a zillion unsuccessful attempts I turn to you in search for help. Maybe you can help me with an approach I did not think of.I am looking to process an unsorted list having multiple identical keys into a sorted list, in which each key appears just once, or in other words, I want to eliminate all dupes, e.g.: <list><item><key>C</key>...</item><item><key>A</key>...</item><item><key>B</key>...</item><item><key>A</key>...</item></list> should transform to <list><item><key>A</key></item><item><key>B</key></item><item><key>C</key></item></list> (it does not matter which of the 2 "A" nodes is discarded.)Looking forward to your suggestions!Thanks in advance! Link to comment Share on other sites More sharing options...
Martin Honnen Posted January 25, 2011 Share Posted January 25, 2011 With XSLT 2.0 (as implemented by Saxon 9 or AltovaXML Tools or XQSharp) you can simply use <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output indent="yes"/> <xsl:template match="list"> <xsl:copy> <xsl:for-each-group select="item" group-by="key"> <xsl:sort select="current-grouping-key()"/> <item><xsl:copy-of select="key"/></item> </xsl:for-each-group> </xsl:copy> </xsl:template> </xsl:stylesheet> With XSLT 1.0 you can use Muenchian grouping <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output indent="yes"/> <xsl:key name="k1" match="item" use="key"/> <xsl:template match="list"> <xsl:copy> <xsl:for-each select="item[generate-id() = generate-id(key('k1', key)[1])]"> <xsl:sort select="key"/> <item><xsl:copy-of select="key"/></item> </xsl:for-each> </xsl:copy> </xsl:template> </xsl:stylesheet> Link to comment Share on other sites More sharing options...
Donar Posted January 26, 2011 Author Share Posted January 26, 2011 XSLT 1.0 it is.Thank you very much, it works for me. The final intention is to process an inner loop within the now possible outer loop:b:1a:2c:3a:4should givea:2, 4b:1c:3Then my inner loop looks like this:<xsl:for-each select="key('key', .)">and everything is absolutely fine.My 2 quite thick XML books did make no mention about generate-id() and key(), or even Muenchian grouping, so I did not know they existed (and hence had no idea about googling them). Can you recommend a good XSLT book (English or German)?Thanks again! Link to comment Share on other sites More sharing options...
Martin Honnen Posted January 26, 2011 Share Posted January 26, 2011 Check http://www.jenitennison.com/xslt/index.html, if you like the explanations there then you might like her books listed also on that page.And of course Michael Kay's XSLT and XPath references from Wrox (http://www.wrox.com/WileyCDA/WroxTitle/XSLT-2-0-and-XPath-2-0-Programmer-s-Reference-4th-Edition.productCd-0470192747.html for XSLT and XPath 2.0, http://www.wrox.com/WileyCDA/WroxTitle/XSL...0764543814.html for XSLT 1.0) surely list anything you ever need to know about XSLT and XPath but these are reference style books, not necessarily suitable for an introduction. Link to comment Share on other sites More sharing options...
Donar Posted January 26, 2011 Author Share Posted January 26, 2011 Ordered "XSLT and XPath On The Edge" and looking forward to it.Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.