Re: Select MaxDate from xml
- From: Peter Flynn <peter.nosp@xxxxxxxxxxxxx>
- Date: Fri, 30 Jan 2009 23:55:39 +0000
Horváth Szilárd wrote:
Hi!
How can I select node from xml where the value is the max? Like "Select * From table0 where date = (select max(date) from table0)". i don't use any DB Server just xml and dataset.
(a) XML is not a database, so it doesn't behave like one. There are some
passing resemblances, but it is a canonical error to conflate the two.
See the warning box on http://xml.silmaril.ie/authors/databases/
(b) Depending on your XML processing language you may or may not have a
max() function. XSLT 1.0 notoriously did not have one, but XSLT 2.0 has
fn:max(); but there are function libraries for XSLT 1.0 which provide
them (eg http://www.datypic.com/xsl/); otherwise you need to calculate
maxima with a recursive template. I have an old one somewhere if you are
constrained to use XSLT 1.0 without libraries.
(c) In any case, it is not a good idea to store dates with alpha months,
because any comparison is going to put April at the top and September at
the bottom. You should always store dates and times in ISO 8601 format
(2008-01-30T23:43:14): you can easily convert them to something
localised for your culture on output, but if you store them wrong,
you'll make them unusable.
(d) XSLT 1.0 did not handle ordering well by default, and XML, being
designed for text documents, does not in itself have the concept of a
"date" datatype; so to make numeric comparisons on date strings you need
to use a date library, or strip the punctuation from the strings (eg
translate(date,'-T:','')) to leave only digits. XSLT 2.0 has an
extensive and excellent selection of date functions to overcome this.
///Peter
<table0>.
<row0>
<id>0</id>
<text>text0</text>
<date>2009-jan-30</date>
</row0>
<row1>
<id>1</id>
<text>text1</text>
<date>2009-jan-29</date>
</row1>
<row2>
<id>2</id>
<text>text2</text>
<date>2009-jan-31</date>
</row2>
</table0>
- Follow-Ups:
- Re: Select MaxDate from xml
- From: Horváth Szilárd
- Re: Select MaxDate from xml
- References:
- Select MaxDate from xml
- From: Horváth Szilárd
- Select MaxDate from xml
- Prev by Date: Select MaxDate from xml
- Next by Date: Re: Select MaxDate from xml
- Previous by thread: Select MaxDate from xml
- Next by thread: Re: Select MaxDate from xml
- Index(es):
Relevant Pages
|