When number is given get correspondent month in XSLT - xslt

I want to get the month when given the correspondent number
Ex:
01 - January
02 - February
03 - March
.
.
.
12 - December
How can I do this with variable and mapping in XSLT. I am using XSLT 2.0

Well, you could do simply:
<xsl:choose>
<xsl:when test="$month=1">January</xsl:when>
<xsl:when test="$month=2">February</xsl:when>
<xsl:when test="$month=3">March</xsl:when>
<xsl:when test="$month=4">April</xsl:when>
<xsl:when test="$month=5">May</xsl:when>
<xsl:when test="$month=6">June</xsl:when>
<xsl:when test="$month=7">July</xsl:when>
<xsl:when test="$month=8">August</xsl:when>
<xsl:when test="$month=9">September</xsl:when>
<xsl:when test="$month=10">October</xsl:when>
<xsl:when test="$month=11">November</xsl:when>
<xsl:when test="$month=12">December</xsl:when>
</xsl:choose>
In XSLT 2.0 you can do:
<xsl:value-of select="format-date(xs:date(concat('0001-', $month, '-01')), '[MNn]')"/>
(this is assuming your $month variable is already zero-padded to two digits).

Related

XSL How to find the greater of 2 strings?

Have the below newbie question on how to compare and find the greater of 2 strings please -
<xsl:variable name="String1" select="ABC"/>
<xsl:variable name="String2" select="DEF"/>
My focus of comparison between the 2 strings is based on the first letter being greater than the other, so, i did this -
<xsl:variable name="String1First" select="substring($String1,1,1)"/>
<xsl:variable name="String2First" select="substring($String2,1,1)"/>
so i have the values of the first letters in string1First & String2First for the compare.
Now the actual comparison is the issue - just tried to check if string1 > string2 but that didnt give me the right result obviously.
<xsl:variable name="Output">
<xsl:choose>
<xsl:when test="'$String1First' > '$String2First'">
<xsl:text> First string is greater </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> Second string is greater </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
tried using compare function, but that didn't seem to work.
<xsl:variable name="Output">
<xsl:choose>
<xsl:when test="(compare('$String1First', '$String2First')) = 1">
<xsl:text> First string is greater </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text> Second string is greater </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
also tried:
<xsl:variable name="Alphabet" select="ABCDEFGHIJKLMNOPQRSTUVWXYZ"/>
<xsl:number name="PosNo" select="index-of-string($Alphabet,$String1First)">
<xsl:number name="PosNo2" select="index-of-string($Alphabet,$String2First)">
to compare as numbers by taking the position, but got the whole format wrong.
Could you please help with the easiest way to achieve this please?
Many thanks for your kind assistance.

decimal format in XSLT along with characters

I'm completely new to XSL and I have a query on how to convert a number into decimal format when we have text in the XML node.
For example:
<Salary> 15000 USD </Salary>
Expected out:
15,000 USD
I've used the below transformation (which I found here), however it only converts when there is no text:
<xsl:choose>
<xsl:when test="$Salary >= 1000">
<xsl:value-of select="format-number(floor($Salary div 1000), '#,##')" />
<xsl:text>,</xsl:text>
<xsl:value-of select="format-number($Salary mod 1000, '000')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number($Salary, '#,###')" />
</xsl:otherwise>
</xsl:choose>
Any help would be really appreciated.
First thing, you need to convert the input to a number by removing all non-digit characters.
Assuming you are using XSLT 1.0, that would be done as:
<xsl:variable name="salary" select="translate(Salary, translate(Salary, '0123456789', ''), '')"/>
Here we assume the input will be always a positive integer - hence no minus sign or decimal point.
Once you have that, you can proceed to format the resulting number as:
<xsl:choose>
<xsl:when test="$salary >= 1000">
<xsl:value-of select="format-number(floor($salary div 1000), '#,##')" />
<xsl:text>,</xsl:text>
<xsl:value-of select="format-number($salary mod 1000, '000')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="format-number($salary, '#,###')" />
</xsl:otherwise>
</xsl:choose>
<xsl:text> USD</xsl:text>
Note:
It looks like you want to format the number as Indian currency - which seems very strange, given that the amount is in USD.

How to change a variable value using choose and when

I'm trying to built some rules around a calculated variable value, but am struggling to get the rules to work with an output of false being returned if either or both criteria are used. I need to use both qualifying criteria, but in the process of bug testing tried each argument individually.
I have a variable called "calculation". However when a discount amount is applied to it, I do not want the value of the calculation to be below 301.12. This however is only the case when a discount amount has been applied. If the value of the calculation is below 301.12 without a discount then this is OK, and the lower value is acceptable.
What is happening however is the the lower value after the discount has been applied is still being returned when the process is run.
All help is greatly appreciated.
Here is my code:
<xsl:variable name="discountAmount">
<xsl:choose>
<xsl:when test="#affiliateID='12345'">50</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="feeAmount">
<xsl:choose>
<xsl:when test="#affiliateID='12345'">25</xsl:when>
<xsl:otherwise>50</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="grossAmount" select="format-number(db1:grossPremium, '#0.00')" />
<xsl:variable name="discountGiven" select="($grossAmount - $feeAmount) div 100 * $discountAmount" />
<xsl:variable name="calculation" select="($grossAmount - $discountGiven)" />
<xsl:choose>
<xsl:when test="$discountAmount > 0">
<xsl:choose>
<xsl:when test="$calculation < 301.12">
<xsl:value-of select="$calculation=301.12" />
</xsl:when>
</xsl:choose>
</xsl:when>
</xsl:choose>
You cannot change values of variables in XSLT. Try it like this:
<xsl:variable name="calc0" select="($grossAmount - $discountGiven)" />
<xsl:variable name="calculation">
<xsl:choose>
<xsl:when test="$discountAmount > 0 and $calc0 < 301.12">
301.12
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$calc0" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

Comparing negative number with xsl choose

i am trying to do equal comparison of negative number, but i dont see any output. below is the code.
<xsl:variable name="OwnershipStatus" select="Veh_Ownsp[Veh_Ownsp_ID=$OwnerData1/Veh_Ownsp_ID]/Ownsp_Cntl_Type_ID"/>
Here OwnershipStatus is returninenter code hereg -1
xsl:choose>
<xsl:when test="$OwnershipStatus = -1" >
<xsl:value-of select="Or"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="And"/>
</xsl:otherwise>
</xsl:choose>
I guess you are trying to hardcode 'Or' and 'And' in case of condition getting evaluated to true and false, respectively. Since the values are literal they should be enclosed with single quotes as shown below:
<xsl:choose>
<xsl:when test="$OwnershipStatus = -1" >
<xsl:value-of select="'Or'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'And'"/>
</xsl:otherwise>

XSLT: Calculating year based on non January start

I'm trying to calculate a year based on a year starting 6th April.
Using EXSLT I can get the year based on a normal January start:
date:formatDate(date:add(date:date(), '-P6Y'), 'yyyy')
How can I do the same but for a year starting 6th April.
Thanks.
Something like this
<xsl:choose>
<xsl:when test="(date:month-in-year() = 4 and date:day-in-month() <= 6) or (date:month-in-year() < 4)">
<xsl:value-of select="date:formatDate(date:add(date:date(), '-P7Y'), 'yyyy')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="date:formatDate(date:add(date:date(), '-P6Y'), 'yyyy')" />
</xsl:otherwise>
</xsl:choose>