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 July 11th, 2006, 07:12 AM
Registered User
 
Join Date: Jul 2006
Location: Dumfries, , United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML Date Formats

<center></center>Hi,

I'm new to XML and XSL transformations. I have output from one system that has the dates as DD/MM/YYYY and they did need to be transformed so that the import to another system is in this format DD/MM/YY. Can someone please help in what code I should use as well as where to put it? Thank you so much. Kind Regards, Tracy
Reply With Quote
  #2 (permalink)  
Old July 11th, 2006, 07:44 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try:

<xsl:template match="date-element">
  <xsl:copy>
    <xsl:value-of select="concat(substring(., 1, 6), substring(., 9, 2))"/>
  </xsl:copy>
</xsl:template>

If this is the only change to be made to the input document, write a stylesheet that contains the above rule (with the element name changed, of course) plus a default rule that copies other elements unchanged:

<xsl:template match="*">
  <xsl:copy>
  <xsl:copy-of select="@"*/>
  <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old July 11th, 2006, 07:54 AM
Registered User
 
Join Date: Jul 2006
Location: Dumfries, , United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael, Thank you so much for your reply. Here's part of the XSL file and one of date fields.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <Batch>
  <ResultsSummary>
    <ExtractCount>5</ExtractCount>
    <ErrorCount>0</ErrorCount>
      </ResultsSummary>
       <ExtractedFrom>
    <BusinessContext>Telco</BusinessContext>
    <RootDataFolder>Marketing Automation\Business Contexts\Telco\Data</RootDataFolder>
  </ExtractedFrom>
    <LogicalUnit>
        <CampaignDO>
            <Name><xsl:value-of select="/AprimoXMLPublishing/DataObjects/Object/ResultData/name" /></Name>
            <Code><xsl:value-of select="/AprimoXMLPublishing/DataObjects/Object/ResultData/campaign_code/value" /></Code>
            <Description><xsl:value-of select="/AprimoXMLPublishing/DataObjects/Object/ResultData/description" /></Description>
            <DateModified><xsl:value-of select="/AprimoXMLPublishing/DataObjects/Object/ResultData/modified_date" /></DateModified>
            <DateCreated><xsl:value-of select="/AprimoXMLPublishing/DataObjects/Object/ResultData/modified_date" /></DateCreated>
            <Owner>SAS Demo User</Owner>
            <ModifiedUser>SAS Demo User</ModifiedUser>
            <Shared>true</Shared>
            <HasDiagram>false</HasDiagram>
            <HasCells>false</HasCells>
            <HasAudience>false</HasAudience>
            <HasCommunications>false</HasCommunications>
            <HasCellRefs>false</HasCellRefs>
            <Saved>true</Saved>

Are you saying I change date-element to be modified_date but place in another file? Or can I just use the sxl:value command to change it?

Kind Regards,

Tracy

Reply With Quote
  #4 (permalink)  
Old July 11th, 2006, 08:07 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You asked where the code should go and I had no idea of the context so I guessed you were doing a transformation in which this was the only change being made. I guessed wrong.

Just put the xsl:value-of expression in the place where you are constructing the modified date, changing "." to an expression that fetches the original date to be modified.

Since many of the fields of CampaignDO come from one element deep in the source document, I would do an apply-templates to that element and put this logic in a separate template rule.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #5 (permalink)  
Old July 12th, 2006, 08:19 AM
Registered User
 
Join Date: Jul 2006
Location: Dumfries, , United Kingdom.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Michael.
Final conumdrum I have.

DD/MM/YYYY 12:00:00 AM needs to be converted to DD/MM/YY 12:00 AM

Because some of the dates are 7/12/2006 and then 10/12/2006 the position may change. Is there a way for it to take i.e. all chars before / or all chars after / or a xsl function that will search for / and return it's location. Thanks, Tracy

Kind Regards,
Tracy
Reply With Quote
  #6 (permalink)  
Old July 12th, 2006, 10:17 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>DD/MM/YYYY 12:00:00 AM needs to be converted to DD/MM/YY 12:00 AM

This one can be handled using exactly the same kind of concat/substring logic as the previous problem

>Because some of the dates are 7/12/2006 and then 10/12/2006 the position may change.

This makes it more difficult. In XSLT 2.0 you can use regular expressions to analyse the strings. In 1.0, you're confined to a more limited set of functions such as substring-before and substring-after. It's tedious, for example to extract the second component you have to do

substring-before(substring-after(., '/'), '/')

but it's still possible. You can use format-number() to force a number to exactly two digits.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
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 Formats carumuga SQL Server 2000 4 January 2nd, 2007 07:20 PM
Trouble with date formats DrewMills Access VBA 3 March 7th, 2005 06:22 PM
displaying different date formats using LCID leo_vinay Classic ASP Basics 1 November 26th, 2004 07:17 PM
SQL and Date Formats al_bllinky Access 3 August 28th, 2003 08:45 AM
Date formats in the forum Crispin Horsfield Forum and Wrox.com Feedback 1 August 3rd, 2003 08:14 PM



All times are GMT -4. The time now is 03:52 PM.


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