[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Sorting problem

Subject: Re: Sorting problem
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 31 Aug 2005 21:36:44 +1000
xsl for each item
On 8/31/05, Joe Fawcett <joefawcett@xxxxxxxxxxx> wrote:
> Dear All
>
> Supposing I have a document such as below =>
> <root>
>  <item type="A" subType="1"/>
>  <item type="A" subType="2"/>
>  <item type="A" subType="4"/>
>  <item type="B" subType="2"/>
>  <item type="B" subType="3"/>
>  <item type="D" subType="1"/>
>  <item type="D" subType="2"/>
>  <item type="D" subType="3"/>
>  <item type="D" subType="4"/>
>  <item type="E" subType="2"/>
>  <item type="E" subType="4"/>
> </root>
>
> This is sorted by @type and then @subType, the subType can be any positive
> number, not just an integer.
>
> I need to select the first occurrence of each item, based on its @subType,
> then the second, then the third etc, keeping them in alphabetical order
> based on @type =>
>
> <root>
>  <item type="A" subType="1"/>
>  <item type="B" subType="2"/>
>  <item type="D" subType="1"/>
>  <item type="E" subType="2"/>
>  <item type="A" subType="2"/>
>  <item type="B" subType="3"/>
>  <item type="D" subType="2"/>
>  <item type="E" subType="4"/>
>  <item type="A" subType="4"/>
>  <item type="D" subType="3"/>
>  <item type="D" subType="4"/>
> </root>
>
> This is the result of converting a RTF via xx:node-set, I am stuck with
> version 1.0, so as far as I understand I cannot use keys in the solution.
>
>
> I have produced a working solution but it seems inelegant. I first select a
> distinct list of @type using pre-Muenchian methods where the type doesn't
> have a preceding-sibling of the same value. I then call a template by name
> recursively whereby the first pass iterates through the distinct list using
> for-each and selects the the first <item> that matches the type. This
> template is then called again and does the same for the second matching
item
> and continues in the same fashion. The recursion stops after being called
> sufficient times to ensure each item is matched once.
>
> Can anyone suggest anything more appealing?


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 exclude-result-prefixes="msxsl"
 >
	<xsl:output omit-xml-declaration="yes" indent="yes"/>

	<xsl:key name="kTypes" match="@type" use="."/>
	<xsl:key name="kItemByType" match="item" use="@type"/>

	<xsl:variable name="vDoc" select="/"/>

	<xsl:variable name="vrtfdistTypes">
	  <xsl:for-each select=
	    "/*/item/@type
	        [generate-id()
	        =
	         generate-id(key('kTypes', .)[1])
	         ]">
	     <type code="{.}" count="{count(key('kTypes', .))}"/>
    </xsl:for-each>
	</xsl:variable>

	<xsl:variable name="vdistTypes" select="msxsl:node-set($vrtfdistTypes)/*"/>

	<xsl:variable name="vmaxCount"
	 select="number($vdistTypes[not(@count &lt; $vdistTypes/@count)]/@count)"/>

	<xsl:variable name="vmaxCountTypecode"
	 select="$vdistTypes[not(@count &lt; $vdistTypes/@count)]/@code[1]"/>

	<xsl:template match="/">
	 <items>
	  <xsl:for-each select="key('kItemByType', $vmaxCountTypecode)">
	    <xsl:variable name="vcurTuple" select="position()"/>
	    <xsl:for-each select="$vdistTypes">
	      <xsl:variable name="vthisType" select="."/>
	      <xsl:for-each select="$vDoc">
	        <xsl:copy-of select=
	           "key('kItemByType', $vthisType/@code)[$vcurTuple]"/>
	      </xsl:for-each>
	    </xsl:for-each>
	  </xsl:for-each>
	 </items>
	</xsl:template>

</xsl:stylesheet>

when applied on this source xml:

<root>
	<item type="A" subType="1"/>
	<item type="A" subType="2"/>
	<item type="A" subType="4"/>
	<item type="B" subType="2"/>
	<item type="B" subType="3"/>
	<item type="D" subType="1"/>
	<item type="D" subType="2"/>
	<item type="D" subType="3"/>
	<item type="D" subType="4"/>
	<item type="E" subType="2"/>
	<item type="E" subType="4"/>
</root>

produces the wanted result:

<items>
  <item type="A" subType="1" />
  <item type="B" subType="2" />
  <item type="D" subType="1" />
  <item type="E" subType="2" />
  <item type="A" subType="2" />
  <item type="B" subType="3" />
  <item type="D" subType="2" />
  <item type="E" subType="4" />
  <item type="A" subType="4" />
  <item type="D" subType="3" />
  <item type="D" subType="4" />
</items>


Cheers,
Dimitre Novatchev.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.