1

我需要在第一级深度上进行所有匹配 http://framework.zend.com/manual/2.0/en/modules/zend.dom.query.html

它向我显示了所有包含类手风琴+所有子匹配的匹配

$dom = new Query($html);
$results = $dom->execute('.accordion');

我正在使用 .accordion 类获得所有 6 个节点

html

<div class="accordion">
    Test1
    <div class="accordion">
      Test2
        <div class="accordion">
            Test3
        </div>
    </div>
</div>
<div class="accordion">
    Name
    <div class="accordion">
      Name2
        <div class="accordion">
            Name3
        </div>
    </div>
</div>

如何在没有孩子的情况下只采用第一级 DOM 对象(节点列表)?
它应该是
第一个对象

<div class="accordion">
    Name
    <div class="accordion">
      Name2
        <div class="accordion">
            Name3
        </div>
    </div>
</div>

第二个对象

<div class="accordion">
    Name
    <div class="accordion">
      Name2
        <div class="accordion">
            Name3
        </div>
    </div>
</div>

谢谢!

4

1 回答 1

2

好吧,我已经测试了一些代码

    $html = '
        <div class="container">
           <div class="accordion">
            Test1
            <div class="accordion">
              Test2
                <div class="accordion">
                    Test3
                </div>
            </div>
        </div>
        <div class="accordion">
            Name
            <div class="accordion">
              Name2
                <div class="accordion">
                    Name3
                </div>
            </div>
        </div>
     </div>
    ';

   $dom = new \Zend\Dom\Query($html);
   // Will returns the first levels .accordion in .container ($results length is 2)
   $results = $dom->queryXpath('//div[@class="container"]/div[@class="accordion"]');
   echo '<pre>';
   foreach($results as $result){
       print_r($result);
   }
   exit;

回声:

DOMElement Object
(
    [tagName] => div
    [schemaTypeInfo] => 
    [nodeName] => div
    [nodeValue] => 
                Test1

                  Test2

                        Test3



    [nodeType] => 1
    [parentNode] => (object value omitted)
    [childNodes] => (object value omitted)
    [firstChild] => (object value omitted)
    [lastChild] => (object value omitted)
    [previousSibling] => (object value omitted)
    [nextSibling] => (object value omitted)
    [attributes] => (object value omitted)
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => div
    [baseURI] => 
    [textContent] => 
                Test1

                  Test2

                        Test3



)

DOMElement Object
(
    [tagName] => div
    [schemaTypeInfo] => 
    [nodeName] => div
    [nodeValue] => 
                Name

                  Name2

                        Name3



    [nodeType] => 1
    [parentNode] => (object value omitted)
    [childNodes] => (object value omitted)
    [firstChild] => (object value omitted)
    [lastChild] => (object value omitted)
    [previousSibling] => (object value omitted)
    [nextSibling] => (object value omitted)
    [attributes] => (object value omitted)
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => div
    [baseURI] => 
    [textContent] => 
                Name

                  Name2

                        Name3



)
于 2013-11-07T15:37:30.367 回答