1

以下の XML ファイルがあります。さまざまな Web サイトで常に繰り返される 4 つの行があります。これらは _URL _Away _Home _Draw です。これらのそれぞれは、Web サイトによって先頭に追加されます。すべての _Away 行を比較して最大値を見つける必要がありますが、これらの行が 1 つある場合もあれば、32 行ある場合もあります。知りたいのは、これらを取得する方法があるかどうかです。各Webサイトの文字列全体を明示的に宣言することなく、文字列の末尾を定義することによって?

<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

編集: 非常に圧縮:

$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 に答える
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 に答える