[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] How do I change a XSL style sheet to group data together under one heading


Subject: Re: [xsl] How do I change a XSL style sheet to group data together under one heading
From: "kieters c" <kieters@xxxxxxxxxxx>
Date: Thu, 17 May 2007 18:42:53 +0000

Thank you. I copied your code into an xsl file and use it with the flat file. It grouped it all by date. Just one additional question data is grouped by a selection of variables. As you would have noticed in the first example: sample_date_time, cp_name, imis_cp_ext and two other. How do I combine it in the key name instruction?

Incidently this may be the only instance that I may be using it.

Baie dankie.

Hennie

From: Abel Braaksma <abel.online@xxxxxxxxx>
Reply-To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] How do I change a XSL style sheet to group data together under one heading
Date: Thu, 17 May 2007 20:08:01 +0200


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/


_________________________________________________________________
Make free PC-to-PC calls with no loss of life! http://www.communicationevolved.com/en-za/



Current Thread
Keywords