When working with XML in the United States (U.S.), one will often find dates which have been formatted in the traditional U.S. Short Format even though XML Schema (XSD) enforces a more locale-neutral format. This means often converting data from:
<SomeUsDate>1/10/2001</SomeUsDate>
Into:
<SomeXsdDate>2001-01-10</SomeXsdDate>
If one is using XSLT 2.0, this can simply be done by including and calling the function within the FunctX library here.
In XSLT 1.0, a very limited set of string manipulation functionality exists. Even so, it is possible (although convoluted) to convert a typical U.S.-formatted date into an XML-Schema enforced date. A possible solution is listed below:
<xsl:template name="aoc:txfDateFormat">
<xsl:param name="UsDate"/>
<xsl:choose>
<!-- Test to see if date contains a date with slashes in it. -->
<xsl:when test="contains($UsDate, '/')">
<xsl:choose>
<!-- 2 Digit Month, 2 Digit Day -->
<xsl:when
test="string-length(substring-before($UsDate, '/'))=2 and (string-length(substring-before(substring-after($UsDate, '/'), '/'))=2)">
<nc:Date>
<xsl:value-of
select="concat(substring-after(substring-after($UsDate, '/'), '/'),'-',substring-before($UsDate, '/'), '-', substring-before(substring-after($UsDate, '/'), '/'))"
/>
</nc:Date>
</xsl:when>
<!-- 1 Digit Month, 2 Digit Day -->
<xsl:when
test="string-length(substring-before($UsDate, '/'))=1 and (string-length(substring-before(substring-after($UsDate, '/'), '/'))=2)">
<nc:Date>
<xsl:value-of
select="concat(substring-after(substring-after($UsDate, '/'), '/'),'-0',substring-before($UsDate, '/'), '-', substring-before(substring-after($UsDate, '/'), '/'))"
/>
</nc:Date>
</xsl:when>
<!-- 2 Digit Month, 1 Digit Day -->
<xsl:when
test="string-length(substring-before($UsDate, '/'))=2 and (string-length(substring-before(substring-after($UsDate, '/'), '/'))=1)">
<nc:Date>
<xsl:value-of
select="concat(substring-after(substring-after($UsDate, '/'), '/'),'-',substring-before($UsDate, '/'), '-0', substring-before(substring-after($UsDate, '/'), '/'))"
/>
</nc:Date>
</xsl:when>
<!-- 1 Digit Month, 1 Digit Day -->
<xsl:when
test="string-length(substring-before($UsDate, '/'))=1 and (string-length(substring-before(substring-after($UsDate, '/'), '/'))=1)">
<nc:Date>
<xsl:value-of
select="concat(substring-after(substring-after($UsDate, '/'), '/'),'-0',substring-before($UsDate, '/'), '-0', substring-before(substring-after($UsDate, '/'), '/'))"
/>
</nc:Date>
</xsl:when>
</xsl:choose>
</xsl:when>
<!-- Omit element if not. -->
<xsl:otherwise/>
</xsl:choose>
</xsl:template>
No comments:
Post a Comment