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 May 13th, 2008, 12:11 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default Date Validation from String

I have a template below for checking to see if a string is a valid date. The idea here I am getting a document containing a list of dates in string values like so.

<DATES>
<DATE1>20080501</DATE1>
<DATE2>20080901</DATE2>
<DATE3>20080112</DATE3>
....................
<DATE42>20081128</DATE42>
</DATES>

I've written an template that will parse this incoming document and tell you if it is a valid string to represent a date in the YYYYMMDD format. Just so you no the only valid format for my incoming document should be 'YYYYMMDD'. It cannot contain any white space. I thought about using 'matches()' or <xsl:analyze-string>, but I thought this worked better. I just wanted to bounce it off the experts to see if you can spot any major holes. Thanks for your thoughts.

Code:
    
<xsl:template match="DATES/*" mode="YYYYMMDD" >
        <xsl:variable name="FullDate">
            <xsl:value-of select="."/>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="string-length(.)=8">
                <xsl:variable name="FullDateFormat">
                    <xsl:value-of select="concat(substring($FullDate, 1, 4),'-',substring($FullDate, 5, 2),'-',substring($FullDate, 7, 2))"/>
                </xsl:variable>
                <xsl:value-of select="if ($FullDateFormat castable as xs:date) then (concat(local-name(.),' is VALID')) else (concat(local-name(.), ' ***IS NOT A DATE***'))" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="concat(local-name(.), ' ***IS NOT A DATE***')" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>
 
Old May 13th, 2008, 12:21 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

The XML with DATE1, DATE2 and so on looks awful to me.
But the XSLT approach using castable as xs:date looks fine to me. There is no need however for the variable FullDate, you could as well use the dot
Code:
concat(substring(., 1, 4),'-',substring(., 5, 2),'-',substring(., 7, 2))
the same you do with string-length(.).

--
  Martin Honnen
  Microsoft MVP - XML
 
Old May 13th, 2008, 12:51 PM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default

Well, the <DATES> XML isn't really what source file looks like. It is actually a XML file that contains various elements that should be in the YYYYMMDD format. This template is only going apply for those elements.

I realized that I really didn't need the variable. I more did it to keep things straight in my head as I worked through the logic. The '.' doesn't read well in my mind, so I just did that to be explicit with my thoughts. Thanks.


 
Old May 13th, 2008, 04:49 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's a really bad habit to use

<xsl:variable name="x">
  <xsl:value-of select="y"/>
</xsl:variable>

when you could have written

<xsl:variable name="x" select="y"/>

which is much shorter and much faster.

Apart from that, nothing very wrong with your code, except it can be shortened. I think I would have written it as:

<xsl:template match="DATES/*">
  <xsl:value-of select="local-name()"/>
  <xsl:value-of select="if (replace(., '([0-9]{4})([0-9]{2})([0-9]{2})', '$1-$2-$3') castable as xs:date)
then ' is VALID' else ' ***IS NOT A DATE***'"/>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 14th, 2008, 07:25 AM
Authorized User
 
Join Date: Apr 2008
Posts: 70
Thanks: 17
Thanked 1 Time in 1 Post
Send a message via Yahoo to iceandrews
Default

Quote:
quote:Originally posted by mhkay
I think I would have written it as:

<xsl:template match="DATES/*">
<xsl:value-of select="local-name()"/>
<xsl:value-of select="if (replace(., '([0-9]{4})([0-9]{2})([0-9]{2})', '$1-$2-$3') castable as xs:date)
then ' is VALID' else ' ***IS NOT A DATE***'"/>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Thanks! Having a different perspective is really helping. I don't think I would've thought of using replace that way. This is a great community.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Validation aftabn10 PHP How-To 0 January 29th, 2007 10:35 AM
Date Validation zaeem Classic ASP Basics 7 October 25th, 2004 11:54 PM
string validation john_reeve41 Beginning VB 6 3 March 3rd, 2004 01:41 PM
DTS Import ( Date string to Date field) gfowajuh SQL Server 2000 1 September 30th, 2003 06:28 AM
Convert String Date to Date for a SQL Query tdaustin Classic ASP Basics 4 July 7th, 2003 06:01 PM





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