 |
| 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
|
|
|
|

February 18th, 2010, 12:58 PM
|
|
Registered User
|
|
Join Date: Apr 2009
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Change ALL DateTime Child Elements
I would like to create a single XSLT to globally change all the DateTime child elements within our incoming XML documents. Our implementations with our business partners guarantee that all the child elements in these documents will have the same name: DateTime. AlI I'd like to do with this is find a way NOT to have to specify the root and parent element names within the template match. Just change all the date fields. Is this even possible?
I have a stylesheet that works provided I perform a template match based on the root/parent elements:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0" exclude-result-prefixes="#all">
<xsl:output indent="yes"/>
<xsl:template match="sales/summary">
<xsl:apply-templates>
<xsl:with-param name="TempDate" select="DateTime/text()"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="DateTime">
<xsl:param name="TempDate"/>
<xsl:variable name="est-timestamp" select="adjust-dateTime-to-timezone($TempDate, xs:dayTimeDuration('-PT5H'))"/>
<date timestamp="{$TempDate}">
<xsl:value-of select="format-dateTime($TempDate, '[D] [MN,*-3] [Y] [h]:[m01][PN,*-2] [ZN,*-3]', (), (), 'us')"/>
<est str="{$est-timestamp}">
<xsl:value-of select="format-dateTime($est-timestamp, '[D] [MN,*-3] [Y] [h]:[m01][PN,*-2] [ZN,*-3]', (), (), 'us')"/>
</est>
</date>
</xsl:template>
</xsl:stylesheet>
The data looks like this:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="XSLT-DateTime.xsl"?>
<sales>
<summary>
<DateTime DateTimeQualifier="ON">2009-12-31T00:00:59-06:00</DateTime>
</summary>
<summary>
<DateTime DateTimeQualifier="ON">2009-12-31T18:00:59-06:00</DateTime>
</summary>
<summary>
<DateTime DateTimeQualifier="ON">2009-12-31T00:10:59-06:00</DateTime>
</summary>
</sales>
Current result appears as follows:
31 DEC 2009 12:00am -06:00 31 DEC 2009 1:00am -05:00
31 DEC 2009 6:00pm -06:00 31 DEC 2009 7:00pm -05:00
31 DEC 2009 12:10am -06:00 31 DEC 2009 1:10am -05:00
Thanks Folks!
|
|

February 18th, 2010, 01:09 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
If you know you want to transform all elements named 'DateTime' then simply write two templates, one with the identity transformation e.g.
Code:
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*, node()"/>
</xsl:copy>
</xsl:template>
and one for your 'DateTime' elements
Code:
<xsl:template match="DateTime">
<xsl:variable name="est-timestamp" select="adjust-dateTime-to-timezone(., xs:dayTimeDuration('-PT5H'))"/>
<date timestamp="{.}">
<xsl:value-of select="format-dateTime(., '[D] [MN,*-3] [Y] [h]:[m01][PN,*-2] [ZN,*-3]', (), (), 'us')"/>
<est str="{$est-timestamp}">
<xsl:value-of select="format-dateTime($est-timestamp, '[D] [MN,*-3] [Y] [h]:[m01][PN,*-2] [ZN,*-3]', (), (), 'us')"/>
</est>
</date>
</xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|

February 18th, 2010, 01:28 PM
|
|
Registered User
|
|
Join Date: Apr 2009
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Change ALL DateTime Child Elements
Hi Martin -
Thanks a bunch! I KNEW there had to be a way to do this without tying the date conversion to the root and parent elements (which I know will be different). Appreciate your help - I'll give that a go!
Bob
|
|
 |