473,498 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
7 2139
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
27835
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
32
4093
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
2
2544
by: Bilal | last post by:
Hello, I'm stuck on this problem for quite some time and hope somebody would be able to guide me. Basically, I need to populate a large number of "template" XML files which have all...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.