Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 13th, 2008, 12:11 PM
Authorized User
Points: 340, Level: 6
Points: 340, Level: 6 Points: 340, Level: 6 Points: 340, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , OH, USA.
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>
Reply With Quote
  #2 (permalink)  
Old May 13th, 2008, 12:21 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
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
Reply With Quote
  #3 (permalink)  
Old May 13th, 2008, 12:51 PM
Authorized User
Points: 340, Level: 6
Points: 340, Level: 6 Points: 340, Level: 6 Points: 340, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , OH, USA.
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.


Reply With Quote
  #4 (permalink)  
Old May 13th, 2008, 04:49 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #5 (permalink)  
Old May 14th, 2008, 07:25 AM
Authorized User
Points: 340, Level: 6
Points: 340, Level: 6 Points: 340, Level: 6 Points: 340, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: , OH, USA.
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:56 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.