3.5 A Step Further: Syndicating Content


3.5 A Step Further: Syndicating Content

You have reached your initial goal of publishing your XML documents for consumption by HTML browsers on the Web using AxKit. Even if that were all you ever wanted to do, you still made a clear division between the content you will maintain and the way in which it is presented. Among other benefits, you can now redesign the look and feel of pages sent to the client without touching content documents. Don't worry about clobbering or obscuring essential information just to change the way it renders in a browser. Similarly, using a custom XML grammar for your content means that the documents themselves can unambiguously define the intended roles of the data they contain, rather than the way that data may be represented on the visual medium of an HTML browser. This makes reusing the data for other purposes a lot easier.

To understand the practical benefits of separating content from presentation, suppose that your list of cryptid sightings becomes wildly popular on the Web. People start asking for a way to put links to the newly reported sighting on their own cryptozoology sites. You could tell them to screen-scrape the HTML list. Instead, you decide to be a good information-sharing citizen and make the list available as an RSS syndication feed. To achieve this, the first thing you need is a stylesheet that transforms the list of cryptid sightings to RSS, in addition to the one you already have that transforms the data into HTML.

For those who may not be familiar with it, RSS (RDF Site Summary, Rich Site Summary, or Really Simple Syndication, depending on whom you ask) is a popular XML grammar used for syndicating online content, especially news headlines. Most weblogs use RSS as the means to both publish content and share links with other bloggers, and many weblog tools store their data natively as RSS. (See Example 3-6.) For more information about RSS and some of its more creative uses, see Ben Hammersley's Content Syndication with RSS (O'Reilly).

Example 3-6. cryptidsightings_rss.xsl
 <?xml version="1.0"?> <xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"   xmlns="http://purl.org/rss/1.0/"   version="1.0" > <xsl:variable      name="this_url"      select="'http://myhost.tld/cryptosightings.xml'" /> <xsl:template match="/"> <rdf:RDF>   <channel rdf:about="{$this_url}">     <title>My Cryptozoology Pages- Sightings</title>     <link>       <xsl:value-of select="$this_url"/>     </link>     <description>       The latest sightings of animals that do not officially exist.     </description>     <items>       <rdf:Seq>         <xsl:for-each select="/sightings/sighting">           <rdf:li rdf:resource="{$this_url}#{position( )}"/>         </xsl:for-each>       </rdf:Seq>     </items>   </channel>   <xsl:apply-templates select="/sightings/sighting"/> </rdf:RDF> </xsl:template> <xsl:template match="sighting"> <item rdf:about="{$this_url}#{position( )}">   <link>     <xsl:value-of select="concat($this_url,'#', position( ))"/>   </link>   <title>     <xsl:value-of select="species"/> Sighting - <xsl:value-of select="date"/>   </title>   <description>     <xsl:apply-templates select="description"/>   </description> </item> </xsl:template> <xsl:template match="descriptionp">   <xsl:value-of select="normalize-space(.)" /> </xsl:template> </xsl:stylesheet> 

Before you can see this stylesheet in action, you need to add a couple of configuration directives to the .htaccess file you created earlier:

 <Files cryptid_sightings.xml>   <AxStyleName rss1>     AxAddProcessor text/xsl stylesheets/cryptidsightings_rss.xsl   </AxStyleName>   AxAddPlugin Apache::AxKit::StyleChooser::QueryString </Files> 

The AxStyleName block creates a named style called rss1 . The AxAddProcessor it contains associates that named style with the RSS stylesheet you just created. The AxAddPlugin directive, in this case, tells AxKit to use additional logic to examine the query string sent from the requesting client for a parameter named style . If it finds one, and the value of that parameter matches one of the named styles configured for that URI, it uses the stylesheets contained in that named style to transform the XML source document. Here, this means that a request to http://myhost.tld/cryptid_sightings.xml?style=rss1 returns the list of species sightings processed by your RSS output stylesheet, not the default style you created earlier. (See Figure 3-3.)

Figure 3-3. Cryptozoology site-processing diagram

Example 3-7 shows the result for a request for the cryptid_sightings.xml file as an RSS document.

Example 3-7. Cryptid sightings delivered as an RSS feed
 <?xml version="1.0"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"  xmlns="http://purl.org/rss/1.0/">   <channel rdf:about="http://myhost.tld/cryptosightings.xml">     <title>My Cryptozoology Pages- Sightings</title>     <link>http://myhost.tld/cryptosightings.xml</link>     <description>       The latest sightings of animals that do not officially exist.     </description>     <items>       <rdf:Seq>         <rdf:li rdf:resource="http://myhost.tld/cryptosightings.xml#1"/>         <rdf:li rdf:resource="http://myhost.tld/cryptosightings.xml#2"/>       </rdf:Seq>     </items>   </channel>   <item rdf:about="http://myhost.tld/cryptosightings.xml#1">     <link>http://myhost.tld/cryptosightings.xml#1</link>     <title>Jersey Devil Sighting - Autumn, 1816</title>     <description>         A Jersey Devil was reportedly seen         by Joseph Bonaparte, former King of Spain         and brother of Napoleon, while hunting in         the woods near Bordentown, New Jersey.       </description>   </item>   <item rdf:about="http://myhost.tld/cryptosightings.xml#2">     <link>http://myhost.tld/cryptosightings.xml#2</link>     <title>Snipe Sighting - June, 2002</title>     <description>         The Phelan Phine Snipe Hunters Association         celebrated the opening of this year's Snipe         season. Unfortunately, the entire         photographic record of the event was ruined         during a nasty "keg stand" incident in         the beer tent after the hunt.       </description>   </item> </rdf:RDF> 

Figure 3-4 shows a request to that same URL rendered in the NetNewsWire RSS client.

Figure 3-4. Information rendered as RSS in NetNewsWire

Now your friends in the cryptozoology community can add your list of sightings to their list of daily news feeds by pointing their RSS viewer or other client to http://myhost.tld/cryptid_sightings.xml?style=rss1, while casual web surfers get the default HTML version. Keep in mind, too, that the query string StyleChooser is only one way to dynamically select the preferred style. Using other plug-ins, you could just as easily examine the connecting client's User-Agent header, or another aspect of the request, to get the same effect. Remember, too, that XSLT and XPathScript are only two transformative Language modules that AxKit supports; there are several others, each with its own unique strengths and weaknesses.

As promised , your first example site is a bit silly (Dahuts, indeed), but let's examine exactly what you have done:

  • You used stylesheets to transform data marked up in custom XML grammars into a commonly used delivery format (HTML).

  • You combined XML from different resources (the content documents and the navigation metadata) to meet your application's needs.

  • You mixed standard Apache configuration blocks with AxKit's custom directives to select an alternative style transformation that delivered the same XML content in a different format in response to data received from the requesting client.

Taken together, these points represent the basic foundation of publishing XML documents with AxKit. You can apply the patterns you learned here to most, if not all, of your future XML publishing projects. However, your little cryptids' site offers only a glimpse of the flexibility and power that AxKit offers. The following chapters build on these basic concepts to show how you can use AxKit to create sophisticated, dynamic, and feature-rich sites.

XML Publishing with AxKit
XML Publishing with Axkit
ISBN: 0596002165
EAN: 2147483647
Year: 2003
Pages: 109
Authors: Kip Hampton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net