all groups > dotnet xml > june 2004 >
You're in the

dotnet xml

group:

selecting specific elements with xpath


selecting specific elements with xpath Lawrence Oluyede
6/27/2004 11:34:05 AM
dotnet xml:

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?

--
Lawrence
Re: selecting specific elements with xpath Lawrence Oluyede
6/27/2004 1:49:36 PM
In data Sun, 27 Jun 2004 14:29:35 +0200, Oleg Tkachenko [MVP] ha scritto:

[quoted text, click to view]

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

--
Lawrence
Re: selecting specific elements with xpath Oleg Tkachenko [MVP]
6/27/2004 2:29:35 PM
[quoted text, click to view]

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

--
Oleg Tkachenko [XML MVP]
Re: selecting specific elements with xpath Oleg Tkachenko [MVP]
6/27/2004 3:08:52 PM
[quoted text, click to view]

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

--
Oleg Tkachenko [XML MVP]
Re: selecting specific elements with xpath Lawrence Oluyede
6/27/2004 4:39:06 PM
In data Sun, 27 Jun 2004 15:08:52 +0200, Oleg Tkachenko [MVP] ha scritto:

[quoted text, click to view]

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

Thanks!

--
Lawrence
" It's probably for the best. What use does Microsoft have for an employee
who knows the W3C standards?"
Re: selecting specific elements with xpath Lawrence Oluyede
6/27/2004 5:45:36 PM
In data Sun, 27 Jun 2004 18:19:37 +0200, Oleg Tkachenko [MVP] ha scritto:

[quoted text, click to view]

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

[quoted text, click to view]

Right

[quoted text, click to view]

I know, I've read it sometimes ago

[quoted text, click to view]

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

--
Lawrence
" It's probably for the best. What use does Microsoft have for an employee
who knows the W3C standards?"
Re: selecting specific elements with xpath Oleg Tkachenko [MVP]
6/27/2004 6:19:37 PM
[quoted text, click to view]

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.

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button