XSLT: Calculating year based on non January start - xslt

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>

Related

When number is given get correspondent month in 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).

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 can I use the value of a variable for the use property of a key in XSLT, I want to achieve use="$Variable" in the key tag;;

Suppose I have a key defined in an xslt file in SharePoint Designer 2010 as:
<xsl:key name="Years" match="/dsQueryResponse/Rows/Row" use="#Date" />
Where #Date is the column, however instead of #Date, I want to use the value of the following variable:
<xsl:variable name="VarNAme">
<xsl:choose>
<xsl:when test="string-length(#Date) = 8">
<xsl:value-of select="substring(#Date, 5, 4)"></xsl:value-of>
</xsl:when>
<xsl:when test="string-length(#Date) = 9">
<xsl:value-of select="substring(#Date, 6, 4)"></xsl:value-of>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring(#Date, 7, 4)"></xsl:value-of>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
If there is a better way (one-liner) to get just the year from a date, I'd welcome that as well. I want to use generate-id to get distinct years (not dates, years).
<xsl:key
name="Years"
match="/dsQueryResponse/Rows/Row"
use="substring(#Date, string-length(#Date) - 3, 4)"
/>
Hint
8 - 3 = 5
9 - 3 = 6
10 - 3 = 7
;-)

Processing an Integer Buried in a String in XSL

I have a stored time value which is of the format H:mm:ss. The hours may be any value from 0 up through several days. This data is sent in an XML tag and processed by XSL to be displayed. The display that I want is of the format:
D days, HH:mm:ss (hours/minutes)
Where the last tag shows hours if HH is greater than 0, minutes if it is 0.
Given the original HH, which may be more than 24, I know I need the floor of HH / 24 to get the days value. Then the original HH % 24 gives me the leftover hours.
I have also handled the minutes and hours question using xsl:when and xsl:if.
It's getting days and hours from the hours value that has me stumped.
EDIT
So far, I'm looking at doing the following:
Variable declaration
<xsl:variable name="time"><xsl:value-of select="time" /><xsl:variable>
<xsl:variable name="days"><xsl:value-of select="floor(substring-before(time, ':') / 24)" /></xsl:variable>
<xsl:variable name="hours"><xsl:value-of select="substring-before(time, ':') mod 24" /></xsl:variable>
<xsl:variable name="minutes"><xsl:value-of select="substring-after(time, ':')" /></xsl:variable>
Use
<xsl:if test="$days > 0">
<xsl:value-of select="$days" /> days
</xsl:if>
<xsl:value-of select="$hours" />:<xsl:value-of select="$minutes" />
<xsl:choose>
<xsl:when test="$hours > 0">
hour<xsl:if test="$hours > 1">s</xsl:if>
</xsl:when>
<xsl:otherwise>
minute<xsl:if test="$minute != '01:00'">s</xsl:if>
</xsl:otherwise>
</xsl:choose>
And for clarification, a sample time would be <time>26:15:00</time> for 1 day 2:15 hours.
Here are the modifications I needed to make my original attempt work. I needed to remember my dollar signs for variables. I needed to use div instead of /. Most importantly, I needed the expression number to cast the substring a number. This is what I've come up with. I welcome commentary that will improve my XSLT, though!
<xsl:variable name="time"><xsl:value-of select="time" /><xsl:variable>
<xsl:variable name="totalHours><xsl:value-of select="number(substring-before($time, ':'))" /></xsl:variable>
<xsl:variable name="days"><xsl:value-of select="floor($totalHours div 24)" /></xsl:variable>
<xsl:variable name="hours"><xsl:value-of select="$totalHours mod 24" /></xsl:variable>
<xsl:variable name="minutes"><xsl:value-of select="substring-after(time, ':')" /></xsl:variable>