XSLT mapping giving alphanumeric for decimal value - xslt

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.)

Related

Formatting number in xslt

I am trying to format a number in XSLT, but I always get NaN as a result.
Original example number is: 1 321.94
Code:
<xsl:value-of select="format-number(number(string(.)), '### ##0,00', 'format1')"/>
Seems like number(string(.)) doesn't work. How can I remove the space from the original number to cope with NaN?
Using translate() should work for both XSLT 1.0 and 2.0. You could also use replace() in 2.0.
Here's an example of translate() (broken up into multiple lines for readability):
<xsl:value-of
select="format-number(
number(translate(.,' ','')),
'### ##0,00','format1')"/>

I want to get the output with 2 decimal numbers

<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.

normalize-space() not working

There is xslt code for version 1.0 but I want to convert it to version 2.0.
<xsl:value-of select="normalize-space(round((. - $var1) div $var2))"/>
But when I try to run it, the SAXON output is:
F [Saxon-HE 9.5.0.2] XPTY0004: Required item type of first argument of normalize-space() is xs:string; supplied value has item type numeric
help me in finding and solve this issue? Thanks in advance.
As the error message mentions, normalize-space expects a string as an argument, but round returns a numeric value. Numbers don't actually have spaces in though, so there is no need to use normalize-space on the result.
This should work instead:
<xsl:value-of select="round((. - $var1) div $var2)"/>

XSLT - Check if pattern exists in an element string

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

XSLT- Get substring and pass it as parameters to java function

In the below code snippet, I am trying to get the substring of my #imageMeta node, append some more path location and pass it as a parameter to my java method through XSLT.
<xsl:variable name="imagePathFrom" select="/config/assets/images/{substring-after(#imageMeta,'/')}" />
<xsl:variable name="imagePathTo" select="'/dev/svn_root/platform/system'" />
<xsl:value-of select="filecopy:copyFile($imagePathFrom, $imagePathTo)"/>
My #imageMeta node data looks like Images/common/dialog/dialogue_black.png.
I have to convert the above path to images/common/dialog/dialogue_black.png (note the change of capital 'I' to small 'i') and append some more path data.
So my final path entry should look like "/config/assets/images/common/dialog/dialogue_black.png". When i run my code snippet i get an error stating:
line 51: Error parsing XPath expression '/config/assets/images/{substring-after(#imageMeta,'/')}'.'
Please help.
<xsl:variable name="imagePathFrom" select="/config/assets/images/{substring-after(#imageMeta,'/')}" />
There are two problems here:
A syntax error -- a select is probably the only attribute attribute in XSLT that cannot contain an AVT.
Even without the AVT, this would attempt to select all /config/assets/images nodes, but the intent is that the variable must contain the string "/config/assets/images"
Solution to both problems:
<xsl:variable name="imagePathFrom" select=
"concat('/config/assets/images/', substring-after(#imageMeta,'/')" />
Alternative solution:
<xsl:variable name="imagePathFrom" select=
"concat('/config/assets/',
translate(substring(#imageMeta, 1, 1),
$vUpper,
$vLower
),
substring(#imageMeta, 2)
)" />
where $vLower and $vUpper are defined, respectively, as:
'abcdefghijklmnopqrstuvwxyz'
and
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
There is one problem in your code:
<xsl:variable name="imagePathFrom" select="/config/assets/images/{substring-after(#imageMeta,'/')}" />
It suppose to be ..
<xsl:variable name="imagePathFrom" select="substring-after(/config/assets/images/#imageMeta,'/')" />
infant programmer 'Aravind' suggestion will solve your parse error.
You also mentioned you wanted to lower-case the capital i. Two options here:
Using XSLT 1.0, this StackOverflow answer explains how to lower-case the first character of a string. It won't work for Unicode characters such as 'Í' but you probably don't need it.
XSLT 2.0 has a lower-case function, which will lower-case your entire string, and may not be what you're looking for.