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

Re: xslt 2.0 challenge

Subject: Re: xslt 2.0 challenge
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 8 Apr 2004 17:08:10 +0100
how to add a percentage
Hi Andrew,

> The steps involved are:
>
> 1.  Find the corresponding start and end <colspecs> based on @namest and
> @nameend
> 2.  Sum the @colwidth values for the <colspecs> between (and including)
> the columns found in step1
> 3.  Divide that value by the sum of all colwidths for that group
> (siblings) and add on a % symbol

Here's another XSLT 2.0 solution. This one uses a function to do the
summing of column widths, and <xsl:for-each> rather than a for
expression (pah!). I use the new >> operator to identify the colspecs
between two others. I also take advantage of the XSLT 1.0
format-number() facility of including a % in the format pattern in
order to get a number formatted as a percentage.

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:my="http://www.jenitennison.com/">

<xsl:function name="my:sum-widths" as="xs:integer">
  <xsl:param name="colspecs" as="element()+" />
  <xsl:variable name="widths" as="xs:integer+">
    <xsl:for-each select="$colspecs">
      <xsl:sequence select="xs:integer(translate(@colwidth, 'm', ''))" />
    </xsl:for-each>
  </xsl:variable>
  <xsl:sequence select="sum($widths)" />
</xsl:function>

<xsl:template match="spanspec">
  <xsl:variable name="all-colspecs" as="element()+"
    select="ancestor::table//colspec" />
  <xsl:variable name="first-colspec" as="element()"
    select="$all-colspecs[@colname = current()/@namest]" />
  <xsl:variable name="last-colspec" as="element()"
    select="$all-colspecs[@colname = current()/@nameend]" />
  <xsl:variable name="sibling-colspecs" as="element()+"
    select="$first-colspec/../colspec" />
  <xsl:variable name="span-colspecs" as="element()+"
    select="$first-colspec |
            $sibling-colspecs[$last-colspec >> . and . >> $first-colspec] |
            $last-colspec" />
  <xsl:value-of select="format-number(my:sum-widths($span-colspecs) div
                                      my:sum-widths($sibling-colspecs),
                                      '#0.00%')" />
</xsl:template>

</xsl:stylesheet>

Not all the variable declarations are necessary of course, though
declaring the types of the variables helped me debug the code. The
element node tests would be "element(colspec, *)" rather than just
"element()", but I wanted a solution that worked in Saxon 7.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

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.