I need to convert ordinal dates like 2011001 (Jan 1, 2011) to MM/dd/yyyy format (01/01/2011). XSLT 2.0 is OK.
I may not have asked this correctly; I'm not sure what additional information one would need to help solve this issue. Let me know and I will provide 开发者_Python百科any needed info. Any solutions or pointers appreciated.
This will work in XSLT 2.0:
<xsl:template match="date">
<xsl:copy>
<xsl:analyze-string select="." regex="([0-9]{{4}})([0-9]{{3}})">
<xsl:matching-substring>
<xsl:variable name="jan1" as="xs:date" select="xs:date(fn:concat(fn:regex-group(1),'-01-01'))"/>
<xsl:variable name="offset" as="xs:dayTimeDuration" select="xs:dayTimeDuration(fn:concat('P',number(fn:regex-group(2))-1,'D'))"/>
<xsl:value-of select="format-date($jan1+$offset, '[M,2]/[D,2]/[Y]')"/>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:copy>
</xsl:template>
The secret is to use the ordinal as an offset from Jan 1 of the supplied year. Then, format-date can output the date in any format you want.
You could it using substring() instead of xsl:analyze-string but this way lets you ensure you only try to convert dates in the proper format.
精彩评论