用于PHP DOMDocument的类jQuery选择器

Str*_*rae 16 php xml jquery dom css-selectors

我正在使用a DOMDocument,我想知道是否存在使用类似CSS的选择器来选择节点的方法,就像我们在jQuery中一样.

示例情况:我正在解析XML文件,其中一个片段如下所示:

<gesmes:Envelope>
    <gesmes:subject>Reference rates</gesmes:subject>
    <gesmes:Sender>
        <gesmes:name>European Central Bank</gesmes:name>
    </gesmes:Sender>
    <Cube>
        <Cube time="2009-07-13">
            <Cube currency="USD" rate="1.3975"/>
            <Cube currency="JPY" rate="129.03"/>
            <Cube currency="BGN" rate="1.9558"/>
            <Cube currency="CZK" rate="26.028"/>
        </Cube>
    </Cube>
</gesmes:Envelope>
Run Code Online (Sandbox Code Playgroud)

使用类似jQuery的选择器访问此结构将非常简单.例如,我可以使用

$("Cube[currency]")
Run Code Online (Sandbox Code Playgroud)

Cube使用'currency'属性检索所有元素.

但是我怎么能用PHP做同样的事情DOMDocument呢?我想选择具有属性或具有特定属性值的元素.

Chr*_*blé 20

如果你想操纵DOM ala Jquery,PHPQuery是适合你的.

http://code.google.com/p/phpquery/

一个简单的例子,你可以用它做什么.

// almost everything can be a chain
$li = null;
$doc['ul > li']
        ->addClass('my-new-class')
        ->filter(':last')
                ->addClass('last-li');
Run Code Online (Sandbox Code Playgroud)

  • 它向后使用DOMDocument,所以你不应该担心;)最后,它只是一个包装器,为你所有的DOM需求提供语法糖... (2认同)
  • -1; phpQuery糟透了,在遭受其缺陷的可怕之后,我建议不要使用它.这是放弃软件(2010年2月最后一次提交)[超过100个开放错误](https://code.google.com/p/phpquery/issues/list?can=2&q=type%3DDefect&colspec=ID%20Type%20Status%20Priority% 20里程碑%20Owner%20Summary&num = 100&start = 0),包括来自[拼写变量名称]的错误等基本内容(https://code.google.com/p/phpquery/issues/detail?id=231).远离这个; 找到另一个库或学习[通过内置DOM库使用Xpath](http://php.net/manual/en/class.domxpath.php). (2认同)

小智 14

看一下DOMXPathPHP 中的类.它使用XPath,因此如果您不熟悉它,则需要阅读XPath语法.有关MSDNW3Schools的一些文档,或者如果你特别勇敢,你可以阅读W3规范.

解决您的示例问题://cube[@currency]是一个XPath查询,它使用currency属性选择文档中的所有元素.使用此类与此DOMXPath类似如下:

$xpath = new DOMXpath($myDomDocument);
$cubesWithCurrencies = $xpath->query('//cube[@currency]');
Run Code Online (Sandbox Code Playgroud)

$cubesWithCurrencies现在是DOMNodeList你可以迭代的.


Joh*_*ers 6

我创建了一个库,允许您像使用jQuery一样抓取HTML5和XML文档.

您可以在GitHub上找到该库.

它应该让你完全按照自己的意愿行事!

在引擎盖下,它使用symfony/DomCrawler将CSS选择器转换为XPath选择器.即使将一个对象传递给另一个对象,它也始终使用相同的DomDocument,以确保良好的性能.

该库还包含自己的零配置自动加载器,用于PSR-0兼容库.包含的示例应该开箱即用,无需任何其他配置.


使用示例:

namespace PowerTools;

// Get file content
$htmlcode = file_get_contents( 'https://github.com' );

// Define your DOMCrawler based on file string
$H = new DOM_Query( $htmlcode );

// Define your DOMCrawler based on an existing DOM_Query instance
$H = new DOM_Query( $H->select('body') );

// Passing a string (CSS selector)
$s = $H->select( 'div.foo' );

// Passing an element object (DOM Element)
$s = $H->select( $documentBody );

// Passing a DOM Query object
$s = $H->select( $H->select('p + p') );

// Select the body tag
$body = $H->select('body');

// Combine different classes as one selector to get all site blocks
$siteblocks = $body->select('.site-header, .masthead, .site-body, .site-footer');

// Nest your methods just like you would with jQuery
$siteblocks->select('button')->add('span')->addClass('icon icon-printer');

// Use a lambda function to set the text of all site blocks
$siteblocks->text(function( $i, $val) {
    return $i . " - " . $val->attr('class');
});

// Append the following HTML to all site blocks
$siteblocks->append('<div class="site-center"></div>');

// Use a descendant selector to select the site's footer
$sitefooter = $body->select('.site-footer > .site-center');

// Set some attributes for the site's footer
$sitefooter->attr(array('id' => 'aweeesome', 'data-val' => 'see'));

// Use a lambda function to set the attributes of all site blocks
$siteblocks->attr('data-val', function( $i, $val) {
    return $i . " - " . $val->attr('class') . " - photo by Kelly Clark";
});

// Select the parent of the site's footer
$sitefooterparent = $sitefooter->parent();

// Remove the class of all i-tags within the site's footer's parent
$sitefooterparent->select('i')->removeAttr('class');

// Wrap the site's footer within two nex selectors
$sitefooter->wrap('<section><div class="footer-wrapper"></div></section>');

[...]
Run Code Online (Sandbox Code Playgroud)
支持的方法:
  1. 出于显而易见的原因,重命名为"选择"
  2. 重命名为'void',因为'empty'是PHP中的保留字