XSL Concatnate Number to Dollar amount - xslt

my XML looks like this
<SAC05>7100</SAC05>
I need to format <SAC05> from 7100 to 71.00 in the display.
I've tried this:
$<xsl:value-of select="format-number(SAC05, '###,###,###.##')"/>
But it comes back at $7100

format-number() function formats the input value into the specified format. Since the input value is 7100 it will try to convert it into 7100.00 and not 71.00.
You need to divide the input by 100 and then apply the formatting. Also you can make use of the concat() function to prefix $ symbol to the output.
<xsl:value-of select="concat('$', format-number(SAC05 div 100, '###,###,###.00'))" />

Related

XSLT 1.0 - Padding 0's with Math Equation

I am trying to pad an output in XSLT with variables being multiplied in XSLT 1.0. I cannot use the format-number because it will auto-round the numbers but I need to get the decimals in place. I can get this to work when I use column formatting within the XML, but since this has multiple column formats within the value-of-select, it does not seem to work here and obey the 7 in the code below:
<xsl:value-of select="substring(E_BaseRate * E_NormalHours * 2, 1, 7)" disable-output-escaping="yes"/>
The output I am getting is: 1601.93 translated to 160193 but I need to get the result of 0160193. Is there a way to do this?
I broke this out to give you an idea. You could use as is or put it all together into one select...
<xsl:variable name="numToString" select="string(160193)"/>
<xsl:variable name="numLen" select="string-length($numToString)"/>
<xsl:variable name="value" select="concat(substring('0000000', 1, 7 - $numLen), $numToString)"/>

xslt check if a variable is a decimal with digits after decimal seperator

I want to show decimal inputs like 100.1, 100.0 in a different format from integer inputs like 100
I have used the following check for identifying decimals(The variable input is of numeric type)
<xsl:when test="not(floor($input) = $input)">
It works for cases like 100.1 but it does not work for cases like 100.00, this would return false saying that this is not a decimal.
I have then tried to use
<xsl:when test="$input castable as xs:decimal">
This is would not work for integers like 100 as the check would return true.
I tried
<xsl:when test="$input instance of xs:decimal">
for which I am getting false for values like 100.50, not sure why.
Is there any other way I can solve this?
If you want to distinguish between 100 and 100.0, you cannot be in the numeric domain. Try perhaps:
<xsl:when test="contains($input, '.')">
or use regex.
You really need to show how you have declared the type of the variable $input (or in the absence of a type declaration, how you have initialized its value). It makes a big difference.
Note that 100.5 as an XPath literal produces the xs:decimal value 100.5, but 100.5 held as the value of an untyped attribute node will be converted to an xs:double value approximately equal to 100.5.
In principle you can identify decimals that are not integers using
$x instance of xs:decimal and not($x instance of xs:integer)
but that assumes that you created the value as an xs:decimal or xs:integer in the first place; if you do xs:decimal("100") then you will probably[*] get an xs:decimal that is not an xs:integer.
[*] I say probably, because the spec requires it to be an xs:decimal, but it arguably permits it to be a subtype of xs:decimal, e.g. it could even return an instance of xs:unsignedByte here if it chose, or for that matter an instance of saxon:integer-in-range-1-to-100, so long as that is defined as a subtype of xs:decimal.]

Formatting Number string representation in xsl

Given a set of 'numbers' 0242, 0980, 0526, 1732, ...
How can I transform them to look like 24,2 98,0 52,6 173,2 ?
I'm trying xsl:value-of select="format_number(0242,'#,##'), but that would produce 2,42. With the '##,#' the output is 2,4,2
Cheers.
First, define a custom decimal format and place it at the top level of your stylesheet:
<xsl:decimal-format decimal-separator="," grouping-separator="."/>
Then you can use:
<xsl:value-of select="format-number(0242 div 10, '#,0')"/>
to get 24,2 and so on.
Alternatively, you could use:
<xsl:value-of select="translate(format-number(0242 div 10, '#.0'), '.', ',')"/>
You can divide it by 10 to get the number right, then format it.
<xsl:value-of select="format-number(node() div 10, '#.0')" />
Please note that I used a dot instead of the comma to use the decimal point operator (for operators see definition on w3schools).

XSLT format-number transform percent

Given the XML:
<current rate="0.1412" />
The desired output to screen is:
14.1200%
However, the current transform:
<xsl:value-of select="format-number(current/#rate, '###,##0.0000')"/>%
Yields the output:
0.1412%
Is it possible to do a format-number transform that will output a value in the desired format (four decimal places) given the current XML input or does it require a change in the xml input to better match the expected output?
Say:
<current rate="0.141200" />
There is no need to multiply or use any other tricks - just tell the format-number() function you want the number to be formatted as percent:
<xsl:value-of select="format-number(current/#rate, '#,##0.0000%')"/>
If your rate is being stored as a decimal, then you will need to multiple by 100 to show it as a percentage:
<xsl:value-of select="format-number(current/#rate * 100, '###,##0.0000')"/>%

Filtering set of rows with XSLT

i have troubles filtering with XSLT, basically i have an xml, where i would like to get only those items where the Due Date falls into interval of 3 months ... i've written the following:
<xsl:variable name="TaskRows" select="/dsQueryResponse/Tasks/Rows/Row[#DueDate > $IsoBeginQuartDate AND #DueDate < $IsoEndQuartDate]"/>
But I get an error: "Failed seting processor stylesheet: expected token ']' found 'NAME' ...
IsoDates are calculated and formated dates in ISO format ...
Any idea, how to do it, or i can't use "AND" when filtering?
PS: i'm using XSLT 1.0
There is case-sensitivity at work here! Try using 'and' instead of 'AND'.
Unfortunately in XSLT 1.0, you can't compare dates to see if a date is between two values.
What you could do is something like this:
<!-- Make sure to format this variables as YYYYMMDD -->
<xsl:variable name="$IsoBeginQuartDate">20130101</xsl:variable>
<xsl:variable name="$IsoBeginQuartDate">20131231</xsl:variable>
<!-- Make sure #DueDate also has the format YYYYMMDD, in this example I assume the DueDate has format YYYY-MM-DD -->
<xsl:variable name="TaskRows" select="/dsQueryResponse/Tasks/Rows/Row[translate(#DueDate, '-', '') > $IsoBeginQuartDate and translate(#DueDate, '-', '') < $IsoEndQuartDate]"/>
You now get errors, because > and < or not supported for strings or dates.