xmlxsltxml-parsingshopifyxml-formatting

Parse XSL Date Format


I'm making an XSL stylesheet to format the XML date output by Shopify.

What is output is;

<created-at type="dateTime">2014-12-23T14:27:53-05:00</created-at>

And I need to adjust it to look like

<created-at type="dateTime">2014-12-23 14:27:53</created-at>

I'm just not sure how to remove the 'T' or clip the time-zone stamp at the end.

Any suggestions?

Current XSL Stylesheet (Order Date is what I'm trying to adjust);

<xsl:template match="order">
    <html>
        <head>
            <title>St. Baldrick's Order #<xsl:value-of select="order-number" /></title>
        </head>
        <body>
            <p>
                Order Number: <xsl:value-of select="order-number" /><br /><!-- Their Order Number -->
                Client ID: <xsl:value-of select="id" /><br /><!-- Internal Compass ID or Customer Account Number -->
                First Name: <xsl:value-of select="first-name" /><br /><!-- Ship-to first name -->
                Last Name: <xsl:value-of select="last-name" /><br /><!-- Ship-to last name -->
                Order Date: <xsl:value-of select="created-at" /><br /><!-- Order Date -->
            </p>
        </body>
    </html>
</xsl:template>


Solution

  • One way to do that would be, just as example for matching the complete node

    <xsl:template match="created-at">
      <xsl:copy>
       <xsl:attribute name="type" select="@type"/>
         <xsl:value-of select="substring(translate(.,'T', ' '),1,string-length()-6)"/>
      </xsl:copy>
    </xsl:template>
    

    Result: <created-at type="dateTime">2014-12-23 14:27:53</created-at>

    This replaces the T with a space using translate() and cuts of the last 6 characters using substring().

    Update: As mentioned in the comment, in case the date is always in the format hh:mm:ss, the string-length() in the substring() can be removed and the select expression simplified to <xsl:value-of select="substring(translate(.,'T', ' '),1,19)"/>

    For the just added original template matching order: It should work if you just change this:

    Order Date: <xsl:value-of select="created-at" />
    

    into this:

    Order Date: <xsl:value-of select="substring(translate(created-at,'T', ' '),1,19)" />