473,804 Members | 3,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(xR es.GetResponseS tream());

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.Documen tElement.Select Nodes("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("Node s: " + 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 4241
I'm no expert on xpath, but I think you need:
nodeList = xmlFeed.Documen tElement.Select Nodes("/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.n o> wrote in message
news:0z******** ************@ne ws4.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(xR es.GetResponseS tream());

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.Documen tElement.Select Nodes("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("Node s: " + 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.Documen tElement.Select Nodes("/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.XPat h.XPathExceptio n: 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.Documen tElement.Select Nodes("/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.n o> wrote in message
news:H3******** ************@ne ws2.e.nsc.no...
Øyvind Jægtnes wrote:
Iain A. Mcleod wrote:
I'm no expert on xpath, but I think you need:
nodeList = xmlFeed.Documen tElement.Select Nodes("/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(xR es.GetResponseS tream());

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.Documen tElement.Select Nodes("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

XmlNamespaceMan ager nm = new XmlNamespaceMan ager(xmlFeed.Na meTable);
nm.AddNamespace ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
nm.AddNamespace ("rss", "http://purl.org/rss/1.0/");
nodeList =
xmlFeed.Documen tElement.Select Nodes("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.Documen tElement.Select Nodes("RDF/item/title/text()");


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

XmlNamespaceMan ager nm = new XmlNamespaceMan ager(xmlFeed.Na meTable);
nm.AddNamespace ("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
nm.AddNamespace ("rss", "http://purl.org/rss/1.0/");
nodeList =
xmlFeed.Documen tElement.Select Nodes("rdf:RDF//rss:item/title/text()");


Hmm this actually throws exception ;)
But with:
nodeList =
xmlFeed.Documen tElement.Select Nodes("/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
1667
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 selected part of that XML file through a XPath expression. These action have actually to modify the structure of the XML file itself and that seems to me like a big hurdle. Maybe I am wrong but I will try to explain what I want to do...
4
8979
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 xmlns="http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefini tion" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"
1
1723
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 to build a new XMLDocument from the Nodelist and apply the Stylesheet then. Is this realy neccessary or is there a simple way to generate an "XPath-Navigable-Document" from my Nodelist wich I seem to need for the XSLTransform? This is the code...
5
1920
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 I've been unsuccessful and I'd like to know if this is even possible and if so how to do it. An example: XMLDOC.SELECTNODES(/root/child) (sees the literal 'csharpvariable', not the actual underlying C# variable
18
7740
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); System.out.println(xp.evaluate(new InputSource(new FileInputStream("a.xml"))));
9
3604
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 following XML <Files> <Application key="one"> <Version>1</Version> <Age>120</Age> </Application>
6
3405
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 xpath, ect. It does all the branching for the different implementations within itself. I am working on a new method for this class that will, given an xpath, remove all nodes that match from the document. It works great in IE, heres the IE code.
1
1758
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
3046
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 stylesheet. Here's the Java/XPath I'm using: import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.SAXException;
0
9591
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10087
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.