0

これは私の xPath ですが、| 演算子は 2 だけを計算しますか? どうすれば2つ以上にできますか?以下にコードを投稿しました

function extractNodeValue($query, $xPath, $attribute = null) {
    $node = $xPath->query("//{$query}")->item(0);
    if (!$node) {
        return null;
    }
    return $attribute ? $node->getAttribute($attribute) : $node->nodeValue;
}


$document = new DOMDocument();
$document->loadHTMLfile(${'html'.$i});
$xPath = new DOMXpath($document);

    $tel = extractNodeValue('//*[@id="eventDetailInfo"]/div[3]/div[4] | //*[@id="eventDetailInfo"]/div[3]/div[3] | //*[@id="eventDetailInfo"]/div[3]/div[5]',$xPath);
4

4 に答える 4

3

と書く2+2+2と、+は二項演算子です。あなたの表現は を意味し(2+2)+2ます。

同様|に、XPath では 2 項演算子ですが、結果はオペランドと同じ型であるため、同じ方法でそれ自体を構成します: $x|$y|$zmeans ($x|$y)|$z.

于 2013-06-25T07:16:08.487 に答える
0

extractNodeValue関数に渡す XPath クエリは//a |になります。b | c、ノードのみを返しa、ノードを無視しbますc

おそらく//a |を実行したいでしょう。//b | //caは、bまたはcノードの最初のオカレンスを取得しますよね?

その場合は、$query引数の使用方法を次のように変更する必要があります。

<?php
$html = <<<HTML
<html>
    <div>
        <a>Empire Burlesque</a>
        <b>Bob Dylan</b>
        <i>USA</i>
    </div>
    <div>
        <a>Hide your heart</a>
        <b>Bonnie Tyler</b>
        <i>UK</i>
    </div>
</html>
HTML;

function extractNodeValue($query, $xPath, $attribute = null) {
    $node = $xPath->query($query)->item(0);
    if (!$node) {
        return null;
    }

    return $attribute ? $node->getAttribute($attribute) : $node->nodeValue;
}

$document = new DOMDocument();
$document->loadHTML($html);
$xPath = new DOMXpath($document);

$tel = extractNodeValue('//a | //b | //i', $xPath);
echo $tel;

出力:

Empire Burlesque
于 2013-07-02T22:04:11.153 に答える