Forum OpenACS Development: Cookbook: Outputing XML from and ADP template

In reference to Jeff's posting https://openacs.org/forums/message-view?message_id=263944

Here is how I generated an RSS XML document from an ADP. Using the ADP template made it easy to adjust the XML. There are definitely other ways to do this. As far as I know this is the simplest thing that can work with the existing OpenACS toolkit.

Other options include:
-turning Tcl variables into an XML file and applying and XSLT stylesheet to it to output the final result.
- using tDOM to build an XML document in Tcl. tDOM has a more than one way to generate an XML document from Tcl code. One interesting method it supports is creating a Tcl command for each XML tag and building a Tcl script as the document and then executing that code to generate the final XML.

Tcl File

#

ad_page_contract { Serve an RSS file @author Dave Bauer (mailto:dave@thedesignexperience.org) @creation-date 2004-07-19

} { item_id:integer,notnull } -properties { } -validate { } -errors { }

set items [ctk_feed::get_items -item_id $item_id] array set channel [ctk_feed::get_feed_channel -item_id $item_id]

ns_set put [ns_conn outputheaders] "Content-Type" "text/xml" ad_return_template

ctk_feed::get_items sets up a multirow datasource

ADP File

<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
  xmlns:dc="http://purl.org/dc/elements/1.1/";
  xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/";
  xmlns="http://purl.org/rss/1.0/";
  xmlns:img="http://igargoyle.com/rss/1.0/modules/img/";
  xmlns:dcterms="http://purl.org/dc/terms/">
  <channel rdf:about="@channel.link@">
    <title>@channel.title@</title>
    <link>@channel.link@</link>
    <description>
      @channel.description@
    </description>
    <dc:publisher xml:lang="@channel.locale@">@channel.publisher@</dc:publisher>
    <dc:creator>@channel.creation_user@ @channel.dc_creator@</dc:creator>
    <dc:rights>@channel.dc_rights@</dc:rights>
    <dc:date>@channel.dc_date@</dc:date>
    <if @channel.image_url@ not nil><image
    rdf:resource="@channel.image_url@" /></if>
  <items><multiple name="rss_items"><rdf:Seq><rdf:li resource="@rss_items.url@" /></rdf:Seq></multiple></items>

  </channel>
    <if @channel.image_url@ not nil>
  <image rdf:about="channel.image_url">
    <title>@channel.image_title@</title>
    <url>@channel.image_url@</url>
    <link>@channel.image_link@</link>
  </image>
    </if>
  <multiple name="rss_items">@rss_items.rss_xml_frag;noquote@</multiple>
</rdf:RDF>

This ADP cheats a little since hte XML fragment for each item is pregenerated. This is the main reason we use an ADP instead of using the tDOM API to generate XML. At the time it was now known if you could build and XML document out of fragments using tDOM.

There is also an index.vuh that takes the feed_id and internally redirects to the template.

I think this is a pretty easy way to get OpenACS started outputting XML for web applications.