473,385 Members | 1,518 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 and nodelist .. im confused :p

I'm playing around a bit with XPath and nodelist and i want to extract
some info from a RSS feed.
The one that i am testing at can be viewed at http://slashdot.org/index.rss

Ok.. heres the deal:
I load xml via httpwebrequest etc and put it in a XmlDocument.

xmlFeed.Load(xRes.GetResponseStream());

This works fine as i can use this stream with a XmlReader to parse the
document in an ugly way.

Now i try to get all the title elemelts:

nodeList = xmlFeed.DocumentElement.SelectNodes("RDF/item/title/text()");

This should (according to my browsing of xpath and thing and reading for
a few hrs) result in getting the titles ;)
But it don't.
System.Console.WriteLine("Nodes: " + nodeList.Count + "\n");
Outputs: "Nodes: 0"

So what part of my brain isnt working? :)
Anyone spot something horribly wrong?

---
Øyvind Jægtnes
Nov 12 '05 #1
8 4220
I'm no expert on xpath, but I think you need:
nodeList = xmlFeed.DocumentElement.SelectNodes("/RDF/item/title/text()");

The RDF node must be prefixed with a forward slash to show it is an absolute
path from the root of the document...

Try it and see?
"Øyvind Jægtnes" <oj****@start.no> wrote in message
news:0z********************@news4.e.nsc.no...
I'm playing around a bit with XPath and nodelist and i want to extract
some info from a RSS feed.
The one that i am testing at can be viewed at http://slashdot.org/index.rss
Ok.. heres the deal:
I load xml via httpwebrequest etc and put it in a XmlDocument.

xmlFeed.Load(xRes.GetResponseStream());

This works fine as i can use this stream with a XmlReader to parse the
document in an ugly way.

Now i try to get all the title elemelts:

nodeList = xmlFeed.DocumentElement.SelectNodes("RDF/item/title/text()");

This should (according to my browsing of xpath and thing and reading for
a few hrs) result in getting the titles ;)
But it don't.
System.Console.WriteLine("Nodes: " + nodeList.Count + "\n");
Outputs: "Nodes: 0"

So what part of my brain isnt working? :)
Anyone spot something horribly wrong?

---
Øyvind Jægtnes

Nov 12 '05 #2
Iain A. Mcleod wrote:
I'm no expert on xpath, but I think you need:
nodeList = xmlFeed.DocumentElement.SelectNodes("/RDF/item/title/text()");

The RDF node must be prefixed with a forward slash to show it is an absolute
path from the root of the document...

Try it and see?


Still same.
But when i debug and look at the XmlElement of <rdf:RDF> it sais:
LocalName is RDF and Name is rdf:RDF.

I then made this XPath string: "/rdf:RDF/item/title/text()"

it trows exception on that :p

System.Xml.XPath.XPathException: Namespace Manager or XsltContext
needed. This query has a prefix, variable, or user-defined function.

Which i cant really decode ;)
Do i need to use Namespace Manager to get a proper query?

---
Øyvind Jægtnes
Nov 12 '05 #3
Øyvind Jægtnes wrote:
Iain A. Mcleod wrote:
I'm no expert on xpath, but I think you need:
nodeList = xmlFeed.DocumentElement.SelectNodes("/RDF/item/title/text()");

The RDF node must be prefixed with a forward slash to show it is an
absolute
path from the root of the document...

Try it and see?

Still same.
But when i debug and look at the XmlElement of <rdf:RDF> it sais:
LocalName is RDF and Name is rdf:RDF.

I then made this XPath string: "/rdf:RDF/item/title/text()"

it trows exception on that :p


Actually i've been poking around a bit and i think it has to do with
rdf:RDF is its name, but SelectNodes think that it is a prefix for a
namespace when i use it. Question then is, is it posible to "escape" the
: in someway in the query?

---
Øyvind Jægtnes
Nov 12 '05 #4
Sorry man, way over my head!

Hope someone else can help you :)

"Øyvind Jægtnes" <oj****@start.no> wrote in message
news:H3********************@news2.e.nsc.no...
Øyvind Jægtnes wrote:
Iain A. Mcleod wrote:
I'm no expert on xpath, but I think you need:
nodeList = xmlFeed.DocumentElement.SelectNodes("/RDF/item/title/text()");
The RDF node must be prefixed with a forward slash to show it is an
absolute
path from the root of the document...

Try it and see?

Still same.
But when i debug and look at the XmlElement of <rdf:RDF> it sais:
LocalName is RDF and Name is rdf:RDF.

I then made this XPath string: "/rdf:RDF/item/title/text()"

it trows exception on that :p


Actually i've been poking around a bit and i think it has to do with
rdf:RDF is its name, but SelectNodes think that it is a prefix for a
namespace when i use it. Question then is, is it posible to "escape" the
: in someway in the query?

---
Øyvind Jægtnes

