[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Re: Calculations involving measurements with units

Subject: [xsl] Re: Calculations involving measurements with units
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 3 Jan 2004 15:44:20 +0100

> Thanks for this.  I used a combination of your code and Michael Kay's
> suggestion using translate() to strip the units:
> <xsl:variable name="upper-margin"
> select="concat(number(translate($page-height, translate($page-height,
> '0123456789.', ''), '')) div 3.75, translate($page-height,
> '012345679.', ''))"/>
> Of course, that's quite ugly and I should probably break it out into
> separate templates to strip the units and identify the units, but it
> works.

This is a text-processing task. See how this can be done using FXSL (its
"strSpan" template):

When applied on this source.xml:


this transformation:

<xsl:stylesheet version="1.0"
  <xsl:import href="strSpan.xsl"/>
  <!-- To be applied on csstext.xml -->
  <xsl:output omit-xml-declaration="yes" indent="yes"/>


  <xsl:variable name="x:st" select="document('')/*"/>

  <xsl:template match="/">
    <xsl:call-template name="strSpan">
      <xsl:with-param name="pStr" select="string(/*)"/>
      <xsl:with-param name="pController"
      <xsl:with-param name="pContollerParam"
      <xsl:with-param name="pElementName1" select="'value'"/>
      <xsl:with-param name="pElementName2" select="'unit'"/>

  <xsl:template match="myController:*">
    <xsl:param name="pChar"/>
    <xsl:param name="pParams"/>

    <xsl:if test="contains($pParams, $pChar)">1</xsl:if>

produces this result:


As explained in this list almost two years ago
.html?line=152#hilite) splitting a string into two parts according to a
condition can almost always be done effectively
using the standard XPath function translate().

However, translate will not be so convenient to use if the string to be
split and the condition are created dynamically (not known in advance),
or if the string may be comprised of any of many different characters
(e.g. all Unicode characters).

Another case is when the condition is a function not of a single
character only.

In these cases using translate will be either inconvenient or
impossible (in the last case).

The "strSpan" template takes a string, a predicate, which acts on a single
character, two element names for the nodes that will be returned, that
will contain the results. The first of these nodes will contain the
longest substring of characters of the string starting from the first
character, which all satisfy the predicate. The second node will
contain the rest of the string.

Hope this helps.

Dimitre Novatchev.
FXSL developer

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html

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

Current Thread