467,145 Members | 995 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,145 developers. It's quick & easy.

Finding "last node with value less than" with XPath

Hello,

I'm considering using XML to represent a stream of location
information, and XPath to do queries against it. I've got most of it
figured out (at least on paper), but I can't figure out how to create
an XPath statement asking for the "last node with a value less than" a
given value.

I need this to be able to ask "Where was Scott at 11:00 yesterday",
which should find the last sighting of Scott before or at 11:00
yesterday. For example, in this (simplified) data:

<!-- Thousands of previous sightings -->
<saw> <who> Scott </who><where> Office </where><when> 10:57 </when></saw>
<saw> <who> Tom </who><where> Office </where><when> 10:57 </when></saw>
<saw> <who> Scott </who><where> Hallway </where><when> 10:58 </when></saw>
<saw> <who> Tom </who><where> Breakroom </where><when> 10:59 </when></saw>
<saw> <who> Sally </who><where> Rooftop </where><when> 11:00 </when></saw>
<saw> <who> Scott </who><where> Dungeon </where><when> 11:01 </when></saw>
<!-- Thousands of later sightings -->

I want:

<saw> <who> Scott </who><where> Hallway </where><when> 10:58 </when></saw>

Is it possible to express this in XPath? And if so, what's the best way?

Thanks!

----ScottG.
Aug 12 '05 #1
  • viewed: 1881
Share:
7 Replies
Hi,
Tempore 22:39:07, die Friday 12 August 2005 AD, hinc in foro {comp.text.xml} scripsit Scott W Gifford <gi*****@umich.edu>:
Hello,

I'm considering using XML to represent a stream of location
information, and XPath to do queries against it. I've got most of it
figured out (at least on paper), but I can't figure out how to create
an XPath statement asking for the "last node with a value less than" a
given value.

I need this to be able to ask "Where was Scott at 11:00 yesterday",
which should find the last sighting of Scott before or at 11:00
yesterday.


Without providing any exclusion on the matter of performance, I can tell you that this would work:

<xsl:variable name="seenYesterday"
select=".//saw"/>
<xsl:variable name="ScottSighted"
select="$seenYesterday[normalize-space(who)='Scott']"/>
<xsl:value-of
select="$ScottSighted[translate(when,': ','') &lt;= 1100][last()]"/>

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
«Η αλήθεια και το λάδι πάντα βγαίνουν από πάνω»
Aug 12 '05 #2
"Joris Gillis" <ro**@pandora.be> writes:
Tempore 22:39:07, die Friday 12 August 2005 AD, hinc in foro {comp.text.xml} scripsit Scott W Gifford <gi*****@umich.edu>:
I'm considering using XML to represent a stream of location
information, and XPath to do queries against it. I've got most of it
figured out (at least on paper), but I can't figure out how to create
an XPath statement asking for the "last node with a value less than" a
given value.

[...]
Without providing any exclusion on the matter of performance, I can
tell you that this would work:

<xsl:variable name="seenYesterday"
select=".//saw"/>
<xsl:variable name="ScottSighted"
select="$seenYesterday[normalize-space(who)='Scott']"/>
<xsl:value-of
select="$ScottSighted[translate(when,': ','') &lt;= 1100][last()]"/>


Hi Joris,

Is it possible to do this with pure XPath? I'm doing queries in a
client/server environment (similar to Xindice and YFilter, which I'll
probably use as backends), and I wasn't planning on using XSLT.

If I simplify my data by changing the time format from "11:00" to
"1100", this looks right, but doesn't work with Xindice:

//saw[who=" Scott " and when <= 1100][last()]

However the below query works correctly, so it's getting the right
data, just not selecting the last node:

//saw[who=" Scott " and when <= 1100][last()]

Thanks for any advice,

----ScottG.
Aug 12 '05 #3
Tempore 23:53:00, die Friday 12 August 2005 AD, hinc in foro {comp.text.xml} scripsit Scott W Gifford <gi*****@umich.edu>:
If I simplify my data by changing the time format from "11:00" to
"1100", this looks right, but doesn't work with Xindice:

//saw[who=" Scott " and when <= 1100][last()]

However the below query works correctly, so it's getting the right
data, just not selecting the last node:

//saw[who=" Scott " and when <= 1100][last()]


I don't see any diference between these 2 queries. They both give the correct result with my XSLT processor (ALtovaXSLT).

Do remember that '//' should be replaced with more specific location steps as soon as you've made up your mind about the XML structure.

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
Aug 13 '05 #4
In article <qs*************@arkanoid.gpcc.itd.umich.edu>,
Scott W Gifford <gi*****@umich.edu> wrote:
If I simplify my data by changing the time format from "11:00" to
"1100", this looks right, but doesn't work with Xindice:

//saw[who=" Scott " and when <= 1100][last()]

However the below query works correctly, so it's getting the right
data, just not selecting the last node:

//saw[who=" Scott " and when <= 1100][last()]


Um, those two queries are identical...

But it doesn't mean "the last <saw> in the document matching the the
condition", it means "the <saw>s matching the condition that are the
last such child of their parent". For the former, you need

(//saw[who=" Scott " and when <= 1100])[last()]

(i.e. put parentheses around the art that selects the nodes).

-- Richard
Aug 14 '05 #5
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <qs*************@arkanoid.gpcc.itd.umich.edu>,
Scott W Gifford <gi*****@umich.edu> wrote:
If I simplify my data by changing the time format from "11:00" to
"1100", this looks right, but doesn't work with Xindice:

//saw[who=" Scott " and when <= 1100][last()]

However the below query works correctly, so it's getting the right
data, just not selecting the last node:

//saw[who=" Scott " and when <= 1100][last()]
Um, those two queries are identical...


Err..Yeah. Cut-n-paste error. I meant:

//saw[who=" Scott " and when <= 1100][last()]
//saw[who=" Scott " and when <= 1100][2]

in the case when there are 2 entries.
But it doesn't mean "the last <saw> in the document matching the the
condition", it means "the <saw>s matching the condition that are the
last such child of their parent". For the former, you need

(//saw[who=" Scott " and when <= 1100])[last()]

(i.e. put parentheses around the art that selects the nodes).


Thanks for the hint, but neither of these return any results for me:

(//saw[who=" Scott " and when <= 1100])[last()]
(//saw[who=" Scott " and when <= 1100])[2]

Am I running into bugs in Xindice here, or is this query still not
quite right?

Thanks!

----ScottG.
Aug 15 '05 #6
In article <qs*************@arkanoid.gpcc.itd.umich.edu>,
Scott W Gifford <gi*****@umich.edu> wrote:
Am I running into bugs in Xindice here, or is this query still not
quite right?


Try it with a different processor (e.g. embed it in a stylesheet and
try Saxon or Xalan).

-- Richard
Aug 15 '05 #7
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <qs*************@arkanoid.gpcc.itd.umich.edu>,
Scott W Gifford <gi*****@umich.edu> wrote:
Am I running into bugs in Xindice here, or is this query still not
quite right?


Try it with a different processor (e.g. embed it in a stylesheet and
try Saxon or Xalan).


Ah, thanks, it works with Saxon at least!

I'm going to end up with quite a large database, however, and making
one giant XML document with all the information, then asking Saxon to
scan this document for each query, will probably not scale well. Can
anybody recommend a good XML database that supports a reasonably large
subset of XPath and is efficient enough to handle many thousands of
small documents?

Thanks!

----ScottG.
Aug 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.