XSL Help formating dates
Here should go questions about transforming XML with XSLT and FOP.
-
- Site Admin
- Posts: 2095
- Joined: Thu Jan 09, 2003 2:58 pm
Re: XSL Help formating dates
Hi,
Let's suppose you have
and you want as output
Then, the following stylesheet will do that. It gets the year, month and day with substring before and after functions and then adds a leading zero if necessary:
Best Regards,
George
Let's suppose you have
Code: Select all
<dates>
<date>11/12/03</date>
<date>1/12/03</date>
<date>1/2/03</date>
<date>11/2/03</date>
</dates>
Code: Select all
<dates>
<date>03-11-12</date>
<date>03-01-12</date>
<date>03-01-02</date>
<date>03-11-02</date>
</dates>
Code: Select all
<?xml version="1.1" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="dates">
<dates><xsl:apply-templates/></dates>
</xsl:template>
<xsl:template match="date">
<xsl:variable name="year" select="substring-after(substring-after(., '/'),'/')"/>
<xsl:variable name="month" select="substring-before(.,'/')"/>
<xsl:variable name="day" select="substring-before(substring-after(., '/'),'/')"/>
<date>
<xsl:value-of select="$year"/>
<xsl:text>-</xsl:text>
<xsl:if test="string-length($month)=1">0</xsl:if>
<xsl:value-of select="$month"/>
<xsl:text>-</xsl:text>
<xsl:if test="string-length($day)=1">0</xsl:if>
<xsl:value-of select="$day"/>
</date>
</xsl:template>
</xsl:stylesheet>
George
George Cristian Bina
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service