-1

我有这样的 XML

<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>

我正在编写一个 PHP 脚本,需要提取<capacity>,<occupied><enrollment>每个标签的内容<atom:entry>(实际上有多个<atom:entry>标签)。现在,我查找了一些关于如何处理名称空间的提示,但仍然无法使其工作。我的代码

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

我明白了

注意:试图获取非对象的属性

4

1 回答 1

0

的返回值xpath()是一个数组,即使在您的情况下它只有一个条目。也不要//用作第二个 xpath 调用的前缀。这将返回整个文档中的所有内容条目。

因此尝试:

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

也看看DOMXPath::query

于 2013-06-17T14:45:57.730 回答