Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Transform Dublin Core date using XSL

    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!
    As easy as 3.1415926535897932384626433832795028841

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    As easy as 3.1415926535897932384626433832795028841

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    As easy as 3.1415926535897932384626433832795028841

  • #6
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    As easy as 3.1415926535897932384626433832795028841

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Location
    Newcastle, England
    Posts
    178
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>
    Last edited by mpjbrennan; 09-27-2002 at 07:57 PM.

  • #8
    New Coder
    Join Date
    Jun 2002
    Location
    London & Oxford
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    As easy as 3.1415926535897932384626433832795028841


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •