[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: <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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Calculations involving me, Paul A. Hoadley | Thread | Re: [xsl] Re: Calculations involvin, Paul A. Hoadley |
RE: [xsl] Calculations involving me, Michael Kay | Date | Re: [xsl] XSLT/Java does not identi, Linc |
Month |