Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Newbie XSL Question: Comparing to preceding-sibling within for-each

From: Pavel Lepin <p.lepin@-------.--->
To: 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


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent