如何使用 XPath 选择以下同级/XML 标记 [英] How to select following sibling/XML tag using XPath

查看:26
本文介绍了如何使用 XPath 选择以下同级/XML 标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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:

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?

推荐答案

我将如何完成下一个兄弟姐妹有没有更简单的方法这是?

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

您可以使用:

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

但我宁愿直接使用:

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

假设:

  1. 用于评估 XPath 表达式的上下文节点是所有 tr 元素的父元素 - 未显示在您的问题中.

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

每个 tr 元素只有一个 td 具有 class 属性值 'name' 并且只有一个 td 具有 class 属性值 'desc'.

Each tr element has only one td with class attribute valued 'name' and only one td with class attribute valued 'desc'.

这篇关于如何使用 XPath 选择以下同级/XML 标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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