用xpath选择特定元素 [英] selecting specific elements with xpath

查看:75
本文介绍了用xpath选择特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在使用以下内容过滤掉特定元素时遇到了一些问题

xml片段(是更大的xml文档的一部分):


< entry>

< link href =" http://www.blogger.com/atom/5281182/108809850419059234"

rel =" service.edit"标题= QUOT;试验" type =" application / x.atom + xml" />

< author>

< name> Lawrence< / name>

< / author>

<已发布> 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"标题= QUOT;试验" 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">这个测试是一个帖子< / div>

< / content>

< / entry>


我使用的代码是(假设导航器位于< entry>

元素):


XPathNavigator nav = navigator .Clone();

XPathNodeIterator iter = nav.SelectDescendants(XPathNodeType.Element,

true);


iter = nav 。选择(" // link [@ type = \" text / html \" and @ rel = \" alternate \"]");


它返回0个节点,相反如果我将上面的代码片段复制并粘贴到

a单个文档中(所以< entry>成为root)并用
$ b打开它$ b XPathDocument以上代码有效,为什么?


-

劳伦斯

ain''t nothin''但很有趣。

解决案例

Lawrence Oluyede写道:

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

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




这有点奇怪。您选择后代,然后丢弃

变量,进行另一个选择。

SelectDescendants()调用有什么意义?


-

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


Lawrence Oluyede写道:

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

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




这有点奇怪。您选择后代,然后丢弃

变量,进行另一个选择。

SelectDescendants()调用有什么意义?


-

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


在数据Sun,2004年6月27日14:29:35 +0200,Oleg Tkachenko [MVP] ha scritto:

这有点奇怪。您选择后代,然后丢弃
变量,进行另一个选择。
SelectDescendants()调用的重点是什么?




是的你是对的,但删除该调用无法解决问题:(


-

劳伦斯

ain''t nothin''但很有趣。



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
"ain''t nothin'' but fun."

解决方案

Lawrence Oluyede wrote:

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?

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


Lawrence Oluyede wrote:

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?

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


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 :(

--
Lawrence
"ain''t nothin'' but fun."


这篇关于用xpath选择特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