[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
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.
kieters c wrote: Good day,
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|