I have a filed value like "+000002030" need to convert it to "20.30" how can i do this using XSLT. This value "+000002030" is the dynamic one any value it can come.Please let me know how we can convert it.
In XSLT 1.0, use:
<xsl:value-of select="format-number(translate(value, '+', '') div 100, '#.00')"/>
To make this future-proof, use:
<xsl:value-of select="format-number(number(translate(value, '+', '')) div 100, '#.00')"/>
This will work the same in both XSLT 1.0 and XSLT 2.0.
A solution in XSLT-2.0 would be
<xsl:value-of select="format-number(number('+000002030') div 100, '#.00')"/>
Related
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).
I have to repeat the following XSLT snippet like 100 times and I would like it to be as small as possible. Is there a way to make an equivalent XSLT snippet that is shorter?
<xslo:variable name="myVariable" select="//This/that/anotherthing" />
<xslo:choose>
<xslo:when test="string($myVariable) != 'NaN'">
<xslo:text>1</xslo:text>
</xslo:when>
<xslo:otherwise>
<xslo:text>0</xslo:text>
</xslo:otherwise>
</xslo:choose>
I'm basically setting the state of a checkbox based on whether or not a value exists in //This/that/anotherthing in the source xml.
Can be XSLT 1.0 or XSLT 2.0, doesn't matter.
You can use an if instead of xsl:choose (XSLT 2.0 only)...
<xsl:value-of select="if (string(number(//This/that/anotherthing)) = 'NaN') then 0 else 1"/>
I also dropped the xsl:variable, but if you need it for some other reason, you can put it back.
You could also create a function...
<xsl:function name="local:isNumber">
<xsl:param name="context"/>
<xsl:value-of select="if (string(number($context)) = 'NaN') then 0 else 1"/>
</xsl:function>
usage...
<xsl:value-of select="local:isNumber(//This/that/anotherthing)"/>
<xslo:variable name="myVariable" select="//This/that/anotherthing" />
<xslo:value-of select="number(boolean($myVariable))"/>
If I understand the purpose correctly - that is return 1 if the value in question can be successfully expressed as a number, 0 otherwise - then I believe:
<xsl:value-of select="number(//This/that/anotherthing castable as xs:double)"/>
would be the most straightforward way (in XSLT 2.0) to achieve it.
Edit
In view of your change of purpose:
I'm basically setting the state of a checkbox based on whether or not
a value exists in //This/that/anotherthing
That's even simpler:
<xsl:value-of select="number(boolean(string(//This/that/anotherthing)))"/>
I have following question, basically i just would like to create a custom array, something like
- Yellow | Red | Green
And after loop through this items using for-each and printing the values, i created something like this:
<xsl:variable name="Colors">
<m>Yellow</m>
<m>Red</m>
<m>Green</m>
</xsl:variable>
And then i try to loop on it:
<xsl:for-each select="$Colors">
<xsl:value-of select ="current()" />
</xsl:for-each>
But i get this error: Exrpession must evalutate to a node-set $Colors
Any idea, what could be wrong?
With XSLT 1.0 your variable contains a result tree fragment (RTF) and you can't do anything with it but use xsl:copy-of or xsl:value-of. Fortunately most XSLT 1.0 processor support exsl:node-set or similar to convert a result tree fragment to a node set so you can use <xsl:for-each select="exsl:node-set($Colors)/m">...</xsl:for-each>, where you then need to declare xmlns:exsl="http://exslt.org/common" in your stylesheet.
How can I change below two fields, format of dateTime in XSLT.
DateTime format
<startdate>2002-05-30T09:30:10+06:00</startdate>
<MidDate>2002-05-30T09:30:10+06:00</MidDate>
I needed as:
<startdate>2002-05-30 09:30:10</startdate>
<MidDate>2002-05-30 9:30</MidDate>
Within XSLT 1.0 you can use substring() as follows:
<xsl:value-of select="substring('2002-05-30T09:30:10+06:00', 1, 10)" /><xsl:text> </xsl:text><xsl:value-of select="substring('2002-05-30T09:30:10+06:00', 12, 8)" />
Above will output:
2002-05-30 09:30:10
If you don't want the seconds attached, you simply adjust the substring a little:
<xsl:value-of select="substring('2002-05-30T09:30:10+06:00', 1, 10)" /><xsl:text> </xsl:text><xsl:value-of select="substring('2002-05-30T09:30:10+06:00', 12, 5)" />
Which will output:
2002-05-30 9:30
If you are able to use XSLT 2.0, you can use format-date() as described in the standard documentation: Formatting Date and Times
<xsl:value-of select="format-date('2002-05-30T09:30:10+06:00', '[Y01]-[M01]-[D01] [H]:[m]:[s]')" />
<xsl:variable name="date1" select="2011-10-05"/>
<xsl:variable name="date2" select="2011-10-05"/>
<xsl:variable name="date3" select="2011-10-06"/>
<xsl:if test="$date2 = $date1 or $date2 < $date1">
..do something
</xsl:if>
<xsl:if test="$date3 = $date1 or $date3 > $date1">
.. do something
</xsl:if>
Both should evaluate true, but the second if doesn't. For the life of me I can't comprehended why!
In the actual transform the dates themselves are being drawn from an XML document but debugging through VS2010 i can see the values are as above.
Must be something fairly fundamental i'm doing wrong - any help would be brilliant!
I tried this in Oxygen/XML... select="2011-10-05 is being interpreted as an arithmetic expression, giving the value 1996 (2011 minus 10 minus 5) and "2011-10-06" is intrepreted as 1995.
What you want is
<xsl:variable name="date1" select="'2011-10-05'"/>
<xsl:variable name="date2" select="'2011-10-05'"/>
<xsl:variable name="date3" select="'2011-10-06'"/>
Note the extra single quotes.
From the XSLT 1.0 Specification:
If the variable-binding element has a select attribute, then the value
of the attribute must be an expression and the value of the variable
is the object that results from evaluating the expression.