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

Re: Calculations involving measurements with units

Subject: Re: Calculations involving measurements with units
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 3 Jan 2004 15:44:20 +0100
use case calculations
> 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:

     <csstext>5.5in</csstext>

this transformation:


<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:myController="f:myController"
 xmlns:x="f:myTest"
 >
  <xsl:import href="strSpan.xsl"/>
  <!-- To be applied on csstext.xml -->
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <myController:myController/>

  <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"
                      select="$x:st/myController:*[1]"/>
      <xsl:with-param name="pContollerParam"
                      select="'0123456789.'"/>
      <xsl:with-param name="pElementName1" select="'value'"/>
      <xsl:with-param name="pElementName2" select="'unit'"/>
    </xsl:call-template>
  </xsl:template>

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

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


produces this result:

    <value>5.5</value>
    <unit>in</unit>


As explained in this list almost two years ago
(http://www.biglist.com/cgi-bin/wilma/wilma_hiliter/xsl-list/200203/msg01075
.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

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.