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

Re: How do I change a XSL style sheet to group data t

Subject: Re: How do I change a XSL style sheet to group data together under one heading
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Thu, 17 May 2007 20:08:01 +0200
Re:  How do I change a XSL style sheet to group data  t
Much better! Still, the XML and XSLT (all your data) are not valid XML (next time, try it yourself before you copy it) but I could make something out of it, I think. More importantly, with these little examples you made instantly clear what you wanted. You XSLT only contains LREs and nothing more, but you started this thread saying that you know "next to nothing" about it.

Unfortunately, grouping is *not* easy in XSLT 1.0 (it is in 2.0) and you'll have to do some learning, tutorials and research on XSLT to get acquainted to it (first learn the normal basics. After a couple of weeks, start with grouping. Search for "Muenchian grouping")

Below is a way to turn your 'sample' elements grouped together based on the child 'sample_date_time'. If you need to group by other constructs, change the xsl:key. It follows the basic Muenchian method, I believe, and it is easily expanded to your needs. I turned all children into attributes of the 'return' element, which is what your sample looked like.

<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">

<xsl:output method="xml" indent="yes"/>

   <!-- the key determines (here) what to group by -->
   <xsl:key name="sample" match="sample" use="sample_date_time" />

   <xsl:template match="/">
       <submission imis_company_code="0001843309" ws_name="LENNOX">
           <xsl:apply-templates />
       </submission>
   </xsl:template>

<xsl:template match="dataroot">

<!-- the actual 'group by' construct -->
<xsl:for-each select="sample[generate-id() = generate-id(key('sample', sample_date_time)[1])]">
<xsl:copy>
<!-- date-time must become an attribute -->
<xsl:apply-templates select="sample_date_time" />


<!-- find all samples with equal date time -->
<xsl:apply-templates select="key('sample', sample_date_time)" />
</xsl:copy>
</xsl:for-each>
</xsl:template>


   <!-- match the sample's that are grouped -->
   <xsl:template match="sample">
       <result>
           <xsl:apply-templates select="*[not(self::sample_date_time)]"  />
       </result>
   </xsl:template>

   <!-- any direct child of 'sample' must be turned into an attribute -->
   <xsl:template match="sample/*">
       <xsl:attribute name="{name()}">
           <xsl:value-of select="."/>
       </xsl:attribute>
   </xsl:template>

</xsl:stylesheet>


This outputs the following when applied to your corrected source:


<submission imis_company_code="0001843309" ws_name="LENNOX">
  <sample sample_date_time="20040801">
     <result rpttime_name="DAILY" sis_imis_code="FTFLOW"/>
     <result rpttime_name="DAILY" sis_imis_code="PH"/>
  </sample>
</submission>


If you add more samples with different sample_date_time, they will be grouped in their own right.



HTH!, Cheers, -- Abel Braaksma




kieters c wrote:
Good day,

I am sorry it once again happened. I will delete some of the data in between as it I would be able to correct it afterwards. I have removed most data and background clutter and hope it is correct now.

I attach an abbreviated version of the present XML file and what I hope to achieve. The flat file and an abbreviated style sheet.

Once again thank you for valuable time.

Hennie

From
<?xml version="1.0" ?>
<sample sample_date_time="20040801" >
      <result rpttime_name="DAILY" sis_imis_code="FTFLOW/>
</sample>
<sample sample_date_time="20040801"
      <result rpttime_name="DAILY" sis_imis_code="PH" />
</sample>

To
<?xml version="1.0" ?>
<sample sample_date_time="20040801" >
       <result rpttime_name="DAILY" sis_imis_code="FTFLOW/>
       <result rpttime_name="DAILY" sis_imis_code="PH" />
</sample>

Flat file

<dataroot>
  <sample>
    <sample_date_time>20040801</sample_date_time>
    <rpttime_name>DAILY</rpttime_name>
    <sis_imis_code>FTFLOW</sis_imis_code>
 </sample>
  <sample>
    <sample_date_time>20040801</sample_date_time>
    <rpttime_name>DAILY</rpttime_name>
    <sis_imis_code>PH</sis_imis_code>
 <sample>
</dataroot>

Style sheet

<?xml version="1.0" encoding="iso-8859-1"?><!--sample.xsl-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
    <submission imis_company_code="0001843309" ws_name="LENNOX">
      <xsl:apply-templates/>
    </submission>
  </xsl:template>

  <xsl:template match="sample">
    <sample
           sample_date_time="sample_date_time"
        <result
               rpttime_name="rpttime_name"
               sis_imis_code="sis_imis_code"
    </sample>
  </xsl:template>
</xsl:stylesheet>

_________________________________________________________________
Message offline contacts without any fire risk! http://www.communicationevolved.com/en-za/

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.