XQuery 映射:将具有相同键的值附加到现有映射
XQuery maps: append value with the same key to the existing map
我有这些地图(它们是根据另一个查询构建的):
let $my_maps :=
(
map {
"book_id": "id_1",
"title": "Some title",
"author": "Some author",
"date": "Some dates"
},
map {
"book_id": "id_2",
"title": "Some title",
"author": "Some author",
"date": "Some dates"
}
)
我有这个XML文件:
<book xml:id="id_1">
<introduction>texte of introduction</introduction>
<chapter id="1">texte of chapter 1</chapter>
<chapter id="2">texte of chapter 2</chapter>
<notes>texte of notes</notes>
<conculsion>texte of conclusion</conculsion>
</book>
我想要什么:
- 只从我的 XML 文件中提取几个部分(对于这个例子,假设
introduction
和 notes
)
- 将 XML 文件中的这些部分(使用键“文本”)附加到具有相应 ID 值的现有地图中。
我的正在进行的 XQuery:
let $grouped_map :=
(
for $id in $my_maps?book_id
for $book_part in collection("path_to_my_collection")/book[@xml:id = $id]//(introduction | notes)
return
map:merge((
$my_maps[?book_id = $id],
for $part in $book_part
group by $xml_id := $part/ancestor::book/@xml:id
return
map:entry("text", $part)
))
)
return
$grouped_map
进行中结果:
1
map {
"book_id": "id_1",
"title": "Some title",
"author": "Some author",
"date": "Some dates",
"text": <introduction>texte of introduction</introduction>
}
2
map {
"book_id": "id_1",
"title": "Some title",
"author": "Some author",
"date": "Some dates",
"text": <notes>texte of notes</notes>
}
预期结果:
1
map {
"book_id": "id_1",
"title": "Some title",
"author": "Some author",
"date": "Some dates",
"text": (<introduction>texte of introduction</introduction>, <notes>texte of notes</notes>)
}
我也试过了map {'duplicates' : 'combine'}
:
let $grouped_map :=
(
for $id in $my_maps?book_id
for $book_part in collection("path_to_my_collection")/book[@xml:id = $id]//(introduction | notes)
return
map:merge((
$my_maps[?book_id = $id],
map:entry("text", $book_part),
map {'duplicates' : 'combine'}
))
)
return
$grouped_map
但结果还是一样。 (顺便说一句,我使用 eXsit,但我在 its documentation about map functions 中没有看到任何提及 map {'duplicates' : 'combine'}
,所以不确定他们是否允许这样做...)
听起来好像你只是想要
for $map in $my_maps
let $books := collection("path_to_my_collection")/book[@xml:id = $map?book_id]
return map:put($map, 'text', $books ! (.//introduction | .//notes))
这会将一个 text
项目放入输入的每个映射中,如果没有匹配的书籍,该值可能是一个空序列。
我有这些地图(它们是根据另一个查询构建的):
let $my_maps :=
(
map {
"book_id": "id_1",
"title": "Some title",
"author": "Some author",
"date": "Some dates"
},
map {
"book_id": "id_2",
"title": "Some title",
"author": "Some author",
"date": "Some dates"
}
)
我有这个XML文件:
<book xml:id="id_1">
<introduction>texte of introduction</introduction>
<chapter id="1">texte of chapter 1</chapter>
<chapter id="2">texte of chapter 2</chapter>
<notes>texte of notes</notes>
<conculsion>texte of conclusion</conculsion>
</book>
我想要什么:
- 只从我的 XML 文件中提取几个部分(对于这个例子,假设
introduction
和notes
) - 将 XML 文件中的这些部分(使用键“文本”)附加到具有相应 ID 值的现有地图中。
我的正在进行的 XQuery:
let $grouped_map :=
(
for $id in $my_maps?book_id
for $book_part in collection("path_to_my_collection")/book[@xml:id = $id]//(introduction | notes)
return
map:merge((
$my_maps[?book_id = $id],
for $part in $book_part
group by $xml_id := $part/ancestor::book/@xml:id
return
map:entry("text", $part)
))
)
return
$grouped_map
进行中结果:
1
map {
"book_id": "id_1",
"title": "Some title",
"author": "Some author",
"date": "Some dates",
"text": <introduction>texte of introduction</introduction>
}
2
map {
"book_id": "id_1",
"title": "Some title",
"author": "Some author",
"date": "Some dates",
"text": <notes>texte of notes</notes>
}
预期结果:
1
map {
"book_id": "id_1",
"title": "Some title",
"author": "Some author",
"date": "Some dates",
"text": (<introduction>texte of introduction</introduction>, <notes>texte of notes</notes>)
}
我也试过了map {'duplicates' : 'combine'}
:
let $grouped_map :=
(
for $id in $my_maps?book_id
for $book_part in collection("path_to_my_collection")/book[@xml:id = $id]//(introduction | notes)
return
map:merge((
$my_maps[?book_id = $id],
map:entry("text", $book_part),
map {'duplicates' : 'combine'}
))
)
return
$grouped_map
但结果还是一样。 (顺便说一句,我使用 eXsit,但我在 its documentation about map functions 中没有看到任何提及 map {'duplicates' : 'combine'}
,所以不确定他们是否允许这样做...)
听起来好像你只是想要
for $map in $my_maps
let $books := collection("path_to_my_collection")/book[@xml:id = $map?book_id]
return map:put($map, 'text', $books ! (.//introduction | .//notes))
这会将一个 text
项目放入输入的每个映射中,如果没有匹配的书籍,该值可能是一个空序列。