首页 > 解决方案 > 如何使用 Xquery 向元素节点插入多个属性?

问题描述

如何为每个单独的写入器赋予不同的属性,以便下面的示例之一具有多个写入属性节点?

所需的输出:

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

xml(正如评论和答案中指出的那样无效。)

输出:

<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>

询问:

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>

故意嵌套循环。

标签: xmlxquerybasexxml-attributeflwor

解决方案


你可以做的是例如

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

但是,如果元素或属性名称带有索引,通常会被认为是一种非常糟糕的 XML 样式,您不能为此编写 DTDS 或模式,并且 XPath 或 XQuery 选择可能会变得复杂。


推荐阅读