XSLT2 format-dateTime - AM/PM without dots/periods - xslt

This is a question for XSLT 2 format-dateTime function.
Please can anyone tell me how to make A.M./P.M. display with not dots/periods (AM/PM)?
<xsl:variable name="ampm" select="format-dateTime(DATE, '[PN]')"/>
The below code returns blank?
<xsl:value-of select="replace($ampm,'.','')"/>
Thanks, Will

format-dateTime(DATE, '[PN,2-2]')
will output AM/PM on some XSLT 2 implementations.
The format string specifies that the output should be exactly 2 character.

translate works
<xsl:value-of select="translate($ampm,'.','')"/>

Related

XSLT mapping giving alphanumeric for decimal value

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

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')"/>

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)"/>

replace string in xslt 2.0 with replace function

I have a string like this
"My string"
Now I want to replace my with best so that the output will be like best string.
I have tried some thing like this
<xsl:value-of select="replace( 'my string',my,best)"/>
but probably its a wrong syntax
I have googled a lot but found nothing..every where the mechanism to do this XSLT 1.0 is explained.Can any one tell me how to do it in XSLT 2.0 ,The easy way compared to 1.0
Given:
<xsl:variable name="s1" select="'My string'"/>
Simply use:
<xsl:value-of select="replace($s1, 'My', 'best')"/>
Note that a regular expression is applied. Meaning:
<xsl:value-of select="replace('test.replace', '.', ':')"/>
Becomes:
::::::::::::
Be sure to escape the characters that have special meaning to the regular expression interpreter:
<xsl:value-of select="replace('test.replace', '\.', '::')"/>
Becomes:
test::replace
First check, if your xslt processor (saxxon) is the latest release. Then you have to set
<xsl:stylesheet version="2.0" in the head of your xslt-stylesheet. That's it.
Your code was fine, besides you forgot the apostrophs:
<xsl:value-of select="replace( 'my string',my,best)"/>
must be
<xsl:value-of select="replace('my string','my','best')"/>

How do I convert strings starting with numbers to numeric data in XSLT?

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]','')