From: Joe W. <jo...@gm...> - 2010-04-25 16:26:48
|
Hi Eric, Sure, nice job with the xmlns. At this point it's probably a matter of style, but I might further refactor a bit along these lines: let $full-names := for $name in collection("/db/bios")/data/bio/person/name let $first := $name/first/text() let $last := $name/last/text() return concat($first, ' ', $last) return <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> <ul>{ for $full-name in $full-names return <li>{$full-name}</li> }</ul> </body> </html> Cheers, Joe On Sun, Apr 25, 2010 at 12:03 PM, Palmer, Eric <ep...@ri...> wrote: > Joe I modified your fix a little because we wanted the <li></li> element to repeat with each name. > > Structurally your approach makes sense to me. > > If I don't put the xmlns="http://www.w3.org/1999/xhtml" in the <li> element I get the empty namespace problem xmlns="" > > Thanks again - Eric > > > let $name := > for $bio in collection("/db/bios") > let $first := data($bio/data/bio/person/name/first) > let $last := data($bio/data/bio/person/name/last) > let $space := ' ' > return <li xmlns="http://www.w3.org/1999/xhtml">{$first}{$space}{$last}</li> > > return > <html xmlns="http://www.w3.org/1999/xhtml"> > <head> > </head> > <body> > <ul> > { $name } > </ul> > </body> > </html> > > > On 4/25/10 10:51 AM, "Joe Wicentowski" <jo...@gm...> wrote: > > Hi Eric, > > Your use of exist:serialize and curly braces is correct. The reason > the 1st query doesn't work is due to a somewhat unintuitive but easily > worked around aspect of XQuery: your query inherits the namespace from > its surrounding context. In other words, by providing a namespace on > your <html> element, your FLWOR inherits this as its default > namespace. The query engine thinks you're looking for bio elements in > the XHTML namespace. Some alterations are needed to keep the > namespace on the <html> element and still query your data (which has > no namespace, or more correctly, is "in the empty namespace"). The > following post outlines some approaches: > > http://x-query.com/pipermail/talk/2008-December/002954.html > > My preferred approach is to do the data selection outside the <html> > element, and reference this information where needed: > > declare option exist:serialize "method=xhtml media-type=text/xml indent=yes"; > > let $name := > for $bio in collection("/db/bios") > let $first := data($bio/data/bio/person/name/first) > let $last := data($bio/data/bio/person/name/last) > return $first > > return > <html xmlns="http://www.w3.org/1999/xhtml"> > <head> > </head> > <body> > <ul> > <li>{ $name }</li> > </ul> > </body> > </html> > > > Cheers, > Joe > > > On Sun, Apr 25, 2010 at 8:17 AM, Palmer, Eric <ep...@ri...> wrote: >> I'm very new to xquery and am having problems understanding where to place the {} and getting output to serialize to xhtml >> >> If I do >> >> declare option exist:serialize "method=xhtml media-type=text/xml indent=yes"; >> <html xmlns="http://www.w3.org/1999/xhtml"> >> <head> >> </head> >> <body> >> <ul> >> { >> for $bio in collection("/db/bios") >> let $first := data($bio/data/bio/person/name/first) >> let $last := data($bio/data/bio/person/name/last) >> return <li> {$first} </li> >> } >> </ul> >> </body> >> </html> >> >> The page source shows the <li></li> elements to be empty >> >> <html xmlns="http://www.w3.org/1999/xhtml"> >> <head></head> >> <body> >> <ul> >> <li></li> >> <li></li> >> >> >> >> However if I just change the first two lines to <html> >> >> <html> >> <head> >> </head> >> <body> >> <ul> >> { >> for $bio in collection("/db/bios") >> let $first := data($bio/data/bio/person/name/first) >> let $last := data($bio/data/bio/person/name/last) >> return <li> {$first} </li> >> } >> </ul> >> </body> >> </html> >> >> The results are output as xml with the <li></li> filled in with the first name. >> >> <html> >> <head/> >> <body> >> <ul> >> <li>Shari</li> >> <li>Jonathan</li> >> >> >> >> I've tried a lot of combinations but I think I'm my mental model of how to structure xquery code and where to place {} characters, etc. is not correct. >> >> Thanks >> >> Eric Palmer >> U of Richmond > > |