Re: [xsl] multiple elements to use for grouping

Subject: Re: [xsl] multiple elements to use for grouping
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Fri, 13 Nov 2009 14:37:49 +0100
Hoskins, Dorothy [OCDUS Non J&J] wrote:
HI, I have a hard time articulating this one, but I think the examples
will describe it best. I am trying to help someone sort a group of
Excel-generated content chunks, which means that the overall structure
is flat and I have to deal with siblings a lot. The output is supposed
to be a classified listing. I am using Oxygen with Saxon 9 open-source
and XSLT 2.
Input:
<?xml version="1.0" encoding="UTF-8"?>
<root>
   <row>
       <Classification1>Printing</Classification1>
       <Classification2>Packaging</Classification2>
       <Customer>Modern Press</Customer>
       <City>Fairville</City>
       <Tel>1-888-777-6666</Tel>
       <Fax>1-888-777-5555</Fax>
       <Email>name@xxxxxxxxx</Email>
       <Image href="images\logo.eps"/>
       <Website> www.modernprinting.com</Website>
   </row>
   <row>
       <Classification1>Packaging</Classification1>
       <Classification2>Printing</Classification2>
       <Classification3>Binding</Classification3>
       <Customer>Complete Printing</Customer>
       <City>Plainville</City>
       <Tel>1-888-777-4444</Tel>
       <Fax>1-888-777-3333</Fax>
       <Email>name@xxxxxxxxx</Email>
       <Image href="images\logo.eps"/>
       <Website> www.completeprinting.com</Website>
   </row>
   <row>
       <Classification1>Binding</Classification3>
^^^^^^^^^^^^^^^^

This is not well-formed.

       <Customer>The Bindery</Customer>
       <City>Overyonder</City>
       <Tel>1-888-777-2222</Tel>
       <Fax>1-888-777-1111</Fax>
       <Email>name@xxxxxxxxx</Email>
       <Image href="images\logo.eps"/>
       <Website> www.thebindery.com</Website>
   </row>
</root>

Desired output: Group all the Customers and their sibling city, phone.
fax, etc. under one unique Classification named from the equality of
values of all Classification, Classification2 and Classification3
elements. In other words, the <Customer> named Modern Printing Press
would show up under each of the three different classifications
(Binding, Packaging and Printing) listed under its parent <data> name.
Complete Printing would show up under both Packaging and Printing. The
Bindery would appear only under Binding.

Try whether the following does what you want:


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";
  exclude-result-prefixes="xs"
  version="2.0">

<xsl:output indent="yes"/>

<xsl:template match="root">
<directory>
<xsl:for-each-group select="row" group-by="Classification1, Classification2, Classification3">
<xsl:sort select="current-grouping-key()"/>
<listing>
<Classification><xsl:value-of select="current-grouping-key()"/></Classification>
<xsl:copy-of select="current-group()/(* except (Classification1, Classification2, Classification3))"/>
</listing>
</xsl:for-each-group>
</directory>
</xsl:template>


</xsl:stylesheet>
--

	Martin Honnen
	http://msmvps.com/blogs/martin_honnen/

Current Thread