Finding "last node with value less than" with XPath

S

Scott W Gifford

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.
 
J

Joris Gillis

Hi,
Tempore 22:39:07 said:
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,
 
S

Scott W Gifford

Joris Gillis said:
Tempore 22:39:07 said:
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.
 
J

Joris Gillis

Tempore 23:53:00 said:
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,
 
R

Richard Tobin

Scott W Gifford said:
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
 
S

Scott W Gifford

Scott W Gifford said:
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.
 
R

Richard Tobin

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
 
S

Scott W Gifford

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,251
Members
46,850
Latest member
Brightrs

Latest Threads

Top