Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6042427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:47:21+00:00 2026-05-23T06:47:21+00:00

the code below is tested and working, it prints the contents of a feed

  • 0

the code below is tested and working, it prints the contents of a feed that has this structure.

<rss>
    <channel>
        <item>
            <pubDate/>
            <title/>
            <description/>
            <link/>
            <author/>
        </item>
    </channel>
</rss>

What I didn’t manage to succesfully do is to print feeds that follow this structure below (the difference is on <feed><entry><published> ) even though I changed the xpath to /feed//entry.
you can see the structure on the page source.

<feed>
    <entry>
        <published/>
        <title/>
        <description/>
        <link/>
        <author/>
    </entry>
</feed>

I have to say that the code sorts all item based on its pubDate. In the second structure feed I guess it should sort all entry based on its published.

I probably make a mistake on the xPath I can’t find. However, if at the end of this I manage to print that feed right, how can I modify the code to handle different structures all at once ?

Is there any service that allow me to create and host my own feeds based on those feeds, so I will have the same structure to all? I hope I made my self clear… Thank you.

<?php

$feeds = array();

// Get all feed entries
$entries = array();
foreach ($feeds as $feed) {
    $xml = simplexml_load_file($feed);
    $entries = array_merge($entries, $xml->xpath(''));
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:47:22+00:00Added an answer on May 23, 2026 at 6:47 am

    The main contribution of this answer is a solution (at the end) that can be used with infinite number of formats, just specifying all “entry” alternative names in the external (global) parameter $postElements and all “published-date” alternative names in the external (global) parameter $pub-dateElements.

    Besides this, here is how to specify an XPath expression that selects all /rss//item and all /feed//entry elements.

    In the simple case of just two possible document formats this (as proposed by @Josh Davis) Xpath expression correctly works:

    /rss//item  |   /feed//entry
    

    A more general XPath expression allows the selection of the wanted elements from a set of unlimited number of document formats:

    /*[contains($topElements, concat('|',name(),'|'))]
        //*[contains($postElements, concat('|',name(),'|'))]
    

    where the variable $topElements should be substituted by a pipe-delimited string of all possible names for a top element, and $postElements should be substituted by a pipe-delimited string of all possible names for a “entry” element. We also allow the “entry” elements to be at different depths in the different document formats.

    In particular, for this concrete case the XPath expression will be;

    /*[contains('|feed|rss|', concat('|',name(),'|'))]
        //*[contains('|item|entry|', concat('|',name(),'|'))]
    

    The rest of this post shows how the complete wanted processing can be done entirely in XSLT — easily and with elegance.


    I. A gentle introduction

    Such processing is easy and simple with XSLT:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/">
      <myFeed>
       <xsl:apply-templates/>
      </myFeed>
     </xsl:template>
    
     <xsl:template match="channel|feed">
      <xsl:apply-templates select="*">
       <xsl:sort select="pubDate|published" order="descending"/>
      </xsl:apply-templates>
     </xsl:template>
    
     <xsl:template match="item|entry">
      <post>
        <xsl:apply-templates mode="identity"/>
      </post>
     </xsl:template>
    
     <xsl:template match="pubDate|published" mode="identity">
      <publicationDate>
       <xsl:apply-templates/>
      </publicationDate>
     </xsl:template>
    
      <xsl:template match="node()|@*" mode="identity">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*" mode="identity"/>
      </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied to this XML document (in format 1):

    <rss>
        <channel>
            <item>
                <pubDate>2011-06-05</pubDate>
                <title>Title1</title>
                <description>Description1</description>
                <link>Link1</link>
                <author>Author1</author>
            </item>
            <item>
                <pubDate>2011-06-06</pubDate>
                <title>Title2</title>
                <description>Description2</description>
                <link>Link2</link>
                <author>Author2</author>
            </item>
            <item>
                <pubDate>2011-06-07</pubDate>
                <title>Title3</title>
                <description>Description3</description>
                <link>Link3</link>
                <author>Author3</author>
            </item>
        </channel>
    </rss>
    

    and when it is applied on this equivalent document (in format 2):

    <feed>
            <entry>
                <published>2011-06-05</published>
                <title>Title1</title>
                <description>Description1</description>
                <link>Link1</link>
                <author>Author1</author>
            </entry>
            <entry>
                <published>2011-06-06</published>
                <title>Title2</title>
                <description>Description2</description>
                <link>Link2</link>
                <author>Author2</author>
            </entry>
            <entry>
                <published>2011-06-07</published>
                <title>Title3</title>
                <description>Description3</description>
                <link>Link3</link>
                <author>Author3</author>
            </entry>
    </feed>
    

    in both cases the same wanted, correct result is produced:

    <myFeed>
       <post>
          <publicationDate>2011-06-07</publicationDate>
          <title>Title3</title>
          <description>Description3</description>
          <link>Link3</link>
          <author>Author3</author>
       </post>
       <post>
          <publicationDate>2011-06-06</publicationDate>
          <title>Title2</title>
          <description>Description2</description>
          <link>Link2</link>
          <author>Author2</author>
       </post>
       <post>
          <publicationDate>2011-06-05</publicationDate>
          <title>Title1</title>
          <description>Description1</description>
          <link>Link1</link>
          <author>Author1</author>
       </post>
    </myFeed>
    

    II. The full solution

    This can be generalized to a parameterized solution:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="postElements" select=
     "'|entry|item|'"/>
     <xsl:param name="pub-dateElements" select=
      "'|published|pubDate|'"/>
    
      <xsl:template match="node()|@*" name="identity">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*" mode="identity"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/">
      <myFeed>
       <xsl:apply-templates select=
       "//*[contains($postElements, concat('|',name(),'|'))]">
        <xsl:sort order="descending" select=
         "*[contains($pub-dateElements, concat('|',name(),'|'))]"/>
       </xsl:apply-templates>
      </myFeed>
     </xsl:template>
    
     <xsl:template match="*">
      <xsl:choose>
       <xsl:when test=
        "contains($postElements, concat('|',name(),'|'))">
        <post>
          <xsl:apply-templates/>
        </post>
       </xsl:when>
       <xsl:when test=
       "contains($pub-dateElements, concat('|',name(),'|'))">
        <publicationDate>
         <xsl:apply-templates/>
        </publicationDate>
       </xsl:when>
       <xsl:otherwise>
        <xsl:call-template name="identity"/>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
    
    </xsl:stylesheet>
    

    This transformation can be used with infinite number of formats, just specifying all “entry” alternative names in the external (global) parameter $postElements and all “published-date” alternative names in the external (global) parameter $pub-dateElements.

    Anyone can try this transformation to verify that when applied on the two XML documents above it again produces the same, wanted and correct result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is the link to the code that has the two RAdEditors I am
Code below is working well as long as I have class ClassSameAssembly in same
The code below working for only first div. I can't display other divs when
The code below removes www., etc. from the beginning of websites that are entered
I wrote some code (about 100 lines) that is working fine on version 5.12.1.
I'm working on a piece of code that GETs a URL and parses the
Here is whats happening (Code Block Below): I am working on a simple drop-down
Below is a snippet of PHP that I need to get working, I need
I have tested alot. Code is working fine in FIREFOX 5 but same code
I am working on a program that includes a selection of code which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.