By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,826 Members | 1,133 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,826 IT Pros & Developers. It's quick & easy.

Changing content of current(?) node

P: n/a
Hi,

My problem is this: I have an XML file I'm transforming to XHTML by
using XSLT, and the XML file contains some values as Unix timestamps
(integer value of seconds since 00:00 1970-01-01) and the only way I can
think of displaying those values as human-readable is by using Javascript.

Converting the times isn't the problem, what I'm stuck with is actually
writing the new value into the document, since document.write(newValue);
just replaces the entire document with the value of newValue.

As far as I'm aware I can't use getElementById because there will be
multiple elements with the same ID - I can't set them individually (can I?)

I'm not great at explaining things so I'll show an example below of what
I have and what I need:

I have (after the XSLT process)
<td>1168096647</td>

I need
<td>Sat, 06 Jan 2007 15:17:27 GMT</tddisplayed in the browser
(obviously inside a table cell, not surrounded by td tags ;)

Thanks for any help anyone can give me.
</newbie>

--
Steven Davies
Feb 16 '07 #1
Share this Question
Share on Google+
4 Replies

P: n/a
On Feb 16, 11:08 am, Steven Davies <sed3.remove...@pants.aber.ac.uk>
wrote:
Hi,

My problem is this: I have an XML file I'm transforming to XHTML by
using XSLT, and the XML file contains some values as Unix timestamps
On the server or at the client?

(integer value of seconds since 00:00 1970-01-01) and the only way I can
think of displaying those values as human-readable is by using Javascript.

Converting the times isn't the problem, what I'm stuck with is actually
writing the new value into the document, since document.write(newValue);
just replaces the entire document with the value of newValue.

As far as I'm aware I can't use getElementById because there will be
multiple elements with the same ID - I can't set them individually (can I?)
Then you are generating invalid XHTML, and getElementById will fail
(and maybe fail differently in different browsers). How you set them
individually is up to you - in the source XML would be best, maybe the
transform can do it (I'm a total n00b there).

>
I'm not great at explaining things so I'll show an example below of what
I have and what I need:

I have (after the XSLT process)
<td>1168096647</td>

I need
<td>Sat, 06 Jan 2007 15:17:27 GMT</tddisplayed in the browser
(obviously inside a table cell, not surrounded by td tags ;)
Once you identify the table cell (however you do that...) you can do
something like:

var el = /* some method to get the td element */
el.innerHTML = convertUNIXtimeToTimestamp(el.innerHTML);
However, replacing innerHTML in some browsers causes a flicker, so
better to use DOM methods where you can. If you give the td an ID,
e.g.:

<td id="ts_0">1168096647</span></td>
You can try something like:

var el = document.getElementById('ts_0');
var timeString = el.firstChild.data;
el.firstChild.data = convertUNIXtimeToTimestamp(timeString);
I've omitted feature detection for convenience, but I'd also assume
that any browser supporting XHTML will also support the DOM features
employed. :-)
--
Rob

Feb 16 '07 #2

P: n/a
On 16 Feb, 03:14, "RobG" <r...@iinet.net.auwrote:
On Feb 16, 11:08 am, Steven Davies <sed3.remove...@pants.aber.ac.uk>
wrote:
Hi,
My problem is this: I have an XML file I'm transforming to XHTML by
using XSLT, and the XML file contains some values as Unix timestamps
assuming server, adapt this, depending on the dates, you might like to
use a parameter as a global value for say the year and month. up to
you. Also check out
format-number(123456.789,'# ##0.00',English_Money) to store formats
for later use
<xsl:template match="Time">
<xsl:variable name="dateTime" select="normalize-space()" />
<xsl:variable name="year" select="substring($dateTime, 1, 4)" />
<xsl:variable name="month" select="number(substring($dateTime, 6,
2))" />
<xsl:variable name="day" select="number(substring($dateTime, 9,
2))" />
<xsl:variable name="time" select="substring($dateTime, 12, 5)" />
<xsl:variable name="hours" select="substring($time, 1, 2)" />
<xsl:variable name="minutes" select="substring($time, 4, 2)" />
<span class="timestamp">
<xsl:value-of select="concat($month, '/', $day, '/', $year, ' ',
$time, ' - ', format-number($hours, '00'), ':', format-
number($minutes,'00'))" />
</span>
</xsl:template>
Feb 16 '07 #3

P: n/a
RobG wrote:
On Feb 16, 11:08 am, Steven Davies <sed3.remove...@pants.aber.ac.uk>
wrote:
>Hi,

My problem is this: I have an XML file I'm transforming to XHTML by
using XSLT, and the XML file contains some values as Unix timestamps

On the server or at the client?

>(integer value of seconds since 00:00 1970-01-01) and the only way I can
think of displaying those values as human-readable is by using Javascript.

Converting the times isn't the problem, what I'm stuck with is actually
writing the new value into the document, since document.write(newValue);
just replaces the entire document with the value of newValue.

As far as I'm aware I can't use getElementById because there will be
multiple elements with the same ID - I can't set them individually (can I?)

Then you are generating invalid XHTML, and getElementById will fail
(and maybe fail differently in different browsers). How you set them
individually is up to you - in the source XML would be best, maybe the
transform can do it (I'm a total n00b there).

>I'm not great at explaining things so I'll show an example below of what
I have and what I need:

I have (after the XSLT process)
<td>1168096647</td>

I need
<td>Sat, 06 Jan 2007 15:17:27 GMT</tddisplayed in the browser
(obviously inside a table cell, not surrounded by td tags ;)

Once you identify the table cell (however you do that...) you can do
something like:

var el = /* some method to get the td element */
el.innerHTML = convertUNIXtimeToTimestamp(el.innerHTML);
However, replacing innerHTML in some browsers causes a flicker, so
better to use DOM methods where you can. If you give the td an ID,
e.g.:

<td id="ts_0">1168096647</span></td>
You can try something like:

var el = document.getElementById('ts_0');
var timeString = el.firstChild.data;
el.firstChild.data = convertUNIXtimeToTimestamp(timeString);
I've omitted feature detection for convenience, but I'd also assume
that any browser supporting XHTML will also support the DOM features
employed. :-)
Thanks, after discovering the XSLT generate-id() function I managed to
use a slightly modified version of your code (I first create a new text
node then append it to the TD element) which works in Firefox - next
task is to test it in IE ;-)

Thanks for your help :-)

--
Steven Davies
Feb 16 '07 #4

P: n/a
In comp.lang.javascript message <11***************@leri.aber.ac.uk>,
Fri, 16 Feb 2007 01:08:54, Steven Davies <se************@pants.aber.ac.u
kposted:
>My problem is this: I have an XML file I'm transforming to XHTML by
using XSLT, and the XML file contains some values as Unix timestamps
(integer value of seconds since 00:00 1970-01-01)
Be aware that the origin is 1970-01-01 00:00:00 UTC (or GMT) (in
Wales/Scotland that was one in the morning!).

>I have (after the XSLT process)
<td>1168096647</td>

I need
<td>Sat, 06 Jan 2007 15:17:27 GMT</tddisplayed in the browser
var D = new Date(1168096647 * 1000)
String(D) gives me Sat Jan 6 15:17:27 UTC 2007
d.toGMTString() gives me Sat, 6 Jan 2007 15:17:27 UTC

If you want exactly that for sure in any browser you'll need to build it
yourself from the component fields, since date-to-string conversion
results are not defined in ISO 16262.

But

new Date(1168096647 * 1000).toGMTString().
replace("UTC", "GMT").replace(/ (\d) /, " 0$1 ")

will usually give what you want. See sig line 3.

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 16 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.