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)
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="https://standardebooks.org/feeds/atom/style" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<id>https://standardebooks.org/feeds/atom/new-releases</id>
<link href="https://standardebooks.org/feeds/atom/new-releases" rel="self" type="application/atom+xml"/>
<title>Standard Ebooks - Newest Ebooks</title>
<subtitle>The 15 latest Standard Ebooks, most-recently-released first.</subtitle>
<icon>https://standardebooks.org/images/logo.png</icon>
<updated>2025-04-29T17:50:47Z</updated>
<author>
<name>Standard Ebooks</name>
<uri>https://standardebooks.org</uri>
</author>
<link href="https://standardebooks.org/opensearch" rel="search" title="Standard Ebooks" type="application/opensearchdescription+xml"/>
<entry>
<id>https://standardebooks.org/ebooks/anthony-berkeley/the-poisoned-chocolates-case</id>
<title>The Poisoned Chocolates Case</title>
<author>
<name>Anthony Berkeley</name>
<uri>https://standardebooks.org/ebooks/anthony-berkeley</uri>
</author>
<published>2025-04-21T03:39:39Z</published>
<updated>2025-04-21T09:01:35Z</updated>
<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.</rights>
<summary type="text">A club of amateur sleuths attempts to solve a crime whose solution has eluded Scotland Yard.</summary>
<category scheme="http://purl.org/dc/terms/LCSH" term="Detective and mystery stories"/>
<category scheme="http://purl.org/dc/terms/LCSH" term="London (England) -- Fiction"/>
<category scheme="http://purl.org/dc/terms/LCSH" term="Sheringham, Roger (Fictitious character) -- Fiction"/>
<category scheme="http://purl.org/dc/terms/LCSH" term="Police -- England -- London -- Fiction"/>
<category scheme="https://standardebooks.org/vocab/subjects" term="Fiction"/>
<category scheme="https://standardebooks.org/vocab/subjects" term="Mystery"/>
</entry>
<entry>
<id>https://standardebooks.org/ebooks/g-k-chesterton/whats-wrong-with-the-world</id>
<title>What’s Wrong with the World</title>
<author>
<name>G. K. Chesterton</name>
<uri>https://standardebooks.org/ebooks/g-k-chesterton</uri>
</author>
<published>2025-04-29T03:11:02Z</published>
<updated>2025-04-29T03:11:05Z</updated>
<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.</rights>
<summary type="text">Philosopher G. K. Chesterton comments on various social issues of his day, including property, imperialism, feminism, and education.</summary>
<category scheme="http://purl.org/dc/terms/LCSH" term="Social problems"/>
<category scheme="https://standardebooks.org/vocab/subjects" term="Nonfiction"/>
<category scheme="https://standardebooks.org/vocab/subjects" term="Philosophy"/>
</entry>
<entry>
<id>https://standardebooks.org/ebooks/george-gissing/the-private-papers-of-henry-ryecroft</id>
<title>The Private Papers of Henry Ryecroft</title>
<author>
<name>George Gissing</name>
<uri>https://standardebooks.org/ebooks/george-gissing</uri>
</author>
<published>2025-04-29T17:21:20Z</published>
<updated>2025-04-29T17:49:20Z</updated>
<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.</rights>
<summary type="text">A retired Englishman muses on life and literature in his private diary.</summary>
<category scheme="http://purl.org/dc/terms/LCSH" term="English fiction -- 19th century"/>
<category scheme="https://standardebooks.org/vocab/subjects" term="Fiction"/>
</entry>
</feed>
PHP code
<?php
echo "<ol>". 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 '<li>'.$entry->author->name .', <i>'. $entry->title .'</i> | '. $entry->category['term'] .'</li>';
}
echo "</ol>";
?>
Parsed array (output of print_r($xml)
)
Array
(
[0] => SimpleXMLElement Object
(
[id] => https://standardebooks.org/ebooks/anthony-berkeley/the-poisoned-chocolates-case
[title] => The Poisoned Chocolates Case
[author] => 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
[title] => What’s Wrong with the World
[author] => 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
[title] => The Private Papers of Henry Ryecroft
[author] => 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
<ol>
<li>
Anthony Berkeley,
<i>The Poisoned Chocolates Case</i>
| Detective and mystery stories
</li>
<li>
G. K. Chesterton,
<i>What’s Wrong with the World</i>
| Social problems
</li>
<li>
George Gissing,
<i>The Private Papers of Henry Ryecroft</i>
| English fiction -- 19th century
</li>
</ol>
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!
This is a possible solution:
$xmla = simplexml_load_file($url);
foreach($xmla->entry as $entry) {
$categoryTerm = $entry->category[$entry->category->count() - 1]['term'] ?? '';
echo <<<HTML
<li>
{$entry->author->name}, <i> $entry->title </i> | $categoryTerm
</li>
HTML;
}