Ok so here is my situation. I am generating a wordpress (blog) import file from an xml file using xslt. I am trying to add an hour to each publish date node. So for example....

<title> Post 1 </title>
<pubdate>8-10-2008 5:00</pubdate> ( I realize the format may not be right)

<title> Post 2</title>
<pubdate>8-10-2009 6:00</pubdate>

You get the idea. This way I can import thousands of post and wordpress will automatically post them when their time comes up in the future. I have tried using the exslt date:add string with no luck.

Any ideas on how I can do this?

Member Avatar

You can use the substring() functions as follows:

<?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" indent="yes"/>

    <xsl:template match="/">

	<xsl:template match="title">
		<xsl:copy-of select="."/>
	<xsl:template match="pubdate">
			<xsl:call-template name="increment"/>
	<xsl:template name="increment">
		<xsl:variable name="date" select="substring-before(.,' ')"/>
		<xsl:variable name="hourStart" select="substring-after(.,' ')"/>
		<xsl:variable name="hour" select="number(substring-before($hourStart,':'))+1"/>
		<xsl:variable name="minutes" select="substring-after($hourStart,':')"/>
		<xsl:value-of select="$date"/> <xsl:value-of select="$hour"/>:<xsl:value-of select="$minutes"/>


Note that this fragment doesn't address time properly -- you would need to add logic to handle 12/24 hour clocks and increment the date if necessary, but this should get you started.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.