DateTime Parsing in XSLT Xp20 - xslt

I am having the Date field as below
<Input>
<date>08/26/2020</date>
</Input>
i would need to parse it as like below
<date>2020-08-26</date>
I have tried using below xpath funtion in XSLT, which is not producing any result
xp20:format-dateTime(/Input/date,'[Y0001]-[M01]-[D01]')
Any help here??

Using replace you can reorder the components, if you want to create an XSLT/XPath xs:date, additionally use the constructor function:
<xsl:template match="date">
<xsl:copy>
<xsl:value-of select="xs:date(replace(., '([0-9]{2})/([0-9]{2})/([0-9]{4})', '$3-$1-$2'))"/>
</xsl:copy>
</xsl:template>

What you could do is, extract sub strings, and re-join them as needed.
<date><xsl:value-of select="concat(substring-after(substring-after(date/text(),'/'),'/'), '-', substring-before(date/text(),'/'), '-', substring-before(substring-after(date/text(),'/'),'/'))" /></date>
or better use a template for this:
<xsl:template name="format_date">
<xsl:param name="date" />
<xsl:value-of select="concat( substring($date, 7, 2),'.',substring($date, 5, 2), '.', substring($date, 1, 4), ', ', substring($date, 9, 2),':',substring($date, 11, 2),'h' )" />
</xsl:template>
and pass the date as param (with-param).
which'll give you the desired <date>2020-08-26</date>

Related

XSLT 1.0 String Length - 2

I need to check the length of a field and then add . before last two
digits.
Example: the value of Amount is 0001234567, to be replaced as 00012345.67. Here string length will be 10.
But the command fails and is not able to retrieve the value from
($VARAmtLength-2) or ($VARAmtLength-1).
My code as below:
<xsl:variable name="VARAmtLength" select="string-length (ns0:Amount )"/>
<xsl:if test=" ($VARAmtLength> 0)">
<tns:Amount>
<xsl:value-of select="concat(substring(ns0:Amount, 1, ($VARAmtLength- 2)),'.', substring(ns0:Amount, ($VARAmtLength-1, 2)))"/>
</tns:Amount>
</xsl:if>
Any help?
I think your code is working fine.
Just replace this line with existing one:
<xsl:value-of select="concat(substring(ns0:Amount, 1, ($VARAmtLength - 2)),'.', substring(ns0:Amount, ($VARAmtLength - 1), 2))" />
1. There should be a space around subtraction operator '-'. Otherwise it will consider $VARAmtLength- as variable name.
2. You had misplaced round parentheses for second substring() function.
XML
<amount>0001234567</amount>
Xsl
<xsl:template match="/">
<xsl:variable name="length" select="//amount"/>
<xsl:if test="$length>0">
<amount>
<xsl:variable name="ajeet" select="concat(substring(//amount, 1, 8), '.')"/>
<xsl:variable name="kumar" select="substring(//amount, 9, 2)"/>
<xsl:value-of select="concat($ajeet, $kumar)"/>
</amount>
</xsl:if>
</xsl:template>

Can I not access my xsl:variable this way when I'm outputting plain text

