2013-04-02 5 views
2
내가 확인

와 많은 예 XPath는 문자를

following-sibling::text()[1] 

강한 태그 후 텍스트를받을 수있는 정답로 주어진다 작동하지 않습니다. 나는 별표에 관심이 텍스트를 표시 :

<?php 
    $html=' 
     <html> 
     <head> 
     </head> 
     <body>  
      <div class="someclass"> 
       <h2 class="h3">header 1</h2> 
       <ul class="bulleted"> 
        <li><strong>prop1: </strong>**name**</li> 
        <li><strong>prop2: </strong>**street**</li> 
        <li><strong>prop is 3: </strong>**city**</li> 
        <li><strong>prop 4: </strong>**more**</li> 
       </ul> 
      </div> 
     </body> 
    </html> 
'; 
    $doc = new DOMDocument(); 
    $doc->strictErrorChecking = FALSE; 
    $doc->loadHtml($html); 
    $data = simplexml_import_dom($doc); 
    $properties = $data->xpath('//strong/following-sibling::text()[1]'); 

    var_dump($properties); 

내가 항상 얻을 것은 [강력]의 내용이지만, 내용없는 [리] 내에서 텍스트가 아닌 [/ 리] [ ] 강력한 :

array(4) { 
    [0] => 
    class SimpleXMLElement#3 (1) { 
    public $strong => 
    string(7) "prop1: " 
    } 
    [1] => 
    class SimpleXMLElement#4 (1) { 
    public $strong => 
    string(7) "prop2: " 
    } 
    [2] => 
    class SimpleXMLElement#5 (1) { 
    public $strong => 
    string(11) "prop is 3: " 
    } 
    [3] => 
    class SimpleXMLElement#6 (1) { 
    public $strong => 
    string(8) "prop 4: " 
    } 
} 

당신은 내가 오류에 날 지점 만약 내가 기쁠 ...

답변

4

이 XPath를 작업에 SimpleXML을 사용하지 마십시오, 그것은 귀하의 경우, 어떤 방법으로 제한됩니다 제한 사항은 SimpleXML Xpath로 텍스트 노드를 반환 할 수 없다는 것입니다.

$xpath = new DOMXpath($doc); 
$properties = $xpath->query('//strong/following-sibling::text()[1]'); 

foreach ($properties as $property) 
    var_dump($property->textContent); 

결과 :

string(8) "**name**" 
string(10) "**street**" 
string(8) "**city**" 
string(8) "**more**" 
DOMXPath 훨씬 더 할 수있는, 그것은 텍스트 노드를 포함한 모든 노드 유형을 반환 할 수 있습니다