How to insert multiple attributes to an element node with Xquery?

295 views Asked by At

How is each individual writer given a distinct attribute, so that one of the examples below would have multiple write attribute nodes?

desired output:

<writers writer="n"></writers>
<writers writer="a";writer="b";writer="c"></writers>  
<writers writer="z"></writers>

(which isn't valid xml as pointed out in the comments and answer.)

output:

<writers writer="Giada De Laurentiis"></writers>
<writers writer="J K. Rowling"></writers>
<writers writer="James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan"></writers>
<writers writer="Erik T. Ray"></writers>

query:

xquery version "3.1";


declare option output:method 'html';


for $doc in db:open("bookstore")
    let $books := $doc/bookstore/book
    for $book in $books
        let $authors := $book/author
        
        return <writers writer="{data($authors)}"></writers>

Intentionally nested loops.

1

There are 1 answers

0
Martin Honnen On

What you can do is e.g.

element { 'writers' } { $authors ! attribute { 'writer' || position() } { . } }

but it is usually consider a very poor XML style if element or attribute names carry an index, you can't write DTDS or schemas for that and XPath or XQuery selection might get complicated.