xpath 2.0 - XQuery: How to add a comma after sequence except for the last element -


i have following xml:

<form> <bibl>     <biblscope>1</biblscope>     <biblscope>a</biblscope> </bibl> <bibl>     <biblscope>2</biblscope>     <biblscope>b</biblscope> </bibl> <bibl>     <biblscope>38</biblscope>     <biblscope>c</biblscope> </bibl> </form> 

using xquery

for $bibl in form/bibl  return <div> { $biblscope in $bibl/biblscope/text()     return     $biblscope } {if ($bibl[position()] ne $bibl[last()]) ',' else '@'} </div>   

the <biblscope> contents printed 1 after another. after each biblelement, add separator (comma / ",") except last one, code above, is

<div> 1a @ </div> <div> 2b @ </div> <div> 38c @ </div> 

and wrong, because have is

<div>1a,</div> <div>2b,</div> <div>38c@</div> 

(add comma after every bibl element content; last 1 supposed followed @ instead of comma.)

having tried different things time now, need help. proper way this?

the problem position() , last() work on current context, not set flwor expressions. if want use similar semantics, use at $position syntax position counter, , define $last number of results:

let $last := count(form/bibl) $bibl @ $position in form/bibl  return <div> { $biblscope in $bibl/biblscope/text()     return     $biblscope } {if ($position ne $last) ',' else '@'} </div>   

if you're able use xquery 3.0, application operator ! might of use here. replacing unnecessary loops axis steps , element constructors in those, can rely on positional predicates, context set to <bibl/> elements:

form/bibl ! <div> {   biblscope/text(),   if (position() ne last()) ',' else '@' } </div> 

Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -