Generate date into CDATA tag

37 views
Skip to first unread message

Mags

unread,
Jul 7, 2005, 9:55:04 AM7/7/05
to XS...@googlegroups.com
Hi everyone, I have a question regarding how to use xslt to format a
date so it gets into a CDATA tag in the resulting xml.

I have a date in my xml source document in the following format:
2005-07-06T10:32:21.1970000-04:00

In my XSLT I want to convert it to look like this:
<![CDATA[2004-06-14T01:00:00]]>

The xml source document looks like this:

<SomeDate>2005-07-06T10:32:21.1970000-04:00</SomeDate>

My xslt looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<MyMessage>
<Body>
<ActualizedDate>
<xsl:call-template name="standard_date">
<xsl:with-param name="date" select="/SomeDate"/>
</xsl:call-template>
</ActualizedDate>
</Body>
</MyMessage>
</xsl:template>


<xsl:template name="standard_date">
<xsl:param name="date" />
<xsl:variable name="date_length" select="string-length($date)" />
<xsl:if test="number($date_length) > 0">
![CDATA[
<!-- Year -->
<xsl:value-of select="substring($date, 1, 4)" />
<xsl:text>-</xsl:text>
<!-- Month -->
<xsl:value-of select="substring($date, 6, 2)" />
<xsl:text>-</xsl:text>
<!-- Day -->
<xsl:value-of select="substring($date, 9, 2)" />
T01:00:00
]] <xsl:text>></xsl:text>>

</xsl:if>
</xsl:template>

</xsl:stylesheet>


The issue is that when I add a the the tags before the Cdata tag and
after, the xslt will not generate because it doesn't think it's valid
xml.

If I change my function from this:
![CDATA[

To
<![CDATA[

And the end from:
]] <xsl:text>></xsl:text>>
To
]] <xsl:text>></xsl:text>> >

results in the problem. Is there a way around this so I can generate it
into the tag so it looks like this: <![CDATA[2004-06-14T01:00:00]]>
?

TIA,
Magnus

Joris Gillis

unread,
Jul 7, 2005, 10:33:15 AM7/7/05
to XS...@googlegroups.com
Hi,

Tempore 15:55:04, die 07/07/2005 AD, hinc in XS...@googlegroups.com
scripsit Mags <magnus...@gmail.com>:

> Hi everyone, I have a question regarding how to use xslt to format a
> date so it gets into a CDATA tag in the resulting xml.
>
> I have a date in my xml source document in the following format:
> 2005-07-06T10:32:21.1970000-04:00
>
> In my XSLT I want to convert it to look like this:
> <![CDATA[2004-06-14T01:00:00]]>

You cannot explicitely create CDATA nodes in xslt.
use the 'cdata-section-elements' attribute:

<xsl:output method="xml" encoding="UTF-8" indent="yes"
cdata-section-elements="ActualizedDate"/>

P.S. Why should you one wish to use CDATA sections for a date?

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Spread the wiki (http://www.wikipedia.org)

Mags

unread,
Jul 7, 2005, 12:36:47 PM7/7/05
to XS...@googlegroups.com
Thanks, that worked great!

The reason why I need to put the dates in a CDATA tag is that we're
rewriting our existing systems integration and we have a number of
legacy systems that actually expects to get the dates inside CDATA
tags, and we don't have time to modify those systems at the same time.

Thanks,
Magnus

Reply all
Reply to author
Forward
0 new messages