simple_xml获取属性的最大值

时间:2013-02-08 17:45:50

标签: php xpath simplexml

我正在尝试获取以下xml(代码段)中每个项目的option-node的最大值和最小值:

<body>
    <page number="1">
        <itemset name="a">
            <item id="1">
               <option value="0">no</option>
               <option value="1">rarely</option>
               <option value="3">sometimes</option>
               <option value="5">often</option>
               <scale id="ho" />
            </item>
            <item id="2">
                <option value="0">no</option>
                <option value="1">rarely</option>
                <option value="3">sometimes</option>
                <option value="5">often</option>
                <scale id="hi" />
            </item>
        </itemset>
    </page>
</body>

我在foreach循环中这样做......

...

$scid="ho";
$total=0;
$count=0;

foreach ($txml->xpath("//item[scale/@id='$scid']") as $item) {
    $score=$rxml->xpath("//item[@id='".$item['id']."']"); // get a value from another simple_xml-object
$total+=intval($score[0]);
$count++;
// ******* problem starts here...           
$values = $item->option['value'];
// doing sth with $values to get max and min value
    echo '<pre>'; 
    print_r($values); // ******* 
    echo '</pre>';
} // foreach $item

...

我在$values得到的是

SimpleXMLElement Object
(
    [0] => 0
)

代替$values = $item->xpath('option[@value]');,我得到......

    Array
(
    [0] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [value] => 0
            )

    )

    [1] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [value] => 1
            )

    )

    [2] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [value] => 3
            )

    )

    [3] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [value] => 5
            )

    )

)

我正在寻找一个更直接的解决方案 - 或者如何从那里获得最小值和最大值。

我想得到$values['max']=5$values['min']=0

提前谢谢你,

道之

PS:因为我无法使用simple_xml并且不想切换到DOM,所以我决定使用一些基本的PHP来快速而肮脏:

foreach ($item->xpath("//item[@id='".$item['id']."']/option/@value") as $value) {
    $v[]=(int)$value->value;
}
echo max($v);
echo min($v);

1 个答案:

答案 0 :(得分:0)

post的帮助下,我得到了以下示例:

$doc = new DOMDocument();
@$doc->loadHTML($htmlcontent);

$selector = new DOMXPath($doc);

// get max value of item with id 1
$result = $selector->query('//item[@id=1]/option[not(preceding-sibling::option/@value >= @value) and not(following-sibling::option/@value > @value)]/@value');

if(!$result || $result->length < 1) {
    die('error');
}
foreach($result as $item) {
    echo 'max: ', $result->item(0)->value, PHP_EOL;
}


// get min value of item with id 1
$result = $selector->query('//item[@id=1]/option[not(preceding-sibling::option/@value <= @value) and not(following-sibling::option/@value < @value)]/@value');

if(!$result || $result->length < 1) {
    die('error');
}
foreach($result as $item) {
    echo 'min: ' . $result->item(0)->value, PHP_EOL;
}