Sign In
Wednesday, February 17, 2010

The very same simple tasks tend to appear in different languages (e.g. C# Haiku). Now we have to find:

• integer and fractional part of a decimal;
• length and precision of a decimal.

These tasks have no trivial solutions in xslt 2.0.

At present we have came up with the following answers:

Fractional part:

```<xsl:function name="t:fraction" as="xs:decimal">   <xsl:param name="value" as="xs:decimal"/>   <xsl:sequence select="\$value mod 1"/> </xsl:function>```

Integer part v1:

```<xsl:function name="t:integer" as="xs:decimal">   <xsl:param name="value" as="xs:decimal"/>   <xsl:sequence select="\$value - t:fraction(\$value)"/> </xsl:function>```

Integer part v2:

```<xsl:function name="t:integer" as="xs:decimal">   <xsl:param name="value" as="xs:decimal"/>   <xsl:sequence select="     if (\$value ge 0) then       floor(\$value)     else       -floor(-\$value)"/> </xsl:function>```

Length and precision:

```<!--   Gets a decimal specification as a closure:     (\$length as xs:integer, \$precision as xs:integer). --> <xsl:function name="t:decimal-spec" as="xs:integer+">   <xsl:param name="value" as="xs:decimal"/>   <xsl:variable name="text" as="xs:string" select="     if (\$value lt 0) then       xs:string(-\$value)     else       xs:string(\$value)"/>   <xsl:variable name="length" as="xs:integer"     select="string-length(\$text)"/>   <xsl:variable name="integer-length" as="xs:integer"     select="string-length(substring-before(\$text, '.'))"/>     <xsl:sequence select="     if (\$integer-length) then       (\$length - 1, \$length - \$integer-length - 1)     else       (\$length, 0)"/> </xsl:function>```

The last function looks odious. In many other languages its implementation would be considered as embarrassing.

Wednesday, February 17, 2010 7:29:55 AM UTC      Comments [0] -
Tips and tricks | xslt
Comments are closed.
Navigation
Archive
 < October 2019 >
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Categories
 .NET AngularJS Announce ASP.NET BizTalk Server C++ Incremental Parser Java javascript JSF and Facelets kendoui SCCBridge SQL Server puzzle Thinking aloud Tips and tricks Window Search xslt
Blogroll
Statistics
Total Posts: 366
This Year: 2
This Month: 0
This Week: 0
Comments: 226
Disclaimer
The opinions expressed herein are our own personal opinions and do not represent our employer's view in anyway.

© 2019, Nesterovsky bros
All Content © 2019, Nesterovsky bros
DasBlog theme 'Business' created by Christoph De Baene (delarou)