Sign In/My Account | View Cart  
advertisement


Listen Print Discuss
What are Syndication Feeds

Editor's note: welcome to the first installment of a new regular column on XML.com, Practical XQuery. Ivelin Ivanov and Per Bothner will be bringing us tips on the use of the XQuery language, as well as self-contained example applications.

The goal of this article is to demonstrate the use of XQuery to accomplish a routine, yet interesting task; in particular, to render an HTML page that merges RSS news feeds from two different weblogs. RSS has earned its popularity by allowing people to easily share news among and between web sites. And for almost any programming language used on the Web, there is a good selection of libraries for consuming RSS.

Readers will benefit from a basic knowledge of the XQuery language. Per Bothner has written an informal introduction to XQuery.

Even though XQuery started as an XML-based version of SQL, the language has a very broad application on the Web. In what follows, I will show that XQuery allows RSS feeds to be consumed and processed easily. In fact, we will see that it isn't necessary to use a specialized library. We will utilize only functions of the core language.

Jump Right In

If we were using another language we would have probably started with a breakdown of the components of the script and their individual responsibilities. But the XQuery script is so brief that there is not much to break apart.

I will let the code speak for itself; if you still think you need further analysis, stick around and read the text further below.

Listing 1: XQuery Script -- RSS Feed Merge


define function row ($link, $title)
{
  <div>
    RSS item <b> {$title}</b> is located at <b>
{$link}</b>
  </div>
}

define function filter-rss ($url)
{
 for $b in document($url)/rss/channel/item
 return row($b/link/text(), $b/title/text())
}

<html>
  <body>
  <i>Remote RSS Feed Demo, written in XQuery. 
    Compiled and Run by The Open Source QEXO.org engine.</i>
  <hr/>

 {filter-rss("http://www.javablogs.com/ViewDaysBlogs.jspa?view=rss")}
 {filter-rss("http://radio.weblogs.com/0109827/rss.xml")}

  </body>
</html>

If you want to see the result of this script immediately, visit http://www.cocoonhive.org/xquery/xqueryform.html. It will look similar to the output shown in Listing 2.

Listing 2: XQuery Script Output -- RSS Feed Merge

<html>
  <body>
  <i>Remote RSS Feed Demo, written in XQuery. 
    Compiled and Run by The Open Source QEXO.org engine.</i>
  <hr />

  <div>
    RSS item <b> EJB Design Patterns</b> is located at 
    <b> http://www.javablogs.com/Jump.jspa?id=20692</b>
  </div>
  <div>
    RSS item <b> There is a first for everything</b> is located at 
    <b> http://www.javablogs.com/Jump.jspa?id=20667</b>
  </div>
  <div>
    RSS item <b> </b> is located at 
    <b> http://radio.weblogs.com/0109827/2002/12/11.html#a1219</b>
  </div>
  <div>
    RSS item <b> Programmers are Speshal</b> is located at 
    <b> http://radio.weblogs.com/0109827/2002/12/11.html#a1218</b>
  </div>
  </body>
</html>

Let's examine how the script works. It begins with the definition of two functions. The main body starts after the function definitions.

<html>
  <body>
  <i>Remote RSS Feed Demo, written in XQuery. 
    Compiled and Run by The Open Source QEXO.org engine.</i>
  <hr/>

 {filter-rss("http://www.javablogs.com/ViewDaysBlogs.jspa?view=rss")}
 {filter-rss("http://radio.weblogs.com/0109827/rss.xml")}

  </body>
</html>

As you can see, it is plain html, except for the two lines which enclose calls to the function filter-rss() in curly braces. The curly braces are indication that a XQuery expression needs to be evaluated.

The function filter-rss()is defined by

define function filter-rss ($url) 
{
 for $i in document($url)/rss/channel/item
   return row($i/link/text(), $i/title/text())
}

It loops over all XML nodes matched by the XPath expression "/rss/channel/item", which is applied to the XML document returned by the built-in function document(). This function itself is invoked with the $url argument passed to filter-rss(). The value of this argument is either http://www.javablogs.com/ViewDaysBlogs.jspa?view=rss or http://radio.weblogs.com/0109827/rss.xml.

