473,385 Members | 2,014 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,385 software developers and data experts.

XPath help needed

Joe
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,
--
Joe
Mar 24 '06 #1
10 1247

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" wrote:
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,
--
Joe

Mar 24 '06 #2
Joe
Phillip,

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

--
Joe
"Phillip Williams" wrote:

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" wrote:
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,
--
Joe

Mar 24 '06 #3


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(@"Con tent/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>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 24 '06 #4
Joe
Martin,

In all honesty, I do not declare a namespace. Should I?
--
Joe
"Martin Honnen" wrote:


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(@"Con tent/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>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Mar 24 '06 #5
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" wrote:
Phillip,

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

--
Joe
"Phillip Williams" wrote:

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" wrote:
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,
--
Joe

Mar 24 '06 #6
Joe
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" wrote:
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" wrote:
Phillip,

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

--
Joe
"Phillip Williams" wrote:

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" wrote:

> 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,
> --
> Joe

Mar 24 '06 #7
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" wrote:
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" wrote:
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" wrote:
Phillip,

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

--
Joe
"Phillip Williams" wrote:

>
> 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" wrote:
>
> > 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,
> > --
> > Joe

Mar 24 '06 #8
Joe
Je vous remercie.
--
Joe
"Phillip Williams" wrote:
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" wrote:
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" wrote:
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" wrote:

> Phillip,
>
> I have tried both of these. For some reason each returns the first
> PresentationGroup. Any idea why?
>
> --
> Joe
>
>
> "Phillip Williams" wrote:
>
> >
> > 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" wrote:
> >
> > > 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,
> > > --
> > > Joe

Mar 24 '06 #9
You are welcome.
--
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Joe" wrote:
Je vous remercie.
--
Joe
"Phillip Williams" wrote:
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" wrote:
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" wrote:

> 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" wrote:
>
> > Phillip,
> >
> > I have tried both of these. For some reason each returns the first
> > PresentationGroup. Any idea why?
> >
> > --
> > Joe
> >
> >
> > "Phillip Williams" wrote:
> >
> > >
> > > 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" wrote:
> > >
> > > > 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,
> > > > --
> > > > Joe

Mar 24 '06 #10


Joe wrote:
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(@"(Co ntent/Line/PresentationGroup)[last()]")

should do.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 25 '06 #11

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

Similar topics

3
by: Kevin | last post by:
I know this has probably been discussed many times before (I found answers when I searched yesterday), but I still can't get it to work... I have an attribute @OID that can contain any...
1
by: Philip | last post by:
Hi, I am trying to output certain nodes inside another. I have an xml template with field definitions for a form, and this includes textfields, labels, checkboxes etc plus fieldssets. I defined...
1
by: Torrent | last post by:
The Following is an Error I am getting using VS.NET 2003. When I bind the XML and XSLT Document together in Internet Explorer it works fine. However when I attempt to Load the File with the...
3
by: Torrent | last post by:
When Trying to Load an XSLT File with the XslTransform i got a rather annoying Exception being thrown "System.Xml.XPath.XPathException: XsltContext is needed for this query because of an unknown...
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
0
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
4
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
3
by: ziggyware | last post by:
Hi All, I have updated my XPath Generator software: XPath Studio .NET. ( http://www.ziggyware.com/downloads.php?cat_id=2 ) Easily select nodes from an xml file to generate XPath statements ...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.