From: Wolfgang M. <me...@if...> - 2003-03-31 12:13:32
|
Another new feature which needs some explanation: to have a simple way to query the database from Cocoon, I wrote an=20 XMLDBTransformer class, which has been inspired by other transformers shi= pped=20 with Cocoon. The transformer is meant as an easy alternative to XSP pages= =2E=20 XSP pages have several disadvantages. For example, they need to be compil= ed,=20 which makes them sensible to changing environments. The transformer is much simpler: it just understands four tags. Basically= , the=20 transformer watches out for those known tags, takes some action, and repl= aces=20 the tags with the results of these actions. Here's a code fragment: <xmldb:collection xmlns:xmldb=3D"http://exist-db.org/transformer/1.0" uri=3D"xmldb:exist:///db"> <!-- iterate through all rdf:Description elements containing the term "computer" --> <xmldb:for-each query=3D"//rdf:Description[dc:title &=3D 'compute= r']" from=3D"0" to=3D"9" sort-by=3D"/dc:title"> <!-- output a book element for each entry --> <book> <!-- extract the title. There's only one title, so we use select-node --> <title><xmldb:select-node query=3D"dc:title/text()"/></title> <!-- extract the creators. There's probably more than one, so we use a nested for-each --> <xmldb:for-each query=3D"dc:creator/text()"> <creator><xmldb:current-node/></creator> </xmldb:for-each> </book> </xmldb:for-each> </xmldb:collection> Before you can start to query the database, you have to select a collection with the collection tag. It accepts a standard XMLDB URI in = its=20 uri attribute. To process a query, you may either use the for-each, or th= e select-node tag. The difference should be obvious: for-each iterates over= all=20 the results it finds for the given query. The body of the statement will = be=20 evaluated for each query result. Contrary to that, the select-node tag si= mply=20 selects the first result it finds. If the result set contains more than o= ne=20 match, the remaining matches will be ignored. The current-node tag is used to actually insert the result currently proc= essed=20 by the for-each tag into the output document. You may restrict the number= of=20 iterations for a for-each tag by specifying the from and to attributes. The sort-by attribute is still experimental: the query res= ults=20 will be sorted by the specified XPath expression. For each of the results= ,=20 the XPath expression is evaluated and the resulting string value is used = to=20 sort the query results in ascending order. As shown above, it is possible to nest multiple for-each or select-node t= ags.=20 The nested tag will be evaluated relative to the current result node. In = the=20 example above, the main for-each statement selects all rdf:Description=20 fragments whose title contains the term "computer". During each iteration= , we=20 further process the current result fragment by using nested for-each and=20 select-node tags to select the title and creators. The transformer as well as some examples are in the CVS. Wolfgang |