<xsl:value-of select="format-number(xs:Position/xs:Weekly_Hours,'##.##')"/>
So if I give it as above, its showing it cannot convert string to integer.
I tried this also , but same
<xsl:variable name="myVar" select="xs:Position/xs:Weekly_Hours"/>
<xsl:value-of select="format-number($myVar,'##.##')"/>
Use <xsl:value-of select="format-number(number(xs:Position/xs:Weekly_Hours),'##.##')"/> or if you use XSLT 2.0 or later, instead of number you can use another numeric type like xs:double or xs:decimal.
Related
I have a requirement where i should get absolute value for -0.0000000005 .I have tried with <xsl:value-of select='abs(-0.0000000005)' /> and <xsl:value-of select="translate(-0.0000000005, '-', '')" /> .But nothing is working and getting always alpha numeric value as 5.0E10.Please help here to get correct absolute value form any decimal number.
need code for correct absolute value to get from decimal numbers
Depending upon the XSLT processor, this might work for you:
<xsl:value-of select="xs:decimal(abs(-0.0000000005))" />
You could try using the format-number function to explicitly specify how the number is formatted. For example something like this:
<xsl:value-of select="format-number(abs(-0.0000000005), '0.##########')" />
(Note that the abs function is not part of XPath 1.0/XSLT 1.0. Users of XSLT 1.0 may want to check if their XSLT processor supports the math:abs extension function from EXSLT.)
IS it possible to change the following exponential notation to integer notation using XSLT 2.0.
2.0151109001E10
to
20151109001
I tried with
number(2.0151109001E10)
but it gives NaN as answer.
EDIT:
XSLT:
<xsl:variable name="a" select="ss:Cell[$key]/ss:Data"/>
<xsl:variable name="b" select="string($a)"/>
<xsl:variable name="c" select="number($b)"/>
OUTPUT:
<a>2.0151109001E10</a>
<b>2.0151109001E10</b>
<c>2.0151109001E10</c>
The following works out
<xsl:value-of select="xs:decimal(xs:double(translate($a, ',', '.')))"/>
The real problem with your input is not the exponential notation, but the use of a comma as the decimal mark. XML only recognizes a period as the decimal mark.
Try something like:
<xsl:value-of select="xs:decimal(xs:double(translate($a, ',', '.')))"/>
or:
<xsl:value-of select="format-number(translate($a, ',', '.'), '0')"/>
You can try to put them inside the single quotes '' like this:
number('7.2345E7')
gives
72340000
You can use format-number:
format-number(2.0151109001E10, '#')
I have the following element as part of a larger XML
<MT N="NonEnglishAbstract" V="[DE] Deutsch Abstract text [FR] French Abstract text"/>
I need to do some formatting of the value in #V attribute, only if it contains anything like [DE], [FR] or any two capital letters representing a country code within square brackets.
If no such pattern exist, I need to simply write the value of #V without any formatting.
I can use an XSLT 2.0 solution
I was hoping that I could use the matches() function something like
<xsl:choose>
<xsl:when test="matches(#V,'\[([A-Z]{{2}})\]([^\[]+'">
//Do something
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="#V"/>
</xsl:otherwise>
</xsl:choose>
I think all you need is:
matches(#V,'\[[A-Z][A-Z]\]')
You don't have to match the entire string to get a true() ... I tell my students to write as short a reg-ex as possible.
You have not posted anything about what you have tried. How about looking up translate function and translating the strings capital letters to something like "X". Then test that string result for the existence of [XX]. That alone would tell you whether you need to process it.
<xsl:variable name="result">
<xsl:value-of select="translate(#V,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','XXXXXXXXXXXXXXXXXXXXXXXXX')"/>
</xsl:variable>
Then use that result and then test:
contains($result, "[XX]")
No regex required, pure XSL 1.1
I have to check if a value matches a certain string, and the input may be in any case.
<xsl:if test="$adminStatus='Down'">
do something
</xsl:if>
Use the translate() function on both $adminStatus and target value.
How can I convert a string to upper- or lower-case with XSLT?
You use the translate function to convert all upper case to lower case.
<xsl:if test="translate($adminStatus, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'down'">
do something
</xsl:if>
Given the following XML:
<table>
<col width="12pt"/>
<col width="24pt"/>
<col width="12pt"/>
<col width="48pt"/>
</table>
How can I convert the width attributes to numeric values that can be used in mathematical expressions? So far, I have used substring-before to do this. Here is an example template (XSLT 2.0 only) that shows how to sum the values:
<xsl:template match="table">
<xsl:text>Col sum: </xsl:text>
<xsl:value-of select="sum(
for $w
in col/#width
return number(substring-before($w, 'pt'))
)"/>
</xsl:template>
Now my questions:
Is there a more efficient way to do the conversion than substring-before?
What if I don't know the text after the numbers? Any way to do it without using regular expressions?
This is horrible, but depending on just how much you know about the potetntial set of non-numeric characters, you could strip them with translate():
translate("12jfksjkdfjskdfj", "abcdefghijklmnopqrstuvwxyz", "")
returns
"12"
which you can then pass to number() as currently.
(I said it was horrible. Note that translate() is case sensitive, too)
I found this answer from Dimitre Novatchev that provides a very clever XPATH solution that doesn't use regex:
translate(., translate(.,'0123456789', ''), '')
It uses the nested translate to strip all the numbers from the string, which yields all other characters, which are used as the values for the wrapping translate function to strip out and return just the number characters.
Applied to your template:
<xsl:template match="table">
<xsl:text>Col sum: </xsl:text>
<xsl:value-of select="sum(
for $w
in col/#width
return number(translate($w, translate($w,'0123456789', ''), ''))
)"/>
</xsl:template>
If you are using XSLT 2.0 is there a reason why you want to avoid using regex?
The most simple solution would probably be to use the replace function with a regex pattern to match on any non-numeric character and replace with empty string.:
replace($w,'[^0-9]','')