如何使用 xpath 选择以下同级/xml 标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3139402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:11:17  来源:igfitidea点击:

How to select following sibling/xml tag using xpath

xmlxpathlxml

提问by Corey Farwell

I have an HTML file (from Newegg) and their HTML is organized like below. All of the data in their specifications table is 'desc' while the titles of each section are in 'name.' Below are two examples of data from Newegg pages.

我有一个 HTML 文件(来自 Newegg),它们的 HTML 组织如下。他们的规格表中的所有数据都是' desc',而每个部分的标题都在' name 中。' 下面是来自 Newegg 页面的两个数据示例。

<tr>
    <td class="name">Brand</td>
    <td class="desc">Intel</td>
</tr>
<tr>
    <td class="name">Series</td>
    <td class="desc">Core i5</td>
</tr>
<tr>
    <td class="name">Cores</td>
    <td class="desc">4</td>
</tr>
<tr>
    <td class="name">Socket</td>
    <td class="desc">LGA 1156</td>

<tr>
    <td class="name">Brand</td>
    <td class="desc">AMD</td>
</tr>
<tr>
    <td class="name">Series</td>
    <td class="desc">Phenom II X4</td>
</tr>
<tr>
    <td class="name">Cores</td>
    <td class="desc">4</td>
</tr>
<tr>
    <td class="name">Socket</td>
    <td class="desc">Socket AM3</td>
</tr>

In the end I would like to have a class for a CPU (which is already set up) that consists of a Brand, Series, Cores, and Socket type to store each of the data. This is the only way I can think of to go about doing this:

最后,我希望为 CPU(已设置)创建一个类,该类由 Brand、Series、Cores 和 Socket 类型组成,用于存储每个数据。这是我能想到的唯一方法:

if(parsedDocument.xpath(tr/td[@class="name"])=='Brand'):
    CPU.brand = parsedDocument.xpath(tr/td[@class="name"]/nextsibling?).text

And doing this for the rest of the values. How would I accomplish the nextsibling and is there an easier way of doing this?

并对其余的值执行此操作。我将如何完成下一个兄弟姐妹,有没有更简单的方法来做到这一点?

回答by Dimitre Novatchev

How would I accomplish the nextsibling and is there an easier way of doing this?

我将如何完成下一个兄弟姐妹,有没有更简单的方法来做到这一点?

You may use:

您可以使用

tr/td[@class='name']/following-sibling::td

but I'd rather use directly:

但我宁愿直接使用

tr[td[@class='name'] ='Brand']/td[@class='desc']

This assumes that:

这假设

  1. The context node, against which the XPath expression is evaluated is the parent of all trelements -- not shown in your question.

  2. Each trelement has only one tdwith classattribute valued 'name'and only one tdwith classattribute valued 'desc'.

  1. 对 XPath 表达式进行评估的上下文节点是所有tr元素的父元素——未在您的问题中显示。

  2. 每个tr元素只有一个tdclass属性值'name',只有一个tdclass属性值'desc'

回答by Philipp

Try the following-siblingaxis (following-sibling::td).

尝试following-sibling轴 ( following-sibling::td)。