* @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license.
*/
require_once '../src/QueryPath/QueryPath.php';
// This is the stub RSS document.
$rss_stub = '
enQueryPath
';
// This is the stub RSS element.
$rss_item_stub = '
Untitled';
// Here are some dummy items. For the same of
// simplicity, we are just using a nested array. Of
// course, this could be a database lookup or whatever.
$items = [
[
'title' => 'Item 1',
'link' => 'http://example.com/item1',
'description' => 'This has embedded HTML',
'comments' => 'http://example.com/item1/comments',
'category' => 'Some Term',
'pubDate' => date('r'),
'guid' => '123456-789',
],
[
'title' => 'Item 2',
'link' => 'http://example.com/item2',
'description' => 'This has embedded HTML',
'comments' => 'http://example.com/item2/comments',
'category' => 'Some Other Term',
'pubDate' => date('r'),
'guid' => '123456-790',
],
];
// The main QueryPath, which holds the channel.
$qp = qp($rss_stub, 'title')
->text('A QueryPath RSS Feed')
->next('link')->text('http://example.com')
->next('description')->text('QueryPath: Find your way.')
->parent();
// For each element in the array above, we create a new
// QueryPath and then populate the XML fragment with data.
foreach ($items as $item) {
// Begin with the stub RSS item, with title currently selected.
$qpi = qp($rss_item_stub, 'title')
// Add a title.
->text($item['title'])
// Add a link. Note that we are giving no args to next() for the
// sake of simplicity.
->next()->text($item['link'])
// Go to next element and add a description. Note that the text()
// call will automatically encode HTML. < will become < and so on.
->next()->text($item['description'])
// Go on down the list...
->next()->text($item['comments'])
->next()->text($item['category'])
->next()->text($item['pubDate'])
->next()->text($item['guid']);
// Now we append it.
$qp->append($qpi->top());
}
// If we were running this on a server, we would need to set the content
// type:
// header('Content-Type: application/rss+xml');
// Write the outpt as XML.
$qp->writeXML();