Nov 12 '05 #5
Øyvind Jægtnes wrote:
I'm playing around a bit with XPath and nodelist and i want to extract
some info from a RSS feed.
The one that i am testing at can be viewed at http://slashdot.org/index.rss

Ok.. heres the deal:
I load xml via httpwebrequest etc and put it in a XmlDocument.

xmlFeed.Load(xRes.GetResponseStream());

This works fine as i can use this stream with a XmlReader to parse the
document in an ugly way.

Now i try to get all the title elemelts:

nodeList = xmlFeed.DocumentElement.SelectNodes("RDF/item/title/text()");

This should (according to my browsing of xpath and thing and reading for
a few hrs) result in getting the titles ;)


Nope, you forgot about namespaces. It should be something like

XmlNamespaceManager nm = new XmlNamespaceManager(xmlFeed.NameTable);
nm.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
nm.AddNamespace("rss", "http://purl.org/rss/1.0/");
nodeList =
xmlFeed.DocumentElement.SelectNodes("rdf:RDF//rss:item/title/text()");

Read "XML Namespaces and How They Affect XPath and XSLT" at
http://msdn.microsoft.com/library/en...ml05202002.asp

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #6
Oleg Tkachenko [MVP] wrote:
Øyvind Jægtnes wrote:
Now i try to get all the title elemelts:

nodeList = xmlFeed.DocumentElement.SelectNodes("RDF/item/title/text()");


Nope, you forgot about namespaces. It should be something like

XmlNamespaceManager nm = new XmlNamespaceManager(xmlFeed.NameTable);
nm.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
nm.AddNamespace("rss", "http://purl.org/rss/1.0/");
nodeList =
xmlFeed.DocumentElement.SelectNodes("rdf:RDF//rss:item/title/text()");


Hmm this actually throws exception ;)
But with:
nodeList =
xmlFeed.DocumentElement.SelectNodes("/rdf:RDF//rss:item/title/text()", nm);

nodeList.Count is still 0.
When i use "//*" as XPath line it takes the whole document as it should.
But if i do "/*/*/title/text()", or "//*//*/title" or any variations of
that i still get 0 count...
So im really confused ;)
Starting to wander if the best way to use XML is to iterate the nodes
and do the searching manually.. but XPath looks cool if it would work :p
Read "XML Namespaces and How They Affect XPath and XSLT" at
http://msdn.microsoft.com/library/en...ml05202002.asp


Will do..

---
Øyvind Jægtnes
Nov 12 '05 #7
Oleg Tkachenko [MVP] wrote:
Read "XML Namespaces and How They Affect XPath and XSLT" at
http://msdn.microsoft.com/library/en...ml05202002.asp


Ok, solved ;)
I finally got them extrated.. Now its time to see if i can actually use
the namespaces to check the document version (rss 0.9, 1.0, 2.0). That
would be nice :)
(correct xpath was "/rdf:RDF//rss:item/rss:title/text()")

---
Nov 12 '05 #8
Øyvind Jægtnes wrote:
I'm playing around a bit with XPath and nodelist and i want to extract
some info from a RSS feed.
The one that i am testing at can be viewed at http://slashdot.org/index.rss


Ah finally i have something semi working here ;)
While project can be seen at http://home.no.net/~ojaegt/Rss.rar if
someone are interested.

Still todo: make it so you can save a list of feeds and select between
them (GUI) and add support for ATOM feeds (library).

---
Øyvind Jægtnes
Nov 12 '05 #9

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

Similar topics

1
by: Phoebian | last post by:
Hi all, Here is a little concept problem, and I need some guideline. I am currently developing a application which delivers XML files. I want then to sequentially perform some actions on...
4
by: Gismo | last post by:
I have got file raport.rld which is an XML file generated by MS Reporting Services. The problem is: in this file are tags from two different namespaces ...
1
by: Chucker | last post by:
I would like to select some nodes from a very large XML document and then apply a stylesheet to the dom fragment / nodelist that is the result of my XPath-Query. It seems to me now that I have...
5
by: studen77 | last post by:
Thanks in advance to anyone who can help :) I'm trying to extract a specific NodeSet out of an XML file; however, I'd like to include a C# string variable as part of the XPATH statement; so far...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
9
by: DBC User | last post by:
Hi, I have an xml and I am able to use xpath to identify each node that statisfy the selection criteria. I got the node list. I would like to know is it possible to do the following for the...
6
by: Hoss | last post by:
Hello. Because IE and Mozilla have such completely different XML implementations, I have created a class to handle general XML tasks, such as iterating over nodes given an xpath, evaluating an...
1
by: luthriaajay | last post by:
How can I use XPATH to extract the value of Element Code in Java? I havent used this before so help appreciated. <Underlying> <Code>KGF</Code> </Underlying> Java Code:
2
by: A. W. Dunstan | last post by:
I'm trying to figure out how XPath expressions work, and how I can use them to extract data into a particular format. I can extract the data I want using an XPath expression, but not with an XSLT...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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...

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.