Skip to content

Instantly share code, notes, and snippets.

@gjbroom
Created October 19, 2012 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjbroom/9a1f296a743030796987 to your computer and use it in GitHub Desktop.
Save gjbroom/9a1f296a743030796987 to your computer and use it in GitHub Desktop.
XSL to transform Unity Pro date stamps in .XEF file to ISO8601 format
<!-- Fun fun! Parse Unity Pro date stamp and normalize to ISO8601. Add leading zeros to fields if they aren't there. -->
<xsl:variable name="fullTs"><xsl:value-of select="substring-after(/FEFExchangeFile/fileHeader/@dateTime, 'date_and_time#')"/></xsl:variable>
<xsl:variable name="year"><xsl:value-of select="substring-before($fullTs, '-')" /></xsl:variable>
<xsl:variable name="month"><xsl:value-of select="substring-before(substring-after($fullTs, '-'), '-')"></xsl:value-of></xsl:variable>
<xsl:variable name="day"><xsl:value-of select="substring-before(substring-after(substring-after($fullTs, '-'), '-'), '-')"/></xsl:variable>
<xsl:variable name="hour"><xsl:value-of select="substring-before(substring-after(substring-after(substring-after($fullTs, '-'), '-'), '-'), ':')"/></xsl:variable>
<xsl:variable name="minute"><xsl:value-of select="substring-before(substring-after(substring-after(substring-after(substring-after($fullTs, '-'), '-'), '-'), ':'), ':')"/></xsl:variable>
<xsl:variable name="second"><xsl:value-of select="substring-after(substring-after(substring-after(substring-after(substring-after($fullTs, '-'), '-'), '-'), ':'), ':')"/></xsl:variable>
<!-- then use like this... -->
<xsl:copy-of select="$year" /><xsl:text>-</xsl:text>
<xsl:if test="string-length($month)=1"><xsl:text>0</xsl:text></xsl:if>
<xsl:copy-of select="$month" /><xsl:text>-</xsl:text>
<xsl:if test="string-length($day)=1"><xsl:text>0</xsl:text></xsl:if>
<xsl:copy-of select="$day" />
<xsl:text>T</xsl:text>
<xsl:if test="string-length($hour)=1"><xsl:text>0</xsl:text></xsl:if>
<xsl:copy-of select="$hour" /><xsl:text>:</xsl:text>
<xsl:if test="string-length($minute)=1"><xsl:text>0</xsl:text></xsl:if>
<xsl:copy-of select="$minute" /><xsl:text>:</xsl:text>
<xsl:if test="string-length($second)=1"><xsl:text>0</xsl:text></xsl:if>
<xsl:copy-of select="$second" />
@gjbroom
Copy link
Author

gjbroom commented Oct 20, 2012

This is XSLT 1.0, so we have to split using substring-before and substring-after instead of tokenize. Any text being injected is wrapped in an <xst:text> block so we don't get spurious whitespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment