Finding "last node with value less than" with XPath

Discussion in 'XML' started by Scott W Gifford, Aug 12, 2005.

  1. 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.
     
    Scott W Gifford, Aug 12, 2005
    #1
    1. Advertising

  2. Scott W Gifford

    Joris Gillis Guest

    Hi,
    Tempore 22:39:07, die Friday 12 August 2005 AD, hinc in foro {comp.text.xml} scripsit 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.


    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)
    «Η αλήθεια και το λάδι πάντα βγαίνουν από πάνω»
     
    Joris Gillis, Aug 12, 2005
    #2
    1. Advertising

  3. "Joris Gillis" <> writes:

    > Tempore 22:39:07, die Friday 12 August 2005 AD, hinc in foro {comp.text.xml} scripsit Scott W Gifford <>:
    >
    >> 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.
     
    Scott W Gifford, Aug 12, 2005
    #3
  4. Scott W Gifford

    Joris Gillis Guest

    Tempore 23:53:00, die Friday 12 August 2005 AD, hinc in foro {comp.text.xml} scripsit Scott W Gifford <>:

    > 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)
     
    Joris Gillis, Aug 13, 2005
    #4
  5. In article <>,
    Scott W Gifford <> 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
     
    Richard Tobin, Aug 14, 2005
    #5
  6. (Richard Tobin) writes:

    > In article <>,
    > Scott W Gifford <> 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.
     
    Scott W Gifford, Aug 15, 2005
    #6
  7. In article <>,
    Scott W Gifford <> 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
     
    Richard Tobin, Aug 15, 2005
    #7
  8. (Richard Tobin) writes:

    > In article <>,
    > Scott W Gifford <> 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.
     
    Scott W Gifford, Aug 15, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,028
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,523
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    572
  4. goog
    Replies:
    0
    Views:
    536
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,704
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page