471,123 Members | 873 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 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 3087
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Scott W Gifford | last post: by
6 posts views Thread by magix | last post: by
23 posts views Thread by Florian Lindner | last post: by

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.