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

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

问题描述

我只想自己选择一个类叫做.date



出于某种原因,我无法使其工作。如果有人知道我的代码有什么问题,那将非常感谢。

  @ $ doc = new DOMDocument(); 
@ $ doc-> loadHTML($ html);
$ xml = simplexml_import_dom($ doc); //只是为了使xpath更简单
$ images = $ xml-> xpath('// [@ class =date]');
foreach($ images为$ img)
{
echo $ img。;


解决方案

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



我们的问题



CSS 选择器:

  .foo 

会选择任何具有 foo 类的元素。

你如何在XPath中做到这一点?



虽然XPath比CSS更强大,但XPath并没有本地等价的CSS类选择器。然而,有一个解决方案。



正确的做法



XPath 是:

// * [contains(concat(,normalize函数

=https://www.w3.org/TR/1999/REC-xpath-19991116/ =nofollow noreferrer>规范化空间去除前导和尾随空白(并且还替换了空白字符序列由一个单一的空间)。

(从更一般的意义上说)这也是CSS选择器的等价物:

  * [class〜=foo] 

,它将匹配其 class 属性值为空格分隔值列表的任何元素,其中一个元素与 foo 完全相同。 p>

一些明显但错误的做法



XPath选择器:

  // * [@ class =foo] 

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

 < div class =foo bar> 

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

 < div class =foo> 

'改进'XPath选择器

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

也不起作用!因为它错误地将元素与类 foobar 匹配,例如
$ b

  < div class =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天全站免登陆