The content of the XML documents located at these two URLs looks similar to:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
 <rss version="0.91">
 <channel>
  <title>java.blogs Day's Entries</title> 
  <link>http://www.javablogs.com/</link> 
  <description>Blog entries on 14/2/2003</description> 
  <language>en-us</language> 
 <item>
  <title>One thing not to do before a presentation</title> 
  <link>http://www.javablogs.com/Jump.jspa?id=20740</link> 
  <description>Just a helpful hint:...</description> 
 </item>
 <item>
  <title>Reversible</title> 
  <link>http://www.javablogs.com/Jump.jspa?id=20739</link> 
  <description>Links back to pages that link to it. List of referrers 
                and trackbacks. ...</description> 
  </item>
 </channel>
</rss>

As you might expect, the for loop assigns in turn to the variable $i each of the <item> elements of the target document. For each value of $i, the function returns the result of invoking the other custom function in this script, that is, row(), passing the textual values of the link and title sub-elements of item. The latter function is very transparent. It simply returns an HTML <div> element, which contains the textual values of its arguments.

What Are Syndication Feeds

Essential Reading

What Are Syndication Feeds
By Shelley Powers

Table of Contents

Syndication feeds have become a standard tool on the Web. But when you enter the world of syndicated content, you're often faced with the question of what is the "proper" way to do syndication. This edoc, which covers Atom and the two flavors of RSS--2.0 and 1.0--succinctly explains what a syndication feed is, then gets down to the nitty-gritty of what makes up a feed, how you can find and subscribe to them, and which feed will work best for you.


Read Online--Safari
Search this book on Safari:
 

Code Fragments only

Functional Benefits

I am not aware of another language endorsed by a standards body that can do the same thing more briefly and intuitively. The fact that XQuery recognizes XML nodes as first-class language constructs, combined with the familiar C-like language syntax, makes it an attractive tool for the problems it was built to solve. It must be noted that although it has a for loop structure, XQuery is a purely functional language. In short, this means that XQuery functions always return the same values given the same arguments. This is an important property of the language, which allows advanced compilation optimizations not possible for C or Java.

In the past decade, functional language compilers have shown significant advantages over imperative language compilers. Their unconventional syntax and the inertia of imperative languages keep them under the radar of mainstream development. However, the XQuery team seems to recognize these weaknesses and is making an attempt to overcome them.


Comment on this articleShare your comments or questions on this article in our forum.
(* You must be a
member of XML.com to use this feature.)
Comment on this Article


