使用 xpath 选择一个 css 类 [英] Selecting a css class with xpath

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

问题描述

我只想选择一个名为 .date 的类

出于某种原因,我无法让它工作.如果有人知道我的代码有什么问题,将不胜感激.

@$doc = new DOMDocument();@$doc->loadHTML($html);$xml = simplexml_import_dom($doc);//只是为了让 xpath 更简单$images = $xml->xpath('//[@class="date"]');foreach ($images as $img){回声 $img." ";}

解决方案

我想写这个问题的规范答案,因为上面的答案有问题.

我们的问题

CSS 选择器:

.foo

将选择具有 foo 类的任何元素.

您如何在 XPath 中执行此操作?

尽管 XPath 比 CSS 更强大,XPath 没有原生的 CSS 类选择器.但是,有一个解决方案.

正确的做法

XPath 中的等效选择器是:

//*[contains(concat(" ", normalize-space(@class), " "), " foo ")]

函数 normalize-space 去除前导和尾随空格(并用一个空格替换空白字符序列).

(在更一般的意义上)这也相当于 CSS 选择器:

*[class~="foo"]

将匹配任何 class 属性值为以空格分隔的值列表的任何元素,其中一个值完全等于 foo.

一些明显但错误的方法

XPath 选择器:

///*[@class="foo"]

不起作用!因为它不会匹配具有多个类的元素,例如

如果类名周围有任何额外的空格,它也不会匹配:

改进的"XPath 选择器

///*[contains(@class, "foo")]

也不行!因为它错误地匹配了 foobar 类的元素,例如

感谢这位小伙子,他是我在网上找到的最早发布的针对此问题的解决方案:http://dubinko.info/blog/2007/10/01/simple-parsing-of-space-seprated-attributes-in-xpathxslt/

I want to select just a class on its own called .date

For some reason, I cannot get this to work. If anyone knows what is wrong with my code, it would be much appreciated.

@$doc = new DOMDocument();
@$doc->loadHTML($html);
$xml = simplexml_import_dom($doc); // just to make xpath more simple
$images = $xml->xpath('//[@class="date"]');                             
foreach ($images as $img)
{
    echo  $img." ";
}

解决方案

I want to write the canonical answer to this question because the answer above has a problem.

Our problem

The CSS selector:

.foo

will select any element that has the class foo.

How do you do this in XPath?

Although XPath is more powerful than CSS, XPath doesn't have a native equivalent of a CSS class selector. However, there is a solution.

The right way to do it

The equivalent selector in XPath is:

//*[contains(concat(" ", normalize-space(@class), " "), " foo ")]

The function normalize-space strips leading and trailing whitespace (and also replaces sequences of whitespace characters by a single space).

(In a more general sense) this is also the equivalent of the CSS selector:

*[class~="foo"]

which will match any element whose class attribute value is a list of whitespace-separated values, one of which is exactly equal to foo.

A couple of obvious, but wrong ways to do it

The XPath selector:

//*[@class="foo"]

doesn't work! because it won't match an element that has more than one class, for example

<div class="foo bar">

It also won't match if there is any extra whitespace around the class name:

<div class="  foo ">

The 'improved' XPath selector

//*[contains(@class, "foo")]

doesn't work either! because it wrongly matches elements with the class foobar, for example

<div class="foobar">

Credit goes to this fella, who was the earliest published solution to this problem that I found on the web: http://dubinko.info/blog/2007/10/01/simple-parsing-of-space-seprated-attributes-in-xpathxslt/

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

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