2

我检查并在许多示例中

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);

我总是得到的是 [strong] 的内容,而不是 [li] [/li] 中没有 [strong] 内容的文本:

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

1 回答 1

4

不要将此 XPath 操作使用 SimpleXML,它在某些方面受到限制,在您的情况下,限制是您不能使用 SimpleXML Xpath 返回文本节点。DOMXPath功能更强大,它可以返回所有节点类型,包括文本节点:

$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**"
于 2013-04-03T00:07:27.227 回答