Jump to content

Show "new" according to date


kwindo

Recommended Posts

In my development attempt I stumbled on another problem. I've managed to solve about 2000 other problems already but this one is a bit tricky.Still I'm working on transforming an xml feed from an estates webservice to xhtml to show the data on a website.I'm trying to show something like "New" on the items that are recently posted.The date format in the xml is like this : 2010-10-21T08:48:31.073I've managed to format this to a european format (21.10.2010 08:48:31)  like this

	<xsl:template name="FormatDate">		<xsl:param name="DateTime" />		<xsl:variable name="year"> <xsl:value-of select="substring($DateTime,1,4)" /> </xsl:variable>		<xsl:variable name="month-temp"> <xsl:value-of select="substring-after($DateTime,'-')" /> </xsl:variable>		<xsl:variable name="month"> <xsl:value-of select="substring-before($month-temp,'-')" /> </xsl:variable>		<xsl:variable name="day-temp"> <xsl:value-of select="substring-after($month-temp,'-')" /> </xsl:variable>		<xsl:variable name="day"> <xsl:value-of select="substring($day-temp,1,2)" /> </xsl:variable>		<xsl:variable name="time"> <xsl:value-of select="substring-after($DateTime,'T')" /> </xsl:variable>		<xsl:variable name="hh"> <xsl:value-of select="substring($time,1,2)" /> </xsl:variable>		<xsl:variable name="mm"> <xsl:value-of select="substring($time,4,2)" /> </xsl:variable>		<xsl:variable name="ss"> <xsl:value-of select="substring($time,7,2)" /> </xsl:variable>		<xsl:value-of select="$day"/> <xsl:value-of select="'.'"/>		<!--18.-->		<xsl:value-of select="$month"/> <xsl:value-of select="'.'"/>		<!--18.03.-->		<xsl:value-of select="$year"/> <xsl:value-of select="' '"/>		<!--18.03.1976 -->		 <xsl:value-of select="$hh"/> <xsl:value-of select="':'"/>		<!--18.03.1976 13: -->		 <xsl:value-of select="$mm"/> <xsl:value-of select="':'"/> 		<!--18.03.1976 13:24 -->		<xsl:value-of select="$ss"/> 		<!--18.03.1976 13:24:55 -->	</xsl:template>	<xsl:call-template name="FormatDate">		<xsl:with-param name="DateTime" select="kwd:CreateDateTime"/>	</xsl:call-template>

Is there a way to show an html item when an estate is created less then a week (or month) ago?

Link to comment
Share on other sites

If you move to XSLT 2.0 then you can use functions like format-date http://www.w3.org/TR/xslt20/#function-format-date to format such dates and you can use the function current-date http://www.w3.org/TR/xpath-functions/#func-current-date to get the current date and compare it to other dates. XSLT 2.0 is implemented by software like Saxon 9 (http://saxon.sourceforge.net/), AltovaXML Tools (http://www.altova.com/altovaxml.html) und XQSharp (http://www.xqsharp.com/).If you want to use XSLT 1.0 then you need to look into processor specific extension functions or pass in the current date as parameter; how you do that depends also on the API of the specific XSLT processor you use.

Link to comment
Share on other sites

I've read that some stuff is easyer done in XSLT 2.0 but I started out like this and have been puzzeling for some weeks now. I don't really feel like starting over since I'm allmost finished.I have these lines on top of my xsl file:<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:kwd="something, somethingelse">I'm assuming this is not stating the XSLT version : <?xml version="1.0" encoding="ISO-8859-1"?> When I change it to <?xml version="2.0" encoding="ISO-8859-1"?> everything still seems to work.However. Are these two lines the info you where referring to?

Link to comment
Share on other sites

The first line determines the XML version, and the second one - the XSLT version. You can change to XSLT 2.0 by replacing

<xsl:stylesheet version="1.0"

with

<xsl:stylesheet version="2.0"

However, you MUST use an XSLT 2.0 processor to get the XSLT 2.0 features, like the date functions. Martin gave you links to XSLT 2.0 processors above. Depending on the language you're coding your site with, you'll need to use one of them. If you use ASP.NET, any one of them will do. If you're using PHP on a Linux machine, using the Java version of SAXON is basically the only way. You can use a wrapper like my own from my signature to make it easier.

Link to comment
Share on other sites

Hehe, seems so obvious.Just for fun I changed the version nuber to "2" and everything still seems to work.I am using php. Apart from that I'm affraid you have lost me.This SAXON, is it something that should be installed on my server?Sorry for the really noob questions.I don't even know what you mean with the "wrapper" in your signature. :)

Link to comment
Share on other sites

If you use PHP (and server-side XSLT?) then you are probably using libxslt, the XSLT 1.0 processor that PHP 5 normally uses. As I said, as long as you use an XSLT 1.0 processor your options are to pass in the current date as a parameter or to use an extension function to access the current date. According to http://www.exslt.org/date/functions/date/index.html libxslt supports the EXSLT function date:date() to access the current date. You will need to check whether that works with your PHP configuration. Using http://www.exslt.org/date/functions/add/index.html you could then substract a duration of one month to get a previous date and then compare that with existing dates in your input.

Link to comment
Share on other sites

  • 2 weeks later...

It took some time but I figured it out. Since I started this thread I feel I have to share my findings.First off all this is my "stylesheet" tag:

<xsl:stylesheet version="1.0"	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"	xmlns:kwd="Something.Somethingelse"	xmlns:date="http://exslt.org/dates-and-times"	extension-element-prefixes="date">

This part: "xmlns:kwd="Something.Somethingelse" I needed to at because there is a namespace without a prefix in the XML that I'm trying to transform to XHTML. The prefix "kwd" is one I made up. Sorry I don't really know the background about this but I doesn't really do anything to my "date" issue. I'm just mentioning this for other newbees (like me) who might wonder why some of my values have the "kwd" prefix.These parts appear to be important: xmlns:date="http://exslt.org/dates-and-times" and extension-element-prefixes="date".First I needed to get the current date and make that a variable. This line did the trick:

<xsl:variable name="currenttdate" select="date:date()" />

Then (thanks to Martin pointing in the right website) I found a function than can calculate the difference between the current date and the date given in the xml feed.This function is here : http://www.exslt.org/date/functions/difference/index.htmlI used the "XSLT Template (by Jeni Tennison)" and following code to turn the result into a variable

<xsl:variable name="difference">	<xsl:call-template name="date:difference">		<xsl:with-param name="start" select="kwd:CreateDateTime" />		<xsl:with-param name="end" select="$currenttdate" />	</xsl:call-template></xsl:variable>

This gave me a result in a format like "P7D". Meaning there are 7 days between the date in the xml and the current date.I removed the "P" and"D" from the result with "translate" and (again) made a variable from the result which is now only a number. I did that like this:

<xsl:variable name="differencenum" select="translate($difference,'P#D','')" />

The rest was a walk in the park. If the number is smaller then 7 show "NEW".

<xsl:if test="$differencenum < 7">	<xsl:text>NEW</xsl:text></xsl:if>

Link to comment
Share on other sites

Since you're using PHP, you can remove both templates, and just use the functions. They are "built in" with libxslt (again - the XSLT processor of PHP).i.e. you can use directly

<xsl:variable name="differencenum" select="translate(date:difference(kwd:CreateDateTime, date:date()), 'P#D', '')" />

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...