Search code examples
xmlxpathfeed

Use Xpath to get content from linked file in current XML document


I have an XML doc which is a feed of excerpts of all blog entries, and in each entry there is a link to another XML file which holds the full content to that particular entry (bigger images, full text, etc.). Is it possible to access those inner XML docs and get values from it using XPATH?

The main document looks something like this:

    <Objects>
              <Item xml="doc.xml"></Item>  
                      // I would want to be able to access content 
                      // inside the document at Item/@xml
              <Item xml="doc2.xml"></Item>
    </Objects>

Solution

  • <?php
    $feed = "http://example.com/feed.xml";
    if (file_exists($feed)) {
    
        $xml = simplexml_load_file($feed);
        $Objects = $xml->xpath('//Objects/Item[@xml]');
        foreach ($Objects as $O) {
    $feed2 = "http://example.com/".$O."";
    }
    
    if (file_exists($feed2)) {
        $xml = simplexml_load_file($feed2);
    $feed2path = $xml->xpath('//*/*');
    echo $feed2path[@someid];
    }
    
    } 
    ?> 
    

    something like this using two xpaths and a for each?