-1

I have XML like this

<atom:feed xmlns="http://kosapi.feld.cvut.cz/schema/3" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:osearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xml:base="https://kosapi.fit.cvut.cz/api/3/" xml:lang="cs">
    <atom:entry>
        <atom:id>urn:cvut:kos:parallel:353810000</atom:id>
        <atom:updated>2013-06-02T14:15:54.0</atom:updated>
        <atom:author>
            <atom:name>kalvotom</atom:name>
        </atom:author>
        <atom:link rel="self" href="parallels/353810000/"/>
        <atom:content atom:type="xml" xsi:type="parallel">
            <capacity>24</capacity>
            <capacityOverfill>DENIED</capacityOverfill>
            <code>101</code>
            <course xlink:href="courses/BI-EFA/">Efektivní algoritmy</course>
            <enrollment>DENIED</enrollment>
            <occupied>0</occupied>
            <parallelType>TUTORIAL</parallelType>
            <semester xlink:href="semesters/B131/">Zimní 2013/2014</semester>
            <timetableSlot>
                <day>2</day>
                <duration>2</duration>
                <firstHour>1</firstHour>
                <parity>BOTH</parity>
                <room xlink:href="rooms/T9:346/">T9:346</room>
            </timetableSlot>
        </atom:content>
    </atom:entry>
</atom:feed>

I'm writing a PHP script and need to extract contents of <capacity>, <occupied> and <enrollment> tags for every <atom:entry> (there are actually multiple <atom:entry> tags). Now, I looked up some tips about how to deal with namespaces but still wasn't able to make it work. My code

$xml = simplexml_load_file("someurl");
$xml->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
foreach($xml->xpath("//prefix:entry") as $entry) {
    $entry->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
    $content = $entry->xpath("//prefix:content");
    echo $content->enrollment . " | " . $content->occupied . "/" . $content->capacity . "<br />\n";
}

I get

Notice: Trying to get property of non-object

1 Answer 1

0

The return value of xpath() is an array, even if it has only one entry in your case. Also do not use // as prefix for the second xpath call. This would return all content entries in the whole document.

Therefore try:

foreach($xml->xpath("//prefix:entry") as $entry) {
    $entry->registerXPathNamespace('prefix', 'http://www.w3.org/2005/Atom');
    $content = $entry->xpath("prefix:content");
    foreach($content as $c) { 
        echo $c->enrollment . " | " . $c->occupied . "/" . $c->capacity . "<br />\n";
    }
}

Also have a look to DOMXPath::query

2
  • So there isn't simplier way than additional foreach if there's only one entry? Commented Jun 17, 2013 at 15:11
  • list($content) = $entry->xpath("prefix:content") + array(NULL); - see php.net/list Commented Jun 22, 2013 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.