XPath help needed

G

Guest

Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I've tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can't figure out why, although I'm sure that it's
simple!

Can anyone see what I am doing wrong?

TIA,
 
G

Guest

xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")
 
G

Guest

Phillip,

I have tried both of these. For some reason each returns the first
PresentationGroup. Any idea why?

--
Joe


Phillip Williams said:
xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Joe said:
Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I've tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can't figure out why, although I'm sure that it's
simple!

Can anyone see what I am doing wrong?

TIA,
 
M

Martin Honnen

Joe wrote:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>
xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

Here is a complete C# code snippet with your provided XML as the input
XML and your provided XPath expression, that of course finds the element:

string xmlMarkup = @"<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlMarkup);
XmlElement presentationGroup =
xmlDocument.DocumentElement.SelectSingleNode(@"Content/Line/PresentationGroup[last()]")
as XmlElement;
if (presentationGroup != null) {
Console.WriteLine(presentationGroup.OuterXml);
}
else {
Console.WriteLine("No element found.");
}

So somewhere your real code and/or real XML is more complex. Just
guessing, does that XML document declare namespaces, in particular a
default namespace e.g.
<Form xmlns="http://example.com/2006/ns1">
? Then check
<http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>
 
G

Guest

Martin,

In all honesty, I do not declare a namespace. Should I?
--
Joe


Martin Honnen said:
Joe wrote:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>
xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

Here is a complete C# code snippet with your provided XML as the input
XML and your provided XPath expression, that of course finds the element:

string xmlMarkup = @"<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlMarkup);
XmlElement presentationGroup =
xmlDocument.DocumentElement.SelectSingleNode(@"Content/Line/PresentationGroup[last()]")
as XmlElement;
if (presentationGroup != null) {
Console.WriteLine(presentationGroup.OuterXml);
}
else {
Console.WriteLine("No element found.");
}

So somewhere your real code and/or real XML is more complex. Just
guessing, does that XML document declare namespaces, in particular a
default namespace e.g.
<Form xmlns="http://example.com/2006/ns1">
? Then check
<http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616>
 
G

Guest

Oh my mistake, you should be looking for the last line's presentation group
element like this:
xmlDoc.DocumentElement.SelectSingleNode("//Line[last()]/PresentationGroup")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line[last()]/PresentationGroup[last()]")
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Joe said:
Phillip,

I have tried both of these. For some reason each returns the first
PresentationGroup. Any idea why?

--
Joe


Phillip Williams said:
xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Joe said:
Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I've tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can't figure out why, although I'm sure that it's
simple!

Can anyone see what I am doing wrong?

TIA,
 
G

Guest

I'm not looking for the last line's presentation group; I'm looking for the
last presentation group. I do know that the last one will be in a Line node,
but it will not be in the last Line node.

--
Joe


Phillip Williams said:
Oh my mistake, you should be looking for the last line's presentation group
element like this:
xmlDoc.DocumentElement.SelectSingleNode("//Line[last()]/PresentationGroup")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line[last()]/PresentationGroup[last()]")
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Joe said:
Phillip,

I have tried both of these. For some reason each returns the first
PresentationGroup. Any idea why?

--
Joe


Phillip Williams said:
xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I've tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can't figure out why, although I'm sure that it's
simple!

Can anyone see what I am doing wrong?

TIA,
 
G

Guest

The SelectSingleNode would find the first matched presentationGroup that is
the last within its parent element. You are then looking for to get a list of
all presentationGroups and pickup the last one in the list like this:

Dim xnl As XmlNodeList =
xmlDoc.DocumentElement.SelectNodes("//PresentationGroup")
Dim xe As XmlNode = xnl.Item(xnl.Count - 1)

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Joe said:
I'm not looking for the last line's presentation group; I'm looking for the
last presentation group. I do know that the last one will be in a Line node,
but it will not be in the last Line node.

--
Joe


Phillip Williams said:
Oh my mistake, you should be looking for the last line's presentation group
element like this:
xmlDoc.DocumentElement.SelectSingleNode("//Line[last()]/PresentationGroup")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line[last()]/PresentationGroup[last()]")
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Joe said:
Phillip,

I have tried both of these. For some reason each returns the first
PresentationGroup. Any idea why?

--
Joe


:


xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I've tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can't figure out why, although I'm sure that it's
simple!

Can anyone see what I am doing wrong?

TIA,
 
G

Guest

Je vous remercie.
--
Joe


Phillip Williams said:
The SelectSingleNode would find the first matched presentationGroup that is
the last within its parent element. You are then looking for to get a list of
all presentationGroups and pickup the last one in the list like this:

Dim xnl As XmlNodeList =
xmlDoc.DocumentElement.SelectNodes("//PresentationGroup")
Dim xe As XmlNode = xnl.Item(xnl.Count - 1)

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Joe said:
I'm not looking for the last line's presentation group; I'm looking for the
last presentation group. I do know that the last one will be in a Line node,
but it will not be in the last Line node.

--
Joe


Phillip Williams said:
Oh my mistake, you should be looking for the last line's presentation group
element like this:
xmlDoc.DocumentElement.SelectSingleNode("//Line[last()]/PresentationGroup")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line[last()]/PresentationGroup[last()]")
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Phillip,

I have tried both of these. For some reason each returns the first
PresentationGroup. Any idea why?

--
Joe


:


xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I've tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can't figure out why, although I'm sure that it's
simple!

Can anyone see what I am doing wrong?

TIA,
 
G

Guest

You are welcome.
--
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Joe said:
Je vous remercie.
--
Joe


Phillip Williams said:
The SelectSingleNode would find the first matched presentationGroup that is
the last within its parent element. You are then looking for to get a list of
all presentationGroups and pickup the last one in the list like this:

Dim xnl As XmlNodeList =
xmlDoc.DocumentElement.SelectNodes("//PresentationGroup")
Dim xe As XmlNode = xnl.Item(xnl.Count - 1)

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Joe said:
I'm not looking for the last line's presentation group; I'm looking for the
last presentation group. I do know that the last one will be in a Line node,
but it will not be in the last Line node.

--
Joe


:

Oh my mistake, you should be looking for the last line's presentation group
element like this:
xmlDoc.DocumentElement.SelectSingleNode("//Line[last()]/PresentationGroup")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line[last()]/PresentationGroup[last()]")
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Phillip,

I have tried both of these. For some reason each returns the first
PresentationGroup. Any idea why?

--
Joe


:


xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


:

Hello All:

I have an xml document with the following structure, where Form is the
document element:

<Form>
<Content>
<Line>blah blah blah</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
<Line>blah blah blah 2</Line>
<Line>
<PresentationGroup>
miscellaneous tags
</PresentationGroup>
</Line>
</Content>
</Form>

I want an XPath to the last PresentationGroup node (there are multiple
PresentationGroup nodes per xml document and the number of PresentationGroup
nodes per document changes from one document to the next). I've tried
"Content/Line/PresentationGroup[last()]" and am getting nothing.

My code looks like:

xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]")

This returns Nothing. I can't figure out why, although I'm sure that it's
simple!

Can anyone see what I am doing wrong?

TIA,
 
M

Martin Honnen

Joe said:
I'm not looking for the last line's presentation group; I'm looking for the
last presentation group. I do know that the last one will be in a Line node,
but it will not be in the last Line node.

Then

xmlDocument.DocumentElement.SelectSingleNode(@"(Content/Line/PresentationGroup)[last()]")

should do.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top