I'm doing an xslt transform that generates a sql statement for me. The way I'm using below is not working. Is there a way?
<xsl:template match="foo">
<xsl:variable name="var1" select="#att_val1" />
select $var1.* from $var1
</xsl:template>
I know it will work if I do this:
<xsl:template match="foo">
select <xsl:value-of select="#att_val1" />.* from <xsl:value-of select="#att_val1" />
</xsl:template>
In XSLT 1.0, variable references are recognized in XPath expressions, but not in general template text. To evaluate an XPath expression and output the result as a text node in the result tree, use xsl:value-of, as you already know how to do. Example:
<xsl:template match="foo">
<xsl:variable name="var1" select="#att_val1" />
select <xsl:value-of select="$var1"/>.* from <xsl:value-of select="$var1"/>
</xsl:template>
Alternatively, you could build the whole select command in one xsl:value-of with use of the concat() function.
Unless you move to XSLT 3.0 (https://www.w3.org/TR/xslt-30/#text-value-templates) where you can do e.g. <xsl:template match="foo" expand-text="yes">select {#att_val1}.* from {#att_val1}</xsl:template> you will have to use your second option or perhaps a <xsl:template match="foo"><xsl:value-of select="concat('select ', #att_val1, '.* from ', #att_val1)"/></xsl:template>, but there is certainly no way in XSLT 1.0 to avoid the use of xsl:value-of completely.

Best way to remove a leading zero after formatting a date in xslt 1.0

I've got a date format template that I'm passing a date value to in the format YYYYMMDD
The template is the following:
<xsl:template name="formatDate">
<xsl:param name="date" />
<xsl:variable name="year" select="substring($date, 1, 4)" />
<xsl:variable name="month" select="substring($date, 5, 2)" />
<xsl:variable name="day" select="substring($date, 7, 2)" />
<xsl:value-of select="concat($month, '/', $day, '/', $year)" />
</xsl:template>
This would return the string 20131004 as 10/04/2013 which is correct.
What I need to do though is if the $month has a leading zero, to remove it. For example, 20130930 would be 09/30/2013 when I would prefer 9/30/2013.
What's the most efficient way to do that? I could do a choose/when before I set the value of the variable but I'm trying to do it in the proper manner with xslt (I'm still trying to get into it, it's coming along).
Thanks
You could utilize number() function
<xsl:variable name="month" select="number(substring($date, 5, 2))" />
<xsl:variable name="day" select="number(substring($date, 7, 2))" />
It should remove leading zero.

Replacing specific character to lowercase using xsl

Hi i have an xml as below.
<setField identifier=”2”>
<fieldValues>
<fieldValue>
<field>event</field>
<value>
<boundVariable>$event1</boundVariable>
</value>
<type>java.lang.String</type>
</fieldValue>
</fieldValues>
<variable>append</variable>
</setField>
I need to convert to the following format.
<freeForm><text>append.setEvent($event1);</freeForm></text>
I am trying the following approach.
<xsl:template match="setField" name="setFieldTemplate">
<xsl:element name="freeForm">
<xsl:element name="text">
<xsl:value-of select="variable" />
<xsl:text>.set</xsl:text>
<xsl:value-of select="concat(translate(substring(field, 1, 1)"/>
<xsl:text>(</xsl:text>
<xsl:value-of select="boundVariable"/>
<xsl:text>);</xsl:text>
</xsl:element>
</xsl:element>
</xsl:template>
Here my requirment is based on the field name i need to generate corresponding statement as below.For that i need to change the first character of the field name to upper while generating to the following format.
append.setEvent($event1);
Here the field name is "event" and i need to generate setEvent(for which i am concatinating with the string "set").But i need to change the fieldname's first letter to upper one(Event from event).when i try with the above template with translate function i am facing some invalid xpath expression.
Please provide me some pointers to do the same.
<xsl:value-of select="concat(
variable,
'.set',
translate(substring(//field, 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
substring(//field, 2),
'(',
//boundVariable,
');'
)"/>
You want:
<xsl:value-of select="concat(
translate(substring(//field, 1, 1),
'abcdefghijklmnstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
substring(//field, 2))"/>
Also you might prefer:
<xsl:template match="setField">
<freeForm>
<text>
<xsl:value-of select="variable" />
<xsl:text>.set</xsl:text>
<xsl:value-of select="concat(
translate(
substring(fieldValues/fieldValue/field, 1, 1),
'abcdefghijklmnstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
substring(fieldValues/fieldValue/field, 2),
'(',fieldValues/fieldValue/value/boundVariable,');')"/>
</text>
</freeForm>
</xsl:template>

SSN format in XSLT?

I could use some help creating an XSL template that will take a string of numbers (i.e., 123456789) and format that string into a Social Security Number format (i.e., 123-45-6789). I found one example on the Internet, but it seemed overcomplicated.
I'm new to XSLT, so please keep that in mind when replying. Thank you!
XSLT 1.0's string functions are a bit limited, but fortunately this isn't too hard:
Assuming < ssn >123456789< /ssn>:
<xsl:template match="ssn">
<xsl:value-of select="substring(., 0, 4)"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="substring(., 4, 2)"/>
<xsl:text>-</xsl:text>
<xsl:value-of select="substring(., 6, 4)"/>
</xsl:template>
In XSLT 2.0, concat() can take more than two arguments, so it's a single line:
<xsl:template match="ssn">
<xsl:value-of select="concat(substring(., 0, 4), '-', substring(., 4, 2), '-', substring(., 6, 4))" />
</xsl:template>