使用XPATH获取HTML标记的类名称 [英] getting the class name of an HTML tag using XPATH

查看:402
本文介绍了使用XPATH获取HTML标记的类名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们将跨度标签<span class="my-FAV_numberis49"></span>封装在这样的复杂文档中:

Suppose we have this span tag <span class="my-FAV_numberis49"></span> enclosed in a complicated document like this:

<div id="box_content">
    <div class="heading">
        Description
    </div>
    Really cool description about something really cool.
    <br>

    <div class="more_detail">

    </div>
    <div class="more_detail">
        <span class="date">Today's Date is</span>June 06 2014
    </div>
    <span class="my-FAV_numberis49"></span>
</div>

如何仅保存span类的名称(即"my-FAV_numberis49"),我们假设文档的格式将保持不变,但是span类名称可以更改为类似"my-FAV_numberis7".有没有办法做到这一点?

How can I save the name of the span class only(i.e. "my-FAV_numberis49") Let us assume that the format of the document will be unchanged, but the span class name can change to something like "my-FAV_numberis7". Is there a way to do this?

我希望我的问题清楚.谢谢您的帮助.

I hope my question is clear. Thank you for your assistance.

推荐答案

此XPath表达式将选择 all 所有span元素,这些元素是>的子元素. ID为box_content的类属性,其中包含字符串my-FAV_numberis.

This XPath expression will select all the span elements that are children of the div with an ID of box_content that have a class attribute which contains the string my-FAV_numberis.

//div[@id='box_content']/span[contains(@class, 'my-FAV_numberis')]

它将匹配包含my-FAV_numberismy-FAV_numberis49my-FAV_numberis7任何字符串,包括other-class my-FAV_numberis99this-56-my-FAV_numberisnothere.

It will match my-FAV_numberis49, my-FAV_numberis7 and any string that contains my-FAV_numberis, including other-class my-FAV_numberis99 and this-56-my-FAV_numberisnothere.

如果在此上下文中有多个匹配的span,则将选择一个 node-set .您可以避免添加更多限制或位置谓词.

If there is more than one span that matches in this context, a node-set will be selected. You can avoid that adding more restrictions or a positional predicate.

在这种情况下,如果span last span 子元素,则可以使用:

If the span is the last span child element in that context you can use:

//div[@id='box_content']/span[last()]

这基于您提供的示例.如果span元素并不总是div child ,有时可能在另一个元素内,则可以使用 descendant 轴:

This is based on the example you provided. If the span element is not always a child of div and may sometimes be inside another element, then you can use the descendant axis:

//div[@id='box_content']//span[last()]

,它将选择出现在div内部任何级别的 last span.

which will select the last span that occurs inside the div, in any level.

编辑:从span元素中提取 class 名称,您可以使用:

EDIT: to extract the class name from the span element you can use:

//div[@id='box_content']//span[last()]/@class

这篇关于使用XPATH获取HTML标记的类名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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