laminas / laminas-feed

Consume and generate Atom and RSS feeds, and interact with Pubsubhubbub.
https://docs.laminas.dev/laminas-feed/
BSD 3-Clause "New" or "Revised" License
138 stars 25 forks source link

Feature Request: Add XSLT support #1

Open weierophinney opened 4 years ago

weierophinney commented 4 years ago

It would be nice to style generated feeds with a xsl:stylesheet. See the XSLT entry on Wikipedia for more general information about this.

I was thinking about adding a header to reference the stylesheet, e.g.

<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="podcast.xsl"?>

taken from an ORF podcast feed.


Originally posted by @podcasthosting at https://github.com/zendframework/zend-feed/issues/107

weierophinney commented 4 years ago

An XSLT transformation is usually best applied to the XML feed directly, and not through a third party component or language. XSLT is Turing complete, and also a very good FP language, so I'd suggest digging that way first (by piping the XML directly through CLI XML tools).


Originally posted by @Ocramius at https://github.com/zendframework/zend-feed/issues/107#issuecomment-508796131

weierophinney commented 4 years ago

@Ocramius If I understand you correctly than what you are suggesting is not what I want to achieve. I want to make a RSS feed look good in the browser. It should be the feed generated through zend-feed itself without piping it through any extra tools.


Originally posted by @podcasthosting at https://github.com/zendframework/zend-feed/issues/107#issuecomment-508803541

weierophinney commented 4 years ago

I'd rather say that you take the RSS feed (input or output) and pass it to XSLT directly: XSLT is good and mature, use it :+1:


Originally posted by @Ocramius at https://github.com/zendframework/zend-feed/issues/107#issuecomment-508828919

froschdesign commented 4 years ago

taken from an ORF podcast feed.

Example: https://files.orf.at/podcast/oe1/oe1_imoe1journalzugast.xml

froschdesign commented 3 years ago

@podcasthosting Please try the following example: (based on "Writer: Getting Started")

// Create feed
$feed = new Laminas\Feed\Writer\Feed;
$feed->setTitle("Paddy's Blog");
$feed->setDescription("Paddy's Blog");
$feed->setLink('http://www.example.com');

// Create entry
$entry = $feed->createEntry();
$entry->setTitle('All Your Base Are Belong To Us');
$entry->setLink('http://www.example.com/all-your-base-are-belong-to-us');
$entry->addAuthor([
    'name'  => 'Paddy',
    'email' => 'paddy@example.com',
    'uri'   => 'http://www.example.com',
]);
$entry->setDateModified(time());
$entry->setDateCreated(time());
$entry->setDescription('Exposing the difficulty of porting games to English.');
$entry->setContent(
    'I am not writing the article. The example is long enough as is ;).'
);
$feed->addEntry($entry);

// Render feed (creates DOMDocument object)
$writer = new Laminas\Feed\Writer\Renderer\Feed\Rss($feed);
$writer->render();

// Add processing instruction
$writer->getElement()->parentNode->insertBefore(
    $writer->getDomDocument()->createProcessingInstruction(
        'xml-stylesheet',
        'title="Demo" type="text/xsl" href="https://files.orf.at/podcast/oe1/podcast.xsl"'
    ),
    $writer->getElement()
);

// Ouput XML
echo $writer->saveXml();

Ouput:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet title="Demo" type="text/xsl" href="https://files.orf.at/podcast/oe1/podcast.xsl"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>Paddy's Blog</title>
        <description>Paddy's Blog</description>
        <generator>Laminas_Feed_Writer 2 (https://getlaminas.org)</generator>
        <link>http://www.example.com</link>
        <item>
            <title>All Your Base Are Belong To Us</title>
            <description><![CDATA[Exposing the difficulty of porting games to English.]]></description>
            <pubDate>Thu, 24 Sep 2020 06:43:50 +0000</pubDate>
            <link>http://www.example.com/all-your-base-are-belong-to-us</link>
            <guid>http://www.example.com/all-your-base-are-belong-to-us</guid>
            <author>paddy@example.com (Paddy)</author>
            <dc:creator>Paddy</dc:creator>
            <content:encoded><![CDATA[I am not writing the article. The example is long enough as is ;).]]></content:encoded>
            <slash:comments>0</slash:comments>
        </item>
    </channel>
</rss>