How to display date value in XSLT transformation

318 Views Asked by At

My UI supplies the user selected date in milliseconds to the backend rest layer.

For e.g Say the user selects "07/11/2018" from the UI then it gets passed on to the REST layer as milliseconds '1541509200000'. And the REST layer maps this value to a 'XMLGregorianCalendarObject' within my DTO.

import java.io.Serializable;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.bind.annotation.XmlSchemaType;

public class PersonDetails implements Serializable
{

    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar dateOfBirth;

    public XMLGregorianCalendar getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(XMLGregorianCalendar value) {
        this.dateOfBirth = value;
    }

}

And this DTO gets converted to an XML and stored. The XML payload looks like below :

    <personDetails>
       <dateOfBirth>2018-11-06Z</dateOfBirth>
    </personDetails>

I have the below XSLT code which is currently just displaying the above dateOfBirth element :

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">    
        <div>
            <div> Date of birth: </div>
            <div> <xsl:value-of select="//personDetails/dateOfBirth" /> </div>
        </div>
</xsl:template>

And it generates the output

Date of birth: 2018-11-06Z

What should i do to display the dateOfBirth as the original user selected 07/11/2018 in the XSLT transformation .

1

There are 1 best solutions below

2
On

XSLT 1.0 has no concept of dates. You must manipulate the input using string functions:

<xsl:template match="personDetails">    
    <div>
        <div> Date of birth: </div>
        <div>
            <!-- day-->
            <xsl:value-of select="substring(dateOfBirth, 9, 2)" />
            <xsl:text>/</xsl:text>
            <!-- month-->
            <xsl:value-of select="substring(dateOfBirth, 6, 2)" />
            <xsl:text>/</xsl:text>
            <!-- year-->
            <xsl:value-of select="substring(dateOfBirth, 1, 4)" />
        </div>
    </div>
</xsl:template>