Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / June 2004

Tip: Looking for answers? Try searching our database.

selecting specific elements with xpath

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lawrence Oluyede - 27 Jun 2004 10:34 GMT
I've some problems with filtering out a specific element by the following
xml snippet (is part of a bigger xml document):

<entry>
<link href="http://www.blogger.com/atom/5281182/108809850419059234"
rel="service.edit" title="test" type="application/x.atom+xml"/>
<author>
<name>Lawrence</name>
</author>
<issued>2004-06-24T19:34:04+02:00</issued>
<modified>2004-06-24T17:35:04Z</modified>
<created>2004-06-24T17:35:04Z</created>
<link href="http://loluyede.blogspot.com/2004/06/test_24.html"
rel="alternate" title="test" type="text/html"/>
<id>tag:blogger.com,1999:blog-5281182.post-108809850419059234</id>
<title mode="escaped" type="text/html">test</title>
<content type="application/xhtml+xml"
xml:base="http://loluyede.blogspot.com" xml:lang="en-US"
xml:space="preserve">
<div xmlns="http://www.w3.org/1999/xhtml">this test is a post</div>
</content>
</entry>

The code I use is (assume that navigator is positioned on the <entry>
element):

XPathNavigator nav = navigator.Clone();
XPathNodeIterator iter = nav.SelectDescendants(XPathNodeType.Element,
true);

iter = nav.Select("//link[@type=\"text/html\" and @rel=\"alternate\"]");

and it returns me 0 nodes, instead if i copy and paste the above snippet in
a single document (so <entry> becomes the root) and open it with
XPathDocument the above code works, why?

Signature

Lawrence
"ain't nothin' but fun."

Oleg Tkachenko [MVP] - 27 Jun 2004 13:29 GMT
> XPathNodeIterator iter = nav.SelectDescendants(XPathNodeType.Element,
> true);
>
> iter = nav.Select("//link[@type=\"text/html\" and @rel=\"alternate\"]");

That's sort of weird. You select descendants and then discarding the
variable, making another selection. What's the point of the
SelectDescendants() call?

Signature

Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Lawrence Oluyede - 27 Jun 2004 12:49 GMT
In data Sun, 27 Jun 2004 14:29:35 +0200, Oleg Tkachenko [MVP] ha scritto:

> That's sort of weird. You select descendants and then discarding the
> variable, making another selection. What's the point of the
> SelectDescendants() call?

Yeah you're right but removing that call doesn't solve the problem :(

Signature

Lawrence
"ain't nothin' but fun."

Oleg Tkachenko [MVP] - 27 Jun 2004 14:08 GMT
> iter = nav.Select("//link[@type=\"text/html\" and @rel=\"alternate\"]");
>
> and it returns me 0 nodes, instead if i copy and paste the above snippet in
> a single document (so <entry> becomes the root) and open it with
> XPathDocument the above code works, why?

Chances are you've got namespaces involved. Show us entry ancestors.

Signature

Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Lawrence Oluyede - 27 Jun 2004 15:39 GMT
In data Sun, 27 Jun 2004 15:08:52 +0200, Oleg Tkachenko [MVP] ha scritto:

> Chances are you've got namespaces involved. Show us entry ancestors.

The file is http://loluyede.blogspot.com/atom.xml

Thanks!

Signature

Lawrence
" It's probably for the best. What use does Microsoft have for an employee
who knows the W3C standards?"
-- an anonymous about tantek departure from microsoft

Oleg Tkachenko [MVP] - 27 Jun 2004 17:19 GMT
>>Chances are you've got namespaces involved. Show us entry ancestors.
>
> The file is http://loluyede.blogspot.com/atom.xml

Yeah, I was right. Look at this:

<feed version="0.3" xmlns="http://purl.org/atom/ns#">

That xmlns="http://purl.org/atom/ns#" is default namespace declaration.
That means feed element and all its descendants elements are in
"http://purl.org/atom/ns#" namespace.

Read about default namespace and XPath (there was good article at
Extreme XML column a couple of years ago) and take a look at
XPathNavigator.Select(XPathExpression) method documentation for a sample
of selecting nodes with default namespace involved.

Signature

Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Lawrence Oluyede - 27 Jun 2004 16:45 GMT
In data Sun, 27 Jun 2004 18:19:37 +0200, Oleg Tkachenko [MVP] ha scritto:

> Yeah, I was right. Look at this:
>
> <feed version="0.3" xmlns="http://purl.org/atom/ns#">

Yeah I did know because I've added namespace capability for the parsing
object model, maybe that part doesn't recognize the namespace declaration

> That xmlns="http://purl.org/atom/ns#" is default namespace declaration.
> That means feed element and all its descendants elements are in
> "http://purl.org/atom/ns#" namespace.

Right

> Read about default namespace and XPath (there was good article at
> Extreme XML column a couple of years ago)

I know, I've read it sometimes ago

> and take a look at
> XPathNavigator.Select(XPathExpression) method documentation for a sample
> of selecting nodes with default namespace involved.

I try to "wire in" the namespace declaration, thanks!

Signature

Lawrence
" It's probably for the best. What use does Microsoft have for an employee
who knows the W3C standards?"
-- an anonymous about tantek departure from microsoft


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.