DateTime from xslt

Hello, I’m trying to get a date and time from an XML. In the XML it’s formatted as

<start_time>10.4.2017 21:00:00</start_time>

I’ve (keep in mind I’m a total amateur) come up with the following xsl:

<?xml version="1.0"?>
<xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

        <xsl:output indent="yes" encoding="UTF-8" method="text" omit-xml-declaration="yes" />

    <xsl:template match="rsp/recordings">
            <xsl:variable name="in" select="recording[2]/start_time"/>
            <xsl:variable name="datepart" select="substring-before($in, ' ')"/>
            <xsl:variable name="timepart" select="substring-after($in, ' ')"/>
            <xsl:variable name="year" select="substring-after(substring-after($datepart, '.'), '.')"/>
            <xsl:variable name="month" select="format-number(substring-before(substring-after($datepart, '.'), '.'), '00')"/>
            <xsl:variable name="day" select="format-number(substring-before($datepart, '.'), '00')"/>
            <xsl:value-of select="concat($year,'-',$month,'-',$day,'T',$timepart)"/>
    		
    </xsl:template>
</xsl:stylesheet>

which gives me

2017-04-10T21:00:00

I felt kinda proud of myselft for accomplishing that :slight_smile: Even though I’m sure there’s a much easier way.
Now if my item is a string, that gets shown, so I know it’s linked to the item. If however I change it to a DateTime, it goes NULL. How should the string be formatted to be recognized as a proper date and time?

Some more info; if I have it as a string and look at the rest API items, i see this as the value: “\r\n2017-04-10T21:00:00\r\n”. Could the newline-characters be the cause of it not identifying as a DateTime? And if so, how in the world do I get rid of them?

I tried getting just the year-part as a formatted number and that didn’t work either. As a string it returns “\r\n2017.0\r\n” and NULL when set to number. Highly frustrating, I must be missing something very stupid or then there’s something wrong with the HTTP binding when using OpenHAB 2.0, I highly doubt the latter.

Well it was very simple. Changing to:

<?xml version="1.0"?>
<xsl:stylesheet 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

        <xsl:output indent="yes" encoding="UTF-8" method="text" omit-xml-declaration="yes" />

    <xsl:template match="/">
            <xsl:variable name="in" select="rsp/recordings/recording[1]/start_time"/>
            <xsl:variable name="datepart" select="substring-before($in, ' ')"/>
            <xsl:variable name="timepart" select="substring-after($in, ' ')"/>
            <xsl:variable name="year" select="substring-after(substring-after($datepart, '.'), '.')"/>
            <xsl:variable name="month" select="format-number(substring-before(substring-after($datepart, '.'), '.'), '00')"/>
            <xsl:variable name="day" select="format-number(substring-before($datepart, '.'), '00')"/>
            <xsl:value-of select="concat($year,'-',$month,'-',$day,'T',$timepart)"/>
    		
    </xsl:template>
</xsl:stylesheet>

Resolved it. Had to change template to match whole document ja select to traverse the tree. That removed the newline characters, which indeed solved the problem. I’m patting myself on the back for a job well done :smiley: