Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old November 20th, 2008, 04:11 PM
v01 v01 is offline
Registered User
Join Date: Nov 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Transform String, Removing Characters.


I need to transform the following string: "2009-01-30T20:00:00Z" into "20090130T200000Z".
Also need to create another string by adding "3" to the value right after "T" so "20090130T200000Z" becomes "20090130T230000Z"

The initial string is a variable so I need some sort of a template that would work for other strings formatted like the example given. Addition is always the same "+3" to the value of two digits after "T"


Old November 21st, 2008, 01:44 AM
Friend of Wrox
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts

Try this:
<xsl:template match="/">
        <xsl:variable name="a1">2009-01-30T20:00:00Z</xsl:variable>
        <xsl:variable name="a2"><xsl:value-of select="tokenize($a1, 'T')[1]"/></xsl:variable>
        <xsl:variable name="a3"><xsl:value-of select="tokenize($a1, 'T')[2]"/></xsl:variable>
        <xsl:variable name="a4"><xsl:value-of select="tokenize($a3, ':')[1]"/></xsl:variable>
        <xsl:variable name="a5"><xsl:value-of select="$a4 + 3"/></xsl:variable>
        <xsl:value-of select="replace(concat($a2, 'T', replace($a3, $a4, $a5)), '-|:', '')"></xsl:value-of>

There can be better solution than this.

Old November 21st, 2008, 09:40 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

For the first part use the translate function, translate($x, '-:', ''). to remove all - and : replace with nothing. For the second part you could use substring and substring-before to extract the digits you need. What happens if the time is after 21? You will end up with a time of 25 or more, is that acceptable?


Joe (Microsoft MVP - XML)
Old November 21st, 2008, 01:58 PM
v01 v01 is offline
Registered User
Join Date: Nov 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

Thanks guys, I went the translate way to get this done.
Question about time after 21 is totally valid BUT I'm lucky in the way that my data set never exceed 20, and if at some point it does, i'll just put a when/otherwise and add 2 or 1 instead of 3, it will make sense for my purpose.

Once again thanks for replies.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing leading zeros from a string value vikkiefd XSLT 2 June 12th, 2008 03:30 AM
Removing Double Quotes from a string Durkee VB.NET 2002/2003 Basics 12 October 4th, 2007 12:10 PM
Removing Double Qoutes from string hexOffender VB.NET 2002/2003 Basics 2 June 26th, 2006 02:44 PM
Removing Namespaces from XSL Transform output francislang XSLT 2 September 7th, 2004 07:58 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.