使用 xpath 选择一个 css 类 [英] Selecting a css class with xpath
问题描述
我只想选择一个名为 .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屋!
查看全文
相关文章
-
用xpath选择一个css类;
-
使用xpath依次选择一个元素;
-
如何通过CSS类和XPath找到一个元素?;
-
XPath和选择一个节点;
-
css选择一个带有2类的元素;
-
XPath - 选择一个元素或另一个;
-
如何使用另一个 CSS 类覆盖一个 CSS 类的属性;
-
如何使用CSS(无js)选择每隔一个div类元素;
-
如何使用CSS(无js)选择每隔一个div类元素;
-
使用 xpath 选择第一个结果的孩子;
-
在另一个 css 类中定位一个 css 类;
-
在另一个css类中定位一个css类;
-
CSS有一个:模糊选择器(伪类)?;
-
CSS有一个:模糊选择器(伪类)?;
-
PHP XPath选择最后一个匹配元素;
-
XPath来选择下一个兄弟;
-
XPath 随机选择一个子节点;
-
使用CSS选择第一个后代;
-
CSS选择器选择给定类的第一个元素;
-
CSS:第一个孩子使用类;
-
第一个元素的CSS选择器类;
-
第一个元素的CSS选择器类;
-
是否有一个CSS选择器由类前缀?;
-
CSS选择具有特定类的第一个元素;
-
CSS:选择上一个同级;
PHP最新文章
- 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers;
- 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误;
- 什么是NCFB和NOFB模式?;
- 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是);
- 如何处理致命错误:cURL错误7:无法连接到xxxx端口443;
- 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串;
- phpMyAdmin的#2054无法登录到MySQL服务器;
- SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败;
- 在PHPExcel中设置字体颜色,字体和字体大小;
- 如何解决cURL错误(7):无法连接到主机?;