当前有两个XML文件,并希望将它们组合以创建1个XML文件。
XML 1(recipes.xml):
<recipes>
<recipe id="182">
<name>Hamburger</name>
<description>Hamburgers are created...</description>
<chapter>1</chapter>
<page_number>13</page_number>
</recipe>
<recipe id="185">
<name>Muffins</name>
<description>Muffins picked with...</description>
<chapter>2</chapter>
<page_number>43</page_number>
</recipe>
<recipes>
XML 2(ingredients.xml):
<ingredients>
<ingredient id="5">
<name>Burger Buns</name>
<recipe_id>182</recipe_id>
<price>$3.00</price>
<quantity>13</quantity>
</ingredient>
<ingredient id ="111">
<name>Carrot</name>
<recipe_id>182</recipe_id>
<price>2.50</price>
<quantity>1</quantity>
</ingredient>
<ingredient id ="535">
<name>Blueberry</name>
<recipe_id>185</recipe_id>
<price>$5.00</price>
<quantity>1 Packet of 200 grams</quantity>
</ingredient>
<ingredients>
我希望它们结合在一起,使食谱具有以下成分: 输出:
<food>
<recipe id ="182">
<name>Hamburger</name>
<description>Hamburgers are created...</description>
<chapter>1</chapter>
<page_number>13</page_number>
<ingredient id ="5">
<name>Burger Buns</name>
<price>$3.00</price>
<quantity>13</quantity>
</ingredient>
<ingredient id ="111">
<name>Carrot</name>
<price>$2.50</price>
<quantity>1</quantity>
</ingredient>
</recipe>
<recipe id ="185">
<name>Muffins</name>
<description>Muffins picked with...</description>
<chapter>2</chapter>
<page_number>43</page_number>
<ingredient id ="535">
<name>Blueberry</name>
<price>$5.00</price>
<quantity>1 Packet of 200 grams</quantity>
</ingredient>
</recipe>
</food>
当前正在尝试在名为BaseX的程序中进行合并。我可以使用单个for循环进行简单查询,但是在将2个单独的文档放在一起时遇到麻烦。
答案 0 :(得分:1)
您只需从其他文档中选择与ID匹配的元素,然后将其填写:
<food>
{
for $recipe in recipes/recipe
let $ingredients := $doc2/ingredients/ingredient[$recipe/@id = recipe_id]
return
<recipe>
{
$recipe/(@*, *),
$ingredients/<ingredient>{@*, * except recipe_id }</ingredient>
}
</recipe>
}
</food>
您可以将其他文档作为外部变量或使用doc('ingredients.xml')
来阅读,带有外部变量的完整示例(默认示例为紧凑型内联XML)位于https://xqueryfiddle.liberty-development.net/6qM2e2j