[xquery-talk] Generating an element sequence matching a template

Howard Katz howardk at fatdog.com
Tue Apr 25 12:39:12 PDT 2006

Given a template of element names, I want to be able to write an XQuery
function that takes that template, as well as a sequence of element nodes,
and returns a "filled-out" list of elements where the template supplies the
final length of the list and the names of empty elements to add to it if
they're missing from the data. It's probably easier to provide an example
than describe it.

For example, given the data:

let $template := ( "e_1", "e_2", "e_3", "e_1", "e_4" )
let $row-data := ( <e_1/>11</e_1>, <e_3>33</e_3>, <e_4>44</e_4> )

I want the "fill-row-data( $template as xs:string+, $row-data as element()+
)" function to return this sequence:

( <e_1/>11</e_1>, <e_2/>, <e_3>33</e_3>, <e_1/>, <e_4>44</e_4> )

The initial $row-data will contain some or all of the elements named in the
template, and in the same order where present. The number of nodes in the
final result will be the same as the number of names in the template. Where
the element sequence has nodes corresponding to the template, those nodes
are left as is. Where nodes that are named in the template are missing from
the actual data, empty filler nodes are generated (eg <e_2/> and <e_1/>.

The problem is motivated by a desire to produce an xhtml table where the
list of <th> headers is fixed, but the actual data rows are sparse and
heterogenous. I want to be able to generate homogenous rows so that the
contents of each row exactly matches the given header. 


More information about the talk mailing list