<?xml version="1.0" encoding="UTF-8"?>
<clientlist>
<client>
<data key="id" value="111" />
<data key="name" value="The Parlotones" />
<data key="genre" value="Rock / Alternative" />
<data key="description" value="The Parlotones are known for their electric, polished stage performances delivered against the backdrop of their deftly crafted and darkly romantic lyrics." />
<data key="performanceday" value="Sunday" />
<data key="performancetime" value="01PM-03PM" />
<data key="picture" value="the-parlotones.jpg" />
</client>
<client>
<data key="id" value="222" />
<data key="name" value="ShortStraw" />
<data key="genre" value="Folk / Acoustic" />
<data key="description" value="Shortstraw are a joburg based band making waves on the national indie music scene in a big way." />
<data key="performanceday" value="Sunday" />
<data key="performancetime" value="03PM-05PM" />
<data key="picture" value="shortstraw.jpg" />
</client>
<client>
<data key="id" value="333" />
<data key="name" value="Gangs of Ballet" />
<data key="genre" value="Dance / Club" />
<data key="description" value="Their music, which combines their fresh energy with their musically intriguing melodies and arrangements, has a hauntingly anthemic sound." />
<data key="performanceday" value="Saturday" />
<data key="performancetime" value="11AM-01PM" />
<data key="picture" value="gangs-of-ballet.jpg" />
</client>
</clientlist>
I would like help converting 3PM/3AM times from my source file, into digital times, eg (15:00) in my output file using XSL and BASH, with the output being a XML document.
This is what I have so far in xsl :
<xsl:if test="not(PM)">
<starts>
<xsl:value-of select="number(substring before(data[@key='performancetime']/@value, '-'))" ></xsl:value-of>
</starts>
This results are NaN value appearing in all the "starts" and "ends" times.
I really need some help, Thank you :)
Consider making a named template to do the conversion, then you can call it separate for the start and end time. I am making the assumption the input will only contain an hour element, and not minutes (i.e is of the form "hhAM" or "hhPM")
<xsl:template name="convertTime">
<xsl:param name="time" />
<xsl:choose>
<xsl:when test="contains($time, 'PM')"><xsl:value-of select="number(substring($time, 1, 2)) + 12" /></xsl:when>
<xsl:otherwise><xsl:value-of select="substring($time, 1, 2)" /></xsl:otherwise>
</xsl:choose>
<xsl:text>:00</xsl:text>
</xsl:template>
Then you can call it like this for the start time, for example
<xsl:call-template name="convertTime">
<xsl:with-param name="time" select="substring-before(data[@key = 'performancetime']/@value, '-')" />
</xsl:call-template>
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="client">
<xsl:variable name="allTimes" select="data[@key = 'performancetime']/@value" />
<times>
<start>
<xsl:call-template name="convertTime">
<xsl:with-param name="time" select="substring-before($allTimes, '-')" />
</xsl:call-template>
</start>
<end>
<xsl:call-template name="convertTime">
<xsl:with-param name="time" select="substring-after($allTimes, '-')" />
</xsl:call-template>
</end>
</times>
</xsl:template>
<xsl:template name="convertTime">
<xsl:param name="time" />
<xsl:choose>
<xsl:when test="contains($time, 'PM')"><xsl:value-of select="number(substring($time, 1, 2)) + 12" /></xsl:when>
<xsl:otherwise><xsl:value-of select="substring($time, 1, 2)" /></xsl:otherwise>
</xsl:choose>
<xsl:text>:00</xsl:text>
</xsl:template>
</xsl:stylesheet>
I would suggest you try it along the lines of:
<xsl:template match="client">
...
<xsl:variable name="times" select="data[@key='performancetime']/@value"/>
<starts>
<xsl:call-template name="time24">
<xsl:with-param name="time" select="substring-before($times, '-')" />
</xsl:call-template>
</starts>
<ends>
<xsl:call-template name="time24">
<xsl:with-param name="time" select="substring-after($times, '-')" />
</xsl:call-template>
</ends>
...
</xsl:template>
<xsl:template name="time24">
<xsl:param name="time" />
<xsl:variable name="h12" select="substring($time, 1, 2)"/>
<xsl:variable name="pm" select="contains($time,'P')"/>
<xsl:variable name="h24" select="$h12 mod 12 + 12*$pm"/>
<xsl:value-of select="format-number($h24, '00')"/>
<xsl:text>:00</xsl:text>
</xsl:template>