Titles Only Full Threads Newest First
  • A. Locksmith Los Angeles 877-364-5264 Locksmith Security Services, Los Angeles Safe & Vault Sales and Installation
    2008-12-21 12:46:34 services123 [Reply]

    A. Locksmith Los Angeles 877-364-5264 Locksmith Security Services, Los Angeles Locks, Safe & Vault Sales and Installation.
    For All Your Home & Business Needs Doors installation wood, metal, Kalamein, and fire rated wood doors for both heavyweight commercial and standard residential needs. Door Closers Panic Bars Fire Exit Devices Floor or Head Check Bolts Door Frames Hinges Buzzer Systems Card Access Elevator Doors magnetic door locks, electric strikes, CCTV install Dvr Video Camera master key systems, CCTV and video surveillance, Alarm Systems, Keyless Entry Systems and Access Control, Iron Works specializes in custom exterior and interior wrought iron design. We design and hand forge wrought iron entrance gates, driveway gates Child guard gates A/C cage gates Elevator doors Backyard gates Fire exit gates Custom design gates Storefront and Commercial Rolling Access Control Automotive Lockout Tools & Kits CodeLock Pushbutton Locks Cylinders Deadbolt Style Gate Locks DigiLock Digital Locks Door Closers Door Locks Electromagnetic Locks Electronic Hardware Exit Hardware Four Drawer Key File Gate Locks & Weldable Gate Box Government Locks High Security Locks Hospital Locks IEI Digital Pushbutton Locks Key Blanks Key Cabinets & Key Storage Key Cutters Key Tags and Key Rings, Key Towers Knob and Lever Gate Locks Locker Locks Locker, Cabinet & Furn. Locks Locksmith Tools and Equipment Manufacture Line Items Misc Hardware Misc Locking Devices NEW PRODUCTS Omni Lock Pepper Spray Picks and Pick Sets Pins and Pinning Kits Pushbutton Gate Locks Pushbutton Locks Re-Keying Safes Schlage Simplex Parts Simplex Pushbutton Lock Simplex, CodeLock, Trilogy Specials Training and Education Trilogy Digital locks Von Duprin Window Locks Patio Door Locks Padlocks.
    Locksmith in Los Angeles (877) 364-5264

  • Roadside Assistance service Los Angeles 1-818-386-1022
    2008-09-24 18:42:03 0 [Reply]

    Roadside Assistance service exists for your peace of mind.
    it's available 24/7 — just call 1-818-386-1022 at any time, and a representative will dispatch a roadside service professional to help you with the following:
    Flat Tire Change — No need to panic if you have a flat. A roadside professional can put the spare tire on your vehicle at the scene.
    Battery Jump-Start — Call us if your battery fails. If possible, it will be jump-started at the scene to help get you on your way.
    Emergency Fuel and Fluid Delivery — If you run out of gas or your vehicle overheats, get the required fuel or fluids you need delivered directly to your vehicle. You only pay for the cost of the fuel or fluids. The delivery and service is extra.
    Locksmith Service — If your keys are lost, stolen or accidentally locked inside your vehicle, we will dispatch a qualified service provider. You only pay for the cost of the locked out and service charge to send you professional locksmith.




  • Error in program listing
    2003-04-14 08:14:30 Michael Washington [Reply]

    In the code listing for the function filter-rss you used the variable $b in the first listing but you used $i in the second.

  • Here's an XSLT solution
    2003-04-13 17:39:14 Steve Cassidy [Reply]

    Just as brief, perhaps simpler, needs a null XML document to bootstrap (containing just <rsslist/>)



    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">


    <xsl:output method="html"/>


    <xsl:template match="/rsslist">
    <html>
    <body>
    <xsl:apply-templates select='document("http://www.javablogs.com/ViewDaysBlogs.jspa?view=rss")'/>
    <xsl:apply-templates select='document("http://radio.weblogs.com/0109827/rss.xml")'/>
    </body>
    </html>
    </xsl:template>



    <xsl:template match="channel/item">
    <div>
    RSS item <xsl:value-of select="title"/> is located at
    <xsl:value-of select="link"/>

    </div>
    </xsl:template>


    </xsl:stylesheet>

  • XQuery and XSLT
    2003-04-12 08:17:13 Michael Champion [Reply]

    As best I understand it, and for situations that don't involve XQuery's deeper support for XML Schema data types, XQuery and XSLT are more or less equivalent in functionality. I believe that all the use cases in the XQuery specification can be implemented with either.


    So why are both needed? Not everyone would agree that they are, and both are (AFAIK) Turing-complete programming languages. The best explanation I can offer is that each is optimized for the needs of different audiences. XSLT tends to be somewhat easier for document-oriented applications, XQuery somewhat easier for data-oriented applications, especially by those coming from a SQL background and those who need a "native XML programming language" rather than a "template-driven XML transformation" tool.

  • If so, why we do need XQuery?
    2003-04-12 02:13:07 Christian Schmidt-Guetter [Reply]

    Why we do need XQuery if it does practically the same as XSLT?
    (But whithout pure XML syntax and therefore less
    *standard compliant*.)


  • Not the happiest choice of example
    2003-04-10 13:54:45 Lars Marius Garshol [Reply]

    This is why RSS shouldn't be XML: with RDF and topic maps merging RSS feeds is just a single method call. You don't have to do *anything*. The models themselves provide the functionality for it.