首页 > 解决方案 > 按日和月过滤 XML

问题描述

美好的一天,我想弄清楚如何过滤 xml 文件以显示当前月份和日期的项目。这有点像“历史上的这一天”,今天 14/10 将显示日期为 14/10 的 xml 内容,无论年份如何。该代码是我正在尝试自定义的 joomla 模块的一部分,因此我可以显示我的自定义历史事件。
亲切的问候

我的xml:

        <?xml version="1.0" encoding="utf-8"?>
    <articles>
        <article>
            <url>/someurl</url>
            <title>Title</title>
            <text><![CDATA[Event info text]]></text>
            <date>1914-10-14</date>
            <date_publish>0</date_publish>
            <image>/image.jpg</image>
        </article>
        <article>
            <url>/someurl</url>
            <title>Title</title>
            <text><![CDATA[Event info text]]></text>
            <date>1945-10-14</date>
            <date_publish>0</date_publish>
            <image>/image.jpg</image>
        </article>
    </articles>

php文件

class NSP_GK5_xml_file_Model {
    // Method to get sources of articles
    static function getSources($config) {
        $content = array();
        // if there are selected files - set the variables
        if($config['xml_file'] != -1 && file_get_contents(__FILE__) && ini_get('allow_url_fopen')) {
            // loading file content
            $file_content = file_get_contents(JPATH_ROOT . DS . 'modules' . DS . 'mod_news_pro_gk5' . DS . 'external_data' . DS . $config['xml_file']);
            //
            $xml = new SimpleXMLElement($file_content);
            //
            if(count($xml->article) > 0) {
                //
                $art = array();
                //
                foreach ($xml->article as $element) {
                    //
                    foreach($element as $key => $value) {
                        $art[$key] = (string) $value;
                    }
                    //
                    array_push($content, (array) $art);
                }
            }
        }
        //
        return $content;
    }
    // Method to get articles in standard mode 
    static function getArticles($items, $config, $amount) { 
        $content = array();
        //
        for($i = $config['offset']; $i < $amount + $config['offset']; $i++) {
            if(isset($items[$i])) {
                array_push($content, $items[$i]);
            }
        }
        // the content array
        return $content; 
    }
}

// EOF

标签: xmljoomla

解决方案


使用 Xpath 表达式。日期应该是固定长度的格式。所以你可以摆脱字符串比较。

$month = '10';
$day = '14';
$expression = "//article[substring(date, 6, 5) = '$month-$day']";

$root = new SimpleXMLElement($xmlString);
$articles = [];
foreach ($root->xpath($expression) as $article) {
    $articles[] = [
        'title' => (string)($article->title ?? ''),
        'date' => (string)($article->date ?? ''),
        // ...
    ];
}
var_dump($articles);

你可能会注意到我明确地阅读了数据。通用(抽象)读取意味着您可能会错过结果中的键或携带您不使用的数据。它使您的源不太稳定。所以默认情况下要具体。

同样可以在 DOM 中实现:

$month = '10';
$day = '14';
$expression = "//article[substring(date, 6, 5) = '$month-$day']";

$document = new DOMDocument();
$document->loadXML($xmlString);
$xpath = new DOMXpath($document);

$articles = [];
foreach ($xpath->evaluate($expression) as $article) {
    $articles[] = [
        'title' => $xpath->evaluate('string(title)', $article),
        'date' => $xpath->evaluate('string(date)', $article),
        // ...
    ];
}

var_dump($articles);

推荐阅读