Altova Mailing List Archives>Archive Index >comp.text.xml Archive Home >Recent entries >Thread Prev - Newbie XSL Question: Comparing to preceding-sibling within for-each [Thread Next] Re: Newbie XSL Question: Comparing to preceding-sibling within for-eachTo: NULL Date: 5/9/2007 4:44:00 PM
Red <funkandlove@y...> wrote in
<1178711269.587341.163300@n...>:
> We have certain reports that contain grouping and sums in
> the output. The existing XSL just reads out all rows to
> html tables. I would like to make these easier to read by
> eliminating all duplicates within a row. At the moment, I
> dont have the access to change the XML output, just the
> XSL.
>
> <?xml version="1.0"?>
> <rs:data>
Namespace declarations seem to be missing.
> Birmingham 1001001 Open 5380.04
> 1001002 Open 1281.12
> London 1001003 Closed 1015.32
> 1001004 Open 9866.53
> 1001005 Open 1659.55
> Glasgow 1001006 Open 6944.21
Generalising a bit, it's a trivial grouping problem. The
following is a working example of how it's done:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:rs="http://example.org/rs"
xmlns:z="http://example.org/z">
<xsl:output method="html"/>
<xsl:key name="branch" match="z:row" use="@BRANCH"/>
<xsl:key name="branches" match="z:row"
use="count(.|key('branch',@BRANCH)[1])=1"/>
<xsl:template match="rs:data">
<data>
<xsl:apply-templates
select="key('branches',true())" mode="branch"/>
</data>
</xsl:template>
<xsl:template match="z:row" mode="branch">
<xsl:apply-templates select="key('branch',@BRANCH)"/>
</xsl:template>
<xsl:template
match="z:row[count(.|key('branch',@BRANCH)[1])=1]">
<row>
<cell><xsl:apply-templates select="@BRANCH"/></cell>
<xsl:call-template name="acct-stat-bal"/>
</row>
</xsl:template>
<xsl:template match="z:row">
<row>
<cell></cell>
<xsl:call-template name="acct-stat-bal"/>
</row>
</xsl:template>
<xsl:template name="acct-stat-bal">
<cell>
<xsl:apply-templates select="@ACCOUNT_NO"/>
</cell>
<cell>
<xsl:apply-templates select="@STATUS"/>
</cell>
<cell>
<xsl:apply-templates select="@BALANCE"/>
</cell>
</xsl:template>
</xsl:stylesheet>
It gets a bit hairier if you need to combine grouping and
sorting. On the other hand, if you can use an
XSLT2-compliant processor, everything suddenly becomes very
easy as long as overusing sequences does not lead to
performance issues.
<irony intensity="0.75">
Note that for-each is evil and employing it where unneeded
may darn you to heck. If that training next month doesn't
mention it, I advise accusing your mentors of being
blasphemous for-each-worshippers, then preaching the
virtues of template-based approach. Who knows, they might
not be beyond redemption yet.
</irony>
--
Pavel Lepin
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
