simplexml and xpath, read sibling

前端 未结 2 1435
遥遥无期
遥遥无期 2021-01-27 12:15

I have the following XML file :




    
        [...]
             


        
2条回答
  •  悲哀的现实
    2021-01-27 13:02

    This solution includes reference to the Wordpress XML namespace:

    $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";
    }
    

    result:

    item-1-title
    item-2-title
    

    See http://ideone.com/qjOfIW

    The path //wp:postmeta[wp:meta_key = '_yoast_wpseo_title']/wp:meta_value is pretty straight-forward, I don't think it needs special explanation.

提交回复
热议问题