Subject: Re: [xsl] Need help with XSL and XPath From: "Jay Bryant" <jay@xxxxxxxxxxxx> Date: Tue, 23 May 2006 10:06:31 -0500 |
My bad. I rushed too much and thought too little. Sorry, Lassi, and thanks to Ken for a much better solution. Jay Bryant Bryant Communication Services ----- Original Message ----- From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx> To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx> Sent: Monday, May 22, 2006 11:42 PM Subject: Re: [xsl] Need help with XSL and XPath At 2006-05-23 01:48 +0300, Lassi Seppdld wrote: >I've got an XML document with multiple entries of <element> as shown >below. The <name>s are unique, but the <number> can be same for >mulpitle entries of <element>. The XML document isn't sorted in anyway. >... >Now I need the XSL stylesheet to print out something like this: > >number 1 > name1 > name4 > name7 > >number 25 > name2 > name3 > >number 100 > name5 > name6 > >So each "number X" is only printed once and then all the <element>s >that have the number X in the <number> tags are printed below that. >Then the next biggest number is printed and so on. At 2006-05-22 18:28 -0500, Jay Bryant wrote: >Here's one way to do it: >... > <xsl:for-each select="data/element[not(number = following::number)]"> Note that using axes is the slowest of the three methods of doing grouping: axes, variables and keys ... but that's not the objective of my post. > <xsl:sort select="number"/> The problem with the above is that the default data type is "text", so when I put Lassi's test data into Jay's structure, I get this incorrect result: T:\ftemp>xslt jay.xml jay.xsl con <?xml version="1.0" encoding="utf-8"?> <out> <entry number="1"> <name>Oscar</name> <name>Mike</name> </entry> <entry number="100"> <name>Tom</name> <name>Dick</name> <name>Harry</name> </entry> <entry number="25"> <name>Bob</name> <name>George</name> </entry> </out> T:\ftemp> When dealing with number values it is important to use: <xsl:sort select="number" data-type="number"/> in order for the values to not be treated as strings. I have this in one of my students' exercises (hockey standings) in order to trip them up in their reporting. So a complete solution, not using axes and not needing to have a variable would be as posted below. On large data sets this would work very much faster than using axes. I hope this helps. . . . . . . . . . . Ken T:\ftemp>type jay.xml <data> <element> <name>Oscar</name> <number>1</number> </element> <element> <name>Mike</name> <number>1</number> </element> <element> <name>Bob</name> <number>25</number> </element> <element> <name>George</name> <number>25</number> </element> <element> <name>Tom</name> <number>100</number> </element> <element> <name>Dick</name> <number>100</number> </element> <element> <name>Harry</name> <number>100</number> </element> </data> T:\ftemp>type jay.xsl <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:key name="nbr" match="element" use="number"/> <xsl:template match="/"> <out> <xsl:for-each select="data/element[generate-id(.)= generate-id(key('nbr',number)[1])]"> <xsl:sort select="number" data-type="number"/> <entry number="{number}"> <xsl:copy-of select="key('nbr',number)/name"/> </entry> </xsl:for-each> </out> </xsl:template> </xsl:stylesheet> T:\ftemp>xslt jay.xml jay.xsl con <?xml version="1.0" encoding="utf-8"?> <out> <entry number="1"> <name>Oscar</name> <name>Mike</name> </entry> <entry number="25"> <name>Bob</name> <name>George</name> </entry> <entry number="100"> <name>Tom</name> <name>Dick</name> <name>Harry</name> </entry> </out> T:\ftemp> -- Registration open for XSLT/XSL-FO training: Wash.,DC 2006-06-12/16 Also for XSL-FO/XSLT training: Minneapolis, MN 2006-07-31/08-04 Also for XML/XSLT/XSL-FO/UBL training: Varo,Denmark 06-09-25/10-06 World-wide corporate, govt. & user group UBL, XSL, & XML training. G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Aug'05 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Need help with XSL and XP, G. Ken Holman | Thread | [xsl] Passing variables into XSLT v, Duncan Anker |
RE: [xsl] XSLT2 processing question, Florent Georges | Date | [xsl] Re: WordML Question and norma, Jordan Soet |
Month |