Use Cases 1.2.4.1 (TREE-Q1): wrong solution

The use case is supposed to take an input file like this

<book>
   <title>Data on the Web</title>
   <author>Serge Abiteboul</author>
   <author>Peter Buneman</author>
   <author>Dan Suciu</author>
   <section id="intro" difficulty="easy" >
     <title>Introduction</title>
     <p>Text ... </p>
     <section>
       <title>Audience</title>
       <p>Text ... </p>
     </section>
     <section>
       <title>Web Data and the Two Cultures</title>
       <p>Text ... </p>
       <figure height="400" width="400">
         <title>Traditional client/server architecture</title>
         <image source="csarch.gif"/>
       </figure>
       <p>Text ... </p>
     </section>
   </section>
...
and generate a nested table of contents.
However, the solution


define function toc($e as element )
   as element*
{
   let $n := local-name( $e )
   return
     if ($n = "section")
     then <section>
              { $e/@* }
              { toc($e/*) }
            </section>
     else if ($n = "title")
     then $e
     else ()
}

<toc>
   {
     toc( document("book.xml")/book )
   }
</toc>

will generate nothing.

- toc will be called the first time with the node "book" as the $e parameter
- the local-name of $e is "toc"
- so toc will return the empty sequence and processing will stop

The correct definition for the "toc" function would be

define function toc( $elmt as element)
   as element*
{
   for $e in $elmt/*
   let $n := local-name( $e )
   return
     if ($n = "section")
     then
         <section>
            { $e/@* }
            { toc($e) }
         </section>
     else if ($n = "title")
     then $e
     else ()
}

Alberto

-------------------------------
Alberto Massari
eXcelon Corp.
http://www.StylusStudio.com

Received on Tuesday, 10 December 2002 06:40:33 UTC