View Full Version : Transform Dublin Core date using XSL
head8k
09-24-2002, 02:15 PM
I have an XML document which uses Dublin Core dates in this format:
<dc:date>2002-09-24</dc:date>
How can I parse this into something a bit more meaningful like "September 24 2002"? I am trying to use XSL's formatDate method but it gives nonsensical results. Is there a better way? I would rather not use substrings if possible as I think it would be better to treat it as a date rather than a string.
Some code would be great! :)
mpjbrennan
09-24-2002, 06:22 PM
XSL does not have a date datatype, so you will have to resort to string handling, for example:
<xsl:template match="date">
<xsl:value-of select="concat(substring(., 9, 2), '-', substring(., 6, 2), '-', substring(., 1, 4))" />
</xsl:template>
will convert YYYY-MM-DD into DD-MM-YYYY
patrick
head8k
09-24-2002, 09:57 PM
Thanks for the reply Patrick; I will try it when I get back to the office on Thursday.
I read somewhere that the formatDate method that is part of XSL takes a variable of type date (as oppose to a string) which contradicts what you are saying - or am I getting 2 different things confused?
The way I was trying it was by taking my string and converting it to be a date by using date.parse. I'm not sure that this is a valid way of doing it since I'm still a beginner with XSL and find it very hard going.
Any clarification or pointers would be greatfully recieved.
mpjbrennan
09-25-2002, 08:45 AM
My reference to XSL data types is Michael Kay's "XSLT - 2nd Edition". According to him there are only five data types defined in XSLT and XPath:
String
Number
Boolean
Node-set
External object
The formatDate method you refer to is a Microsoft extension to the W3C XML DOM. To access it you have to load the XML fle as an ActiveX object.
regards
Patrick
head8k
09-25-2002, 10:32 AM
Ah, I see now. Thanks for clearing that up.
I'll be picking this project up again tomorrow so may well post back for more help.
Thanks again.
head8k
09-27-2002, 03:33 PM
This has got me tearing my hair out now! I still can't get it to manipulate the date in any way. I can't get the substring method to work at all, it always gives errors.
I am processing the XML found at http://www.nelh.nhs.uk/hth.rdf by transforming it (server side with ASP) using this XSL:
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/TR/WD-xsl"
xmlns="http://www.w3.org/TR/REC-html40"
>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="rdf:RDF">
<xsl:for-each select="item">
<p><xsl:value-of select="description"/><br /><xsl:apply-templates select="dc:date"/></p>
</xsl:for-each>
</xsl:template>
<xsl:template match="dc:date">
<xsl:value-of select="." />
</xsl:template>
</xsl:stylesheet>
The above code is working fine but I want to change the format of the date. Can anyone help?
mpjbrennan
09-27-2002, 08:53 PM
This is a tricky one!
The following stylesheet will display the data you require and transform the dates into dd-mm-yyyy- but only in IE6. Note that the xmlns:xsl namespace is the authorised version rather than the transitional working draft (TR/WD-xsl) one you used. Note also that references to the rdf and dc namespaces are also included in the stylesheet element.
If you don't have IE6 then you are stuck because the string concat method does not exist in the WD version.
Finally - this will only work if you strip out the following namespace from the <rdf:RDF> element in the XML file:
xmlns="http://purl.org/rss/1.0/"
I presume that this is because IE6 doesn't recognise this as an XML namespace.
Hope this helps!
patrick
--------------------------
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/REC-html40" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="rdf:RDF/item/description" />
</body>
</html>
</xsl:template>
<xsl:template match="description">
<p><xsl:value-of select="." /><br />
<xsl:call-template name="dateformat" /></p>
</xsl:template>
<xsl:template name="dateformat">
<xsl:value-of select="concat(substring(../dc:date, 9, 2), '-', substring(../dc:date, 6, 2), '-', substring(../dc:date, 1, 4))" />
</xsl:template>
</xsl:stylesheet>
head8k
09-28-2002, 12:48 PM
Thanks Patrick, that gives me some food for thought. I will have a proper look at your code on Monday when I'm back in the office. First impression is that I am going to have to rethink this as it needs to be cross browser. Why are these things never easy?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.