1

我有以下 XML 文件。对于不同的网站,有 4 行不断重复。这些是_URL _Away _Home _Draw。这些中的每一个都由网站预先设置。我需要比较所有 _Away 行以找到最高值,但有时这些行可能只有 1 行,有时可能多达 32 行。我想知道的是,有没有办法得到这些通过定义字符串的结尾而不必为每个网站显式声明整个字符串?

<XMLSOCCER.COM>
    <Odds>
        <Id>1547</Id>
        <_10Bet_Home_Home>1.31</_10Bet_Home_Home>
        <_10Bet_Home_Url>http://en.10bet.com</_10Bet_Home_Url>
        <_10Bet_Home_Away>8.50</_10Bet_Home_Away>
        <_10Bet_Home_Draw>5.40</_10Bet_Home_Draw>
        <Bet_At_Home_Home>1.25</Bet_At_Home_Home>
        <Bet_At_Home_Url>http://www.bet-at-home.com/</Bet_At_Home_Url>
        <Bet_At_Home_Away>10.00</Bet_At_Home_Away>
        <Bet_At_Home_Draw>5.75</Bet_At_Home_Draw>
        <Bet365_Url>http://www.bet365.com/</Bet365_Url>
        <Bet365_Home>1.30</Bet365_Home>
        <Bet365_Away>9.00</Bet365_Away>
        <Bet365_Draw>5.50</Bet365_Draw>
        <BetVictor_Home>1.30</BetVictor_Home>
        <BetVictor_Url>http://www.betvictor.com/</BetVictor_Url>
        <BetVictor_Away>9.00</BetVictor_Away>
        <BetVictor_Draw>5.40</BetVictor_Draw>
        <Bwin_Home>1.28</Bwin_Home>
    </Odds>
</XMLSOCCER.COM>
4

3 回答 3

1

您可以使用 XPath 来获取所有以_Away. 这是一个完成您想要的代码片段:

<?php
$xml = <<<XML
<XMLSOCCER.COM>
<Odds>
    <Id>1547</Id>
    <_10Bet_Home_Home>1.31</_10Bet_Home_Home>
    <_10Bet_Home_Url>http://en.10bet.com</_10Bet_Home_Url>
    <_10Bet_Home_Away>8.50</_10Bet_Home_Away>
    <_10Bet_Home_Draw>5.40</_10Bet_Home_Draw>
    <Bet_At_Home_Home>1.25</Bet_At_Home_Home>
    <Bet_At_Home_Url>http://www.bet-at-home.com/</Bet_At_Home_Url>
    <Bet_At_Home_Away>10.00</Bet_At_Home_Away>
    <Bet_At_Home_Draw>5.75</Bet_At_Home_Draw>
    <Bet365_Url>http://www.bet365.com/</Bet365_Url>
    <Bet365_Home>1.30</Bet365_Home>
    <Bet365_Away>9.00</Bet365_Away>
    <Bet365_Draw>5.50</Bet365_Draw>
    <BetVictor_Home>1.30</BetVictor_Home>
    <BetVictor_Url>http://www.betvictor.com/</BetVictor_Url>
    <BetVictor_Away>9.00</BetVictor_Away>
    <BetVictor_Draw>5.40</BetVictor_Draw>
    <Bwin_Home>1.28</Bwin_Home>
</Odds>
</XMLSOCCER.COM>
XML;

$sxe                 = new SimpleXMLElement($xml);
$nodesEndingWithAway = $sxe->xpath('//*[substring(name(),string-length(name())-3) = "Away"]');

$highestValue = 0;
$nodeName     = '';

foreach ($nodesEndingWithAway as $node) {
    if ((float) $node > $highestValue) {
        $highestValue = (float) $node;
        $nodeName     = $node->getName();
    }
}

echo "Highest value is {$highestValue} from node {$nodeName}.\n";

输出:

Highest value is 10 from node Bet_At_Home_Away.

注意:我认为可以使用单个 XPath 表达式来完成它,而无需使用foreach.

于 2013-07-18T20:18:46.973 回答
0

您可以使用 XPath 做到这一点。

$doc = new DOMDocument();
$doc->load($filename);

$xpath = new DOMXPath($doc);
$elements = $xpath->query('/XMLSOCCER.COM/Odds/*[substring(name(),string-length(name())-3) = "Away"]');

$maxValue = 0;
foreach ($elements as $element) {
  $value = floatval($element->nodeValue);
  $maxValue = max($maxValue, $value);
}  
于 2013-07-18T20:20:29.480 回答
0

编辑:非常压缩:

$maxbid = max(array_map('floatval', $xml->xpath("//*[substring(name(),string-length(name())-" . (strlen($search) - 1) . ") = '$search']")));

分几个步骤:

使用simplexmlxpath

$search = "_Away";
$xml = simplexml_load_string($x);
$results = $xml->xpath("//*[substring(name(),string-length(name())-" . (strlen($search) - 1) . ") = '$search']");

循环遍历您的结果:

foreach ($results as $result) echo "$result <br />";

打印最高结果:

echo "highest: " . number_format(max(array_map('floatval', $results)), 2, '.', ',');

看到它工作:http ://codepad.viper-7.com/iEpGz9

于 2013-07-18T20:22:42.527 回答