simplexml和xpath,读取兄弟

时间:2015-01-07 09:21:15

标签: xml xpath simplexml siblings

我有以下XML文件:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
    <item>
        [...]
        <wp:postmeta>
            <wp:meta_key>_wp_old_slug</wp:meta_key>
            <wp:meta_value><![CDATA[item-1-slug]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>_yoast_wpseo_title</wp:meta_key>
            <wp:meta_value><![CDATA[item-1-title]]></wp:meta_value>
        </wp:postmeta>
        [...]
    </item>
    <item>
        [...]
        <wp:postmeta>
            <wp:meta_key>_wp_old_slug</wp:meta_key>
            <wp:meta_value><![CDATA[item-2-slug]]></wp:meta_value>
        </wp:postmeta>
        <wp:postmeta>
            <wp:meta_key>_yoast_wpseo_title</wp:meta_key>
            <wp:meta_value><![CDATA[item-2-title]]></wp:meta_value>
        </wp:postmeta>
        [...]
    </item>
</channel>
</rss>

我正在使用

循环浏览我的项目
$xmlurl = file_get_contents($xmlFile);
$xml = simplexml_load_string($xmlurl, null, LIBXML_NOCDATA);
$items = $xml->channel->item;
foreach( $items as $item ) {

}

在这个循环中,我想读取<wp:meta_key>_yoast_wpseo_title</wp:meta_key>节点的兄弟的值。例如,对于第1项,我想获得“item-1-title”。 我可能不得不使用xpath,但我真的不知道如何继续。

我该怎么做?

2 个答案:

答案 0 :(得分:3)

$xpath = './/wp:meta_key[text()="_yoast_wpseo_title"]/following-sibling::wp:meta_value[1]/text()';
$items = $xml->channel->item;
foreach( $items as $item ) {
  $result = $item->xpath($xpath);
  print "$result[0]\n";
}

// => item-1-title
// => item-2-title

XPath表达式的说明:

.                               - from the current node...
//wp:meta_key                   - get all descendant wp:meta_key nodes
[text()="_yoast_wpseo_title"]   - whose text content is _yoast_wpseo_title
/following-sibling::            - then get the siblings that come after this
wp:meta_value[1]                - with tag wp:meta_value; only take the first
/text()                         - and read its text

答案 1 :(得分:2)

此解决方案包括对Wordpress XML命名空间的引用:

$doc = new SimpleXmlElement($xml);
$doc->registerXPathNamespace ('wp', 'http://wordpress.org/export/1.0/');

$wp_meta_title = $doc->xpath("//wp:postmeta[wp:meta_key = '_yoast_wpseo_title']/wp:meta_value");

foreach ($wp_meta_title as $title) {
    echo (string)$title . "\n";
}

结果:

item-1-title
item-2-title

请参阅http://ideone.com/qjOfIW

路径//wp:postmeta[wp:meta_key = '_yoast_wpseo_title']/wp:meta_value非常简单,我认为不需要特别说明。