RE: [xsl] Sort multiple xml files with identical keys

Subject: RE: [xsl] Sort multiple xml files with identical keys
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 1 Mar 2006 14:40:33 -0000
The Muenchian method doesn't work well across multiple documents, because
key() only works within one document at a time. Best approach is to combine
the documents first, then do the grouping.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: Tapio.Niva@xxxxxxxxxxxxxxx [mailto:Tapio.Niva@xxxxxxxxxxxxxxx] 
> Sent: 01 March 2006 13:51
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Sort multiple xml files with identical keys
> 
> Hello,
> I'm using IE6 & MSXML to transform,sort and count 2 separate xml docs,
> but the results are not as I expected.
> As a beginner, I obviously have understood something wrong, I'm using
> the so-called "Muenchian method" here (...wtg for the XSLT 
> 2.0 to MSXML)
> I would be very appreciated if some xslt expert could give me a clue
> what I'm doing wrong.
> 
> Below are xml, xsl, results I got and results I wanted :
> -------------------------
> net_sample1.xml:
> <?xml version="1.0" encoding="UTF-8"?>				
> <?xml-stylesheet type="text/xsl" href="net_sample.xsl"?>		
> <List>	
> 	<row>
> 		<row_id>A1</row_id>
> 		<row_status>New</row_status>
> 	</row>
> 	<row>
> 		<row_id>A2</row_id>
> 		<row_status>New</row_status>
> 	</row>
> 	<row>
> 		<row_id>A2</row_id>
> 		<row_status>Old</row_status>
> 	</row>
> </List>
> -------------------------
> net_sample2.xml:
> <?xml version="1.0" encoding="UTF-8"?>				
> <?xml-stylesheet type="text/xsl" href="net_sample.xsl"?>		
> <List>	
> 	<row>
> 		<row_id>A1</row_id>
> 		<row_status>New</row_status>
> 	</row>
> 	<row>
> 		<row_id>A1</row_id>
> 		<row_status>New</row_status>
> 	</row>
> 	<row>
> 		<row_id>A2</row_id>
> 		<row_status>Old</row_status>
> 	</row>
> </List>
> ------------------------
> net_sample.xsl :
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; >
> <xsl:key name="rowkey"   	match="row"
> use="row_id" />
> <xsl:key name="rowkey_old"  	match="row[row_status='Old']"
> use="row_id" />
> <xsl:key name="rowkey_new"  	match="row[row_status='New']"
> use="row_id" />
> <xsl:template match="/">
> <html>
> <head></head>
> <body>
> <xsl:variable name="all_docs" select="document('net_sample1.xml'
> )/List/row | document('net_sample2.xml' )/List/row "/>	 
> <table border="0" bgcolor="ivory">
> <xsl:for-each select="$all_docs">
>  <xsl:sort select="row_id"/>
>    <xsl:for-each select="current()[count(. | key('rowkey', 
> row_id)[1]) =
> 1]"> 	
>    <tr>
>     <td><xsl:value-of select="row_id"/></td>		
>     <th>Old rows = </th>
>     <td><xsl:value-of select="count( key('rowkey_old',row_id) 
> )"/></td>
>     <th>New rows = </th>
>     <td><xsl:value-of select="count( key('rowkey_new',row_id) 
> )"/></td>
>    </tr>
>   </xsl:for-each>
> </xsl:for-each>
> </table>
> </body>
> </html>
> </xsl:template>	
> </xsl:stylesheet>
> ------------
> Result:
> A1	Old rows=0 New rows=1
> A1	Old rows=0 New rows=2
> A2	Old rows=1 New rows=1
> A2	Old rows=1 New rows=0
> ------------------------------------------
> The result I expected to see  :
> A1	Old rows=0 New rows=3
> A2	Old rows=2 New rows=1
> 
> Any ideas how to reach the wanted result ?
> 
> Regards, Tapio

Current Thread