473,382 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

last()

I don't think I understand the last() function.

I have a document that looks like:

<Root>
<Header>Some text</Header>
<Message>
<MessageID>1</MessageID>
. . . .
</Message>
<Message>
. . . . .
</Message>
</Root>

I have an expression
node = doc.SelectSingleNode("//Message/MessageID[last()]")
and
node = doc.SelectSingleNode("//Message[last()]")

Neither of these seem to return the last node. From the debugger it seems
that the last node is not being retrieved. Am I using XPath correctly?

Thank you.

Kevin
Oct 9 '06 #1
2 3247
This is a FAQ and frequent misunderstanding of the "//" abbreviation.

Use:

(//Message/MessageID) [last()]
Cheers,
Dimitre Novatchev

"Kevin Burton" <Ke*********@discussions.microsoft.comwrote in message
news:D5**********************************@microsof t.com...
>I don't think I understand the last() function.

I have a document that looks like:

<Root>
<Header>Some text</Header>
<Message>
<MessageID>1</MessageID>
. . . .
</Message>
<Message>
. . . . .
</Message>
</Root>

I have an expression
node = doc.SelectSingleNode("//Message/MessageID[last()]")
and
node = doc.SelectSingleNode("//Message[last()]")

Neither of these seem to return the last node. From the debugger it seems
that the last node is not being retrieved. Am I using XPath correctly?

Thank you.

Kevin

Oct 10 '06 #2
Kevin Burton wrote:
I don't think I understand the last() function.
It needs to be used in conjunction with something like position()
I have a document that looks like:

<Root>
<Header>Some text</Header>
<Message>
<MessageID>1</MessageID>
. . . .
</Message>
<Message>
. . . . .
</Message>
</Root>

I have an expression
node = doc.SelectSingleNode("//Message/MessageID[last()]")
and
node = doc.SelectSingleNode("//Message[last()]")

Neither of these seem to return the last node.
"Last node" is ambiguous here. If you want the last MessageID element in
each Message, then //Message/MessageID[position()=last()] will return a
node-set containing those elements (in your example, two MessageID
elements, because you have two Message elements which match, each one
containing one MessageID element which occurs last).

For that reason, SelectSingleNode cannot apply...so what "last" node do
you actually want? If it's the last MessageID in the entire document,
then //MessageID[position()=last()] won't do it: that will still return
two elements.

//Message[position()=last()]/MessageID[position()=last()] would work...

Be careful of white-space: last() doesn't always do what you expect and
return the last element in a sequence. It ought to be called last-node()
and there should be a real last() function that returns the last element
of a sequence.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Oct 10 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SimonC | last post by:
I would like to return data from the last 2 weeks of each given month in Javascript, but in 2 formats. So, the penultimate week (Monday to Sunday) and the last week (Monday to ??) I'm not...
7
by: Scott W Gifford | last post by:
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...
3
by: AndyBell | last post by:
Hi all! I have an Access 2000 database for the Habiat for Humanity where I work. This is the second database I have written and it gets a bit more complex each time... I have learned much and...
32
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
by: rf | last post by:
Hey I need to be able to get the dates from last wednesday to the past tuesday on thursday or friday every week. How would I do that? For example: m t W TH F M T w th f I would need the...
17
by: michel.ank | last post by:
Hi, I'm using the class PrintLines and my last record of page aren't with the borders. Somebody can help me? Thanks,
6
by: magix | last post by:
Hi, when I read entries in file i.e text file, how can I determine the first line and the last line ? I know the first line of entry can be filtered using counter, but how about the last line...
13
by: Greg | last post by:
Most suggestions on this topic recommend to use a page footer and make it visible only on the last page. My problem is that the footer is half of the height of a page which means the detail would...
23
by: Florian Lindner | last post by:
Hello, can I determine somehow if the iteration on a list of values is the last iteration? Example: for i in : if last_iteration: print i*i else:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.