I am trying to parse an Atom XML feed using PHP. It is for an ebook publisher. All is well, except I cannot work out how to extract the last “subject” tag for each entry: that is the publisher’s tag (instead of Library of Congress subject heading = LCSH), and there are a variable number of those for each title.
Here’s some sample code, and what I’ve got so far:
Feed XML (slightly abbreviated; three book’s worth)
https://standardebooks.org/feeds/atom/new-releases
Standard Ebooks - Newest Ebooks
The 15 latest Standard Ebooks, most-recently-released first.
https://standardebooks.org/images/logo.png
2025-04-29T17:50:47Z
Standard Ebooks
https://standardebooks.org
https://standardebooks.org/ebooks/anthony-berkeley/the-poisoned-chocolates-case
The Poisoned Chocolates Case
Anthony Berkeley
https://standardebooks.org/ebooks/anthony-berkeley
2025-04-21T03:39:39Z
2025-04-21T09:01:35Z
Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
A club of amateur sleuths attempts to solve a crime whose solution has eluded Scotland Yard.
https://standardebooks.org/ebooks/g-k-chesterton/whats-wrong-with-the-world
What’s Wrong with the World
G. K. Chesterton
https://standardebooks.org/ebooks/g-k-chesterton
2025-04-29T03:11:02Z
2025-04-29T03:11:05Z
Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
Philosopher G. K. Chesterton comments on various social issues of his day, including property, imperialism, feminism, and education.
https://standardebooks.org/ebooks/george-gissing/the-private-papers-of-henry-ryecroft
The Private Papers of Henry Ryecroft
George Gissing
https://standardebooks.org/ebooks/george-gissing
2025-04-29T17:21:20Z
2025-04-29T17:49:20Z
Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
A retired Englishman muses on life and literature in his private diary.
PHP code
". PHP_EOL;
$url="atom-sample.xml";
$xmla = simplexml_load_file($url);
$xml = array();
foreach($xmla->entry as $node) {
$xml[] = $node;
}
foreach($xml as $entry){
$entry->registerXPathNamespace('ns','http://www.w3.org/2005/Atom');
echo ''.$entry->author->name .', '. $entry->title .' | '. $entry->category['term'] .' ';
}
echo "";
?>
Parsed array (output of print_r($xml)
)
Array
(
[0] => SimpleXMLElement Object
(
[id] => https://standardebooks.org/ebooks/anthony-berkeley/the-poisoned-chocolates-case
Get last value of a sub-node in XML parsed by PHP into an array => The Poisoned Chocolates Case
=> SimpleXMLElement Object
(
[name] => Anthony Berkeley
[uri] => https://standardebooks.org/ebooks/anthony-berkeley
)
[published] => 2025-04-21T03:39:39Z
[updated] => 2025-04-21T09:01:35Z
[rights] => Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
[summary] => A club of amateur sleuths attempts to solve a crime whose solution has eluded Scotland Yard.
[category] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => http://purl.org/dc/terms/LCSH
[term] => Detective and mystery stories
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => http://purl.org/dc/terms/LCSH
[term] => London (England) -- Fiction
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => http://purl.org/dc/terms/LCSH
[term] => Sheringham, Roger (Fictitious character) -- Fiction
)
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => http://purl.org/dc/terms/LCSH
[term] => Police -- England -- London -- Fiction
)
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => https://standardebooks.org/vocab/subjects
[term] => Fiction
)
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => https://standardebooks.org/vocab/subjects
[term] => Mystery
)
)
)
)
[1] => SimpleXMLElement Object
(
[id] => https://standardebooks.org/ebooks/g-k-chesterton/whats-wrong-with-the-world
Get last value of a sub-node in XML parsed by PHP into an array => What’s Wrong with the World
=> SimpleXMLElement Object
(
[name] => G. K. Chesterton
[uri] => https://standardebooks.org/ebooks/g-k-chesterton
)
[published] => 2025-04-29T03:11:02Z
[updated] => 2025-04-29T03:11:05Z
[rights] => Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
[summary] => Philosopher G. K. Chesterton comments on various social issues of his day, including property, imperialism, feminism, and education.
[category] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => http://purl.org/dc/terms/LCSH
[term] => Social problems
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => https://standardebooks.org/vocab/subjects
[term] => Nonfiction
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => https://standardebooks.org/vocab/subjects
[term] => Philosophy
)
)
)
)
[2] => SimpleXMLElement Object
(
[id] => https://standardebooks.org/ebooks/george-gissing/the-private-papers-of-henry-ryecroft
Get last value of a sub-node in XML parsed by PHP into an array => The Private Papers of Henry Ryecroft
=> SimpleXMLElement Object
(
[name] => George Gissing
[uri] => https://standardebooks.org/ebooks/george-gissing
)
[published] => 2025-04-29T17:21:20Z
[updated] => 2025-04-29T17:49:20Z
[rights] => Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
[summary] => A retired Englishman muses on life and literature in his private diary.
[category] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => http://purl.org/dc/terms/LCSH
[term] => English fiction -- 19th century
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[scheme] => https://standardebooks.org/vocab/subjects
[term] => Fiction
)
)
)
)
)
Output
-
Anthony Berkeley,
The Poisoned Chocolates Case
| Detective and mystery stories
-
G. K. Chesterton,
What’s Wrong with the World
| Social problems
-
George Gissing,
The Private Papers of Henry Ryecroft
| English fiction -- 19th century
The value I’m after is the last one under [category]
, which is [5]
for the first book, [2]
for the second book, and [1]
for the third book. Above, I’ve used $entry->category['term']
for this “slot”, which always returns the [0]
value—but I want the last one, which is not consistent!
I am aware of the [last()]
construct, but no permutation of my code using [last()]
has worked. Obviously I’m doing something wrong in handling that sub-array with my approach/code. So what works?! Thanks!