xsl-list
[Top] [All Lists]

Re: Calculations involving measurements with units

2004-01-03 14:54:44
On Sat, Jan 03, 2004 at 12:43:23PM -0000, Michael Kay wrote:

XSLT 2.0 lets you write stylesheet functions that can be called from
within an XPath expression, which solves this problem.

Sounds ideal.

In 1.0 you could try breaking the expression up by using variables,
but of course these can't take arguments in the way that a template
or function can.

I ended up writing a third template to handle calling the first two:

  <xsl:template name="divideDimension">
    <xsl:param name="dimension"/>
    <xsl:param name="divisor"/>
    <xsl:variable name="dimension-stripped">
      <xsl:call-template name="stripUnits">
        <xsl:with-param name="input" select="$dimension"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="result-stripped">
      <xsl:value-of select="$dimension-stripped div $divisor"/>
    </xsl:variable>
    <xsl:value-of select="$result-stripped"/>
    <xsl:call-template name="getUnits">
      <xsl:with-param name="input" select="$dimension"/>
    </xsl:call-template>
  </xsl:template>

Certainly this _works_ (in the limited number of circumstances I have
tested it), but is this the kind of code people are writing?  It just
seems rather verbose to me.  If that is just a limitation of XSLT 1.0,
then fine---but if it's verbose because I haven't seen a better way to
code it, let me know.  :-)


-- 
Paul.

mailto:paulh(_at_)logicsquad(_dot_)net
mailto:phoadley(_at_)maths(_dot_)adelaide(_dot_)edu(_dot_)au

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list