1

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>

4

1 回答 1

1

一种方法是,作为匹配完整节点的示例

<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>

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

这将使用空格替换 T 并使用translate()删除最后 6 个字符substring()

更新:如评论中所述,如果日期始终采用 hh:mm:ss 格式,则可以删除string-length()in并将选择表达式简化为substring()<xsl:value-of select="substring(translate(.,'T', ' '),1,19)"/>

对于刚刚添加的原始模板匹配order:如果您只是更改它应该可以工作:

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

进入这个:

Order Date: <xsl:value-of select="substring(translate(created-at,'T', ' '),1,19)" />
于 2014-12-29T19:44:17.823 回答