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 March 6th, 2014, 07:04 AM
Authorized User
 
Join Date: Aug 2013
Posts: 30
Thanks: 9
Thanked 0 Times in 0 Posts
Default change date format with regex

Hi,

I have an html table as source code with this kind of information in it
Code:
<TD>8/11/2013 10:01:21</TD>
and I want to insert in my result xml a different format of date.

I have done like this but I believe there is a mistake.

Code:
<xsl:variable name="d">
        <xsl:analyze-string select="normalize-space(TD[position()=1])" regex="(\d*)/(\d*)/(\d\d\d\d)">
            <xsl:matching-substring>
                <xsl:value-of select="concat(regex-group(3),'-',regex-group(1),'-',regex-group(2))"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:variable>

...
 <xsl:template match="/">
<change>
<xsl:attribute name="when"><xsl:value-of select="$d"/></xsl:attribute>
</change>
</xsl:template>
I would like the result to be

Code:
<change when="2013-08-11"></change>
Thank you very much for any help.
 
Old March 6th, 2014, 07:19 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If I run this stylesheet:

Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xxx="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  
    <xsl:variable name="d">
        <xsl:analyze-string select="normalize-space(TD[position()=1])" regex="(\d*)/(\d*)/(\d\d\d\d)">
            <xsl:matching-substring>
                <xsl:value-of select="concat(regex-group(3),'-',regex-group(1),'-',regex-group(2))"/>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="."/>
            </xsl:non-matching-substring>
        </xsl:analyze-string>
    </xsl:variable>


 <xsl:template match="/">
   <change>
    <xsl:attribute name="when"><xsl:value-of select="$d"/></xsl:attribute>
    </change>
</xsl:template>
</xsl:stylesheet>
against this input

Code:
<TD>8/11/2013 10:01:21</TD>
I get this output:

Code:
<change when="2013-8-11 10:01:21"/>
So you're almost there; the only thing that remains is to add the leading zeros, which you can do by changing the value-of instruction to

Code:
<xsl:value-of select="concat(format-number(number(regex-group(3)), '00'),'-',
                format-number(number(regex-group(1)), '00'),'-',regex-group(2))"/>
and if you don't want the time included in the result, then remove the value-of instruction within xsl:non-matching-substring.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
pietromaria.liuzzo (March 8th, 2014)





Similar Threads
Thread Thread Starter Forum Replies Last Post
CHANGE DATE FORMAT Vision G Access VBA 2 April 8th, 2008 12:16 PM
how to change date format prasannagps Forum and Wrox.com Feedback 2 September 14th, 2005 01:28 PM
need to change date format desprate Access VBA 1 April 10th, 2005 12:45 PM
Change date format Patrick19 Classic ASP Basics 1 March 14th, 2005 06:23 PM
date format change yuvalk SQL Server DTS 2 March 29th, 2004 05:14 AM





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