[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

Generating an element sequence matching a template

Michael Kay mhk at mhk.me.uk
Tue Apr 25 22:49:35 PDT 2006


xquery return element
Yes, this makes the problem harder. Something like

for $t at $pos in $template 
let $c := count($template[position() le $pos][. eq $t])
return element {$t} { string($row-data[local-name()=$t][$c]) }

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Howard Katz [mailto:http://xquery.com/mailman/listinfo/talk] 
> Sent: 25 April 2006 21:35
> To: 'Michael Kay'; http://xquery.com/mailman/listinfo/talk
> Subject: RE:  Generating an element sequence 
> matching a template
> 
> That does indeed work with the given example, but it fails in 
> a slightly more complex case, one that unfortunately I didn't 
> think to provide. I showed an example with duplicate entries 
> in the template; I should have shown one that also has 
> duplicates in the *data*, a not uncommon occurrence.
> 
> For example, this solution doesn't work if there are 
> duplicate <e_1> nodes in 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_1>e_11-2</e_1>, <e_4>44</e_4> )
> 
> Saxon reports "A sequence of more than one item is not 
> allowed as the first argument of string()".
> 
> Any thoughts?
> 
> Howard
> 
>  > -----Original Message-----
>  > From: http://xquery.com/mailman/listinfo/talk
>  > [mailto:http://xquery.com/mailman/listinfo/talk] On Behalf Of Michael Kay  
> > Sent: April 25, 2006 11:55 AM  > To: 'Howard Katz'; 
> http://xquery.com/mailman/listinfo/talk  > Subject: RE:  Generating an 
> element sequence  > matching a template  >  > Isn't it simply:
>  >
>  > for $t in $template return
>  >   element {$t} { string($row-data[local-name()=$t]) }
>  >
>  > Michael Kay
>  > http://www.saxonica.com/
>  >
>  > > -----Original Message-----
>  > > From: http://xquery.com/mailman/listinfo/talk
>  > > [mailto:http://xquery.com/mailman/listinfo/talk] On Behalf Of Howard 
> Katz  > > Sent: 25 April 2006 19:39  > > To: http://xquery.com/mailman/listinfo/talk  
> > > Subject:  Generating an element sequence  > 
> > matching a template  > >  > >  > > 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. 
>  > >
>  > > Howard
>  > >
>  > > _______________________________________________
>  > > http://xquery.com/mailman/listinfo/talk
>  > > http://xquery.com/mailman/listinfo/talk
>  >
>  > _______________________________________________
>  > http://xquery.com/mailman/listinfo/talk
>  > http://xquery.com/mailman/listinfo/talk
>  > 
> 



PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.