cancel
Showing results for 
Search instead for 
Did you mean: 

Date conversion issue using XSLT

0 Kudos
231

I am trying to transform the following XML using XSLT

<?xml version="1.0" encoding="UTF-8"?>
<root>
<CandidateId>863</CandidateId>
<CaseId>2762833</CaseId>
<Status>Completed</Status>
<InitiatedDate>01 Dec 2022</InitiatedDate>
<CompletionDate>15 Jan 2022</CompletionDate>
<Comments />
</root>

My XSLT code is as follows:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/">
<JobApplication>
<xsl:for-each select="root">
<JobApplication>
<xsl:variable name="month" select="substring(InitiatedDate,4,3)" />
<xsl:if test="$month = 'Dec'">
<xsl:variable name="m">12</xsl:variable>
</xsl:if>
<xsl:if test="$month = 'Nov'">
<xsl:variable name="m">11</xsl:variable>
</xsl:if>
<xsl:if test="$month = 'Oct'">
<xsl:variable name="m">10</xsl:variable>
</xsl:if>
<Bbgvdate><xsl:value-of select="concat(substring(InitiatedDate,8,4),'-',$m,'-',substring(InitiatedDate,1,2),'T00:00:00')"/></Bbgvdate>
<applicationId><xsl:value-of select="CandidateID"/></applicationId>
</JobApplication>
</xsl:for-each>
</JobApplication>
</xsl:template>
</xsl:stylesheet>

Now, I am unable to call the variable "m" for some reason in "Bbgvdate"

I am looking for a converted date that looks like "2022-12-15" (in yyyy-mm-dd format)

I get the error:

Unable to generate the XML document using the provided XML/XSL input. Errors were reported during stylesheet compilation

Need help with this.

Thanks

Accepted Solutions (0)

Answers (1)

Answers (1)

PriyankaChak
Active Contributor
0 Kudos

Hi Meghana,

The scope of variable m lies within If condition. Thats why it is not accessible.

Use the XSLT code as below and try.

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

exclude-result-prefixes="xs">

<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="node()">

<xsl:apply-templates/>

</xsl:template>

<xsl:template match="/">

<JobApplication>

<xsl:for-each select="root">

<JobApplication>

<xsl:variable name="month" select="substring(InitiatedDate,4,3)" />

<xsl:variable name="m">

<xsl:choose>

<xsl:when test="$month = 'Dec'">

<xsl:text>12</xsl:text>

</xsl:when>

<xsl:when test="$month = 'Nov'">

<xsl:text>11</xsl:text>

</xsl:when>

<xsl:when test="$month = 'Oct'">

<xsl:text>10</xsl:text>

</xsl:when>

</xsl:choose>

</xsl:variable>

<Bbgvdate><xsl:value-of select="concat(substring(InitiatedDate,8,4),'-',$m,'-',substring(InitiatedDate,1,2),'T00:00:00')"/></Bbgvdate>

<applicationId><xsl:value-of select="CandidateID"/></applicationId>

</JobApplication>

</xsl:for-each>

</JobApplication>

</xsl:template>

</xsl:stylesheet>

Regards,

Priyanka