XPath来选择下一个兄弟 [英] Xpath for choosing next sibling

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

问题描述

我有这样一段HTML的:

I have piece of HTML like this:


<dt>name</dt>
<dd>value</dd>
<dt>name2</dt>
<dd>value2</dd>

我想找到的所有地方的结构不正确,意思是有后没有 DD 标记DT 标签

I want to find all places where the structure is incorrect, meaning there is no dd tag after dt tag.

我试过这样:


//dt/following-sibling::dt

但是,这并不正常工作。有什么建议?

but this doesn't work. Any suggestions?

推荐答案

修改:由@Gaim指出,我原来的版本未能捕获终端 DT

EDIT as noted by @Gaim, my original version failed to capture a terminal dt

string xml = @"
    <root>
    <dt>name</dt>
    <dd>value</dd>
    <dt>name2</dt>
    <dt>name3</dt>
    <dd>value3</dd>
    <dt>name4</dt>
    <dt>name5</dt>
    <dd>value5</dd>
    <dt>name6</dt>
    </root>
    ";

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNodeList nodes = 
    doc.SelectNodes("//dt[not(following-sibling::*[1][self::dd])]");

foreach (XmlNode node in nodes)
{
    Console.WriteLine(node.OuterXml);
}

Console.ReadLine();

输出的是,不要那些 DT 节点有一个 DD 立即将它们以下内容:

Output is those dt nodes that do not have a dd immediately following them:

<dt>name2</dt>
<dt>name4</dt>
<dt>name6</dt>



我们在这里做的是说:

What we are doing here is saying:

//dt

所有 DT 节点,任何地点....

All dt nodes, anywhere....

[not(following-sibling::*[1]

....这样,它的的的情况下,他们的第一次下面的兄弟姐妹(无论它叫)......

....such that it's not the case that their first following sibling (whatever it is called)....

[self::dd]]

...叫 DD

这篇关于XPath来选择下一个兄弟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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