
Processing RSS
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.
![]() |
Essential Reading What Are Syndication Feeds
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: |
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.
Share 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 | Titles Only | 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
- carpet cleaning services Los Angeles Steam Cleaning, Dry Cleaning, Fabric Lounge Suite Cleaning, Leather Lounge Suite Cleaning, Tile & Grout Cleaning, Mattress Cleaning, Wet Carpet / Water Damage Restoration for: offices, homes, restaurants, clubs a
2008-10-19 10:54:45 orellytos [Reply]
We offer carpet cleaning services such as; Steam Cleaning, Dry Cleaning, Fabric Lounge Suite Cleaning, Leather Lounge Suite Cleaning, Tile & Grout Cleaning, Mattress Cleaning, Wet Carpet / Water Damage Restoration for: offices, homes, restaurants, clubs and hotels
Phone 1-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.
- XQuery and XSLT
2003-10-19 03:33:26 Dimitre Novatchev [Reply]
> One way to think of them: XSLT is essentially a > special-purpose scripting language. It is
> designed to be interpreted, and there is no
> static type-checking or optimization.
Speking about XSLT 2.0 almost everything in the above statement is quite wrong:
- there *is* static type checking.
- there is optimization performed even in most
XSLT 1.0 processors.
> XQuery aspires to be a real general-purpose
> programming language, albeit one with syntax
> and data types optimized for XML applications.
> It supports (at least optionally) type-
> checking, optimization, and compilation.
So you imply that XSLT is not so real general-purpose programming language than XQuery?
Of course this is not true -- XSLT 2.0 has all major features that XQuery has.
What XQuery doesn't offer and what happens to be one of the most powerful features of XSLT is the mechanism to apply the best-matching template out of many.
There are great consequences from this -- e.g. it is possible to implement higher order functions and functional programming in XSLT (see the FXSL library), while this does not seem possible to be done with XQuery.
Dimitre Novatchev
- XQuery and XSLT
2003-04-18 21:40:44 Per Bothner [Reply]
> So why are both needed?
One way to think of them: XSLT is essentially a special-purpose scripting language. It is designed to be interpreted, and there is no static type-checking or optimization. XQuery aspires to be a real general-purpose programming language, albeit one with syntax and data types optimized for XML applications. It supports (at least optionally) type-checking, optimization, and compilation.
This is of course a gray boundary. It is possible to compile and statically analyze at least a subset of XSLT, and it is possible to do quick-and-dirty scripting in XQuery. But your point about "deeper support for XML Schema data types" goes beyond Schema: Having a statically analyzable syntax and type system allows you to catch type errors as compile-time errors rather than run-time errors which I think is very valuable.
- XQuery and XSLT
2003-04-19 13:13:25 Oleg Tkachenko [Reply]
Come on, XSLT is not a "special-purpose scripting language", it's turing complete functional language, look at FXSL library as a prominent example.
And XSLT is designed to allow compiation, that's why xsl:include cannot be dynamic and there is no dynamic XPath evaluatuion even in XSLT2. Moreover, *all* XSLT processors I'm aware do compile XSLT stylesheets and do optimize from //* stuff to tail recursion, that's all pretty possible even in XSLT1.0.
XQuery and XSLT2 really look extremelly similar, except for syntax, but does syntax matter?
"XQuery is XSLT with no templates" (c)Jeni Tennison probably.
- XQuery and XSLT
2003-04-21 19:24:20 Per Bothner [Reply]
> Come on, XSLT is not a "special-purpose scripting language", it's turing complete functional language,
What makes you think there is any conflict between these two statements? It is very difficult *not* to be Turing complete.
> And XSLT is designed to allow compiation, that's why xsl:include cannot be dynamic and there is no dynamic XPath evaluatuion even in XSLT2.
People often talk about "compiled languages" vs "interpreted languages" which is of course nonsense: You can only talk about compiled vs
interrpeted implementations. However, there is a diffence is the "mid-set" and culture of programming languages, and what kind of implementation they facilitate - and what kind of implementation people expect. Many "interpreted" languages" do at least some minimal level of compilation. One important practical difference with "compiled languages" is that people expect a compilation step, and to get static errors at compile time. But this is more a matter of culture than a hard-and-fast definition, as people can run valiators and error-checkers on a "script" as well as on a "program".
> Moreover, *all* XSLT processors I'm aware do compile XSLT stylesheets and do optimize from //* stuff to tail recursion, that's all pretty possible even in XSLT1.0.
I know very little about this. I would expect most implementations to "compile" to some internal form, like a special-purpose bytecode. Is it common to compile to a general-purpose bytecode (such as Java or .net CIL), or native code? (I've actually done a little work compiling XSLT to Java bytecodes, though its too incomplete to be very useful.)
> XQuery and XSLT2 really look extremelly similar, except for syntax, but does syntax matter?
Syntax matters a lot. If you don't think syntax matters, then you shouldn't believe XML matters, since XML is little more than a standard syntax for data. Some syntaxes are more convenient to write, easier to read, and easier to detect errors in than others. Programmer productivity varies inveresely with how many tokens are needed to express common tasks, and that is partly a matter of syntax. XSLT except for XPath is of course very verbose. Plus there is the inconsistency between the XPath syntax/world and the XML syntax/world; XQuery doesn't have this divide.
> "XQuery is XSLT with no templates" (c)Jeni Tennison probably.
Templates are basically just functions, which XQuery has. What XQuery doesn't have is the template *engine* - i.e. apply-templates.
- XQuery and XSLT
- XQuery and XSLT
- XQuery and XSLT
- 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*.)
- If so, why we do need XQuery?
2003-04-18 21:58:42 Per Bothner [Reply]
> (But [XQuery] is without pure XML syntax and therefore less *standard compliant*.)
First, I don't understand your implication that there is any connection between XML syntax and "standards compliance".
Secondly, you seem to be suggesting that a programming language is inferior if it doesn't use XML syntax, which is a rather strange claim. I would rather claim that that XML syntax is a terrible syntax to use for programming languages. XSLT is only usable because it uses the very non-XML-syntax of XPath.
Third, XSLT is actually two languages nested within each other, while XQuery is a compatible extension of one of them. So XQuery is quite a conservative language, and it is actually simpler that XSLT.
- If so, why we do need XQuery?
- 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.
- Not the happiest choice of example
2004-11-02 02:09:05 niq [Reply]
Come on, XSLT is not a "special-purpose scripting language", it's turing complete functional language, look at FXSL library as a prominent example.
And XSLT is designed to allow compiation, that's why xsl:include cannot be dynamic and there is no dynamic XPath evaluatuion even in XSLT2. Moreover, *all* XSLT processors I'm aware do compile XSLT stylesheets and do optimize from //* stuff to tail recursion, that's all pretty possible even in XSLT1.0.
XQuery and XSLT2 really look extremelly similar, except for syntax, but does syntax matter?
- Not the happiest choice of example

