XQuery组合两个xml文件

XQuery组合两个xml文件,xquery,Xquery,当前有两个XML文件,并希望将它们合并以创建一个XML文件 XML1(recipes.XML): 汉堡包 汉堡包是创造出来的。。。 1. 13 松饼 用。。。 2. 43 XML 2(components.XML): 汉堡包 182 $3.00 13 胡萝卜 182 2.50 1. 蓝莓 185 $5.00 一包200克 我想让它们结合起来,使食谱包含如下成分: 输出: 汉堡包 汉堡包是创造出来的。。。 1. 13 汉堡包 $3.00 13 胡萝卜 $2.50 1. 松饼 用。。。 2

当前有两个XML文件,并希望将它们合并以创建一个XML文件

XML1(recipes.XML):


汉堡包
汉堡包是创造出来的。。。
1.
13
松饼
用。。。
2.
43
XML 2(components.XML):


汉堡包
182
$3.00
13
胡萝卜
182
2.50
1.
蓝莓
185
$5.00
一包200克
我想让它们结合起来,使食谱包含如下成分: 输出:


汉堡包
汉堡包是创造出来的。。。
1.
13
汉堡包
$3.00
13
胡萝卜
$2.50
1.
松饼
用。。。
2.
43
蓝莓
$5.00
一包200克

目前正在尝试在名为BaseX的程序中进行合并。我可以使用一个for循环进行简单的查询,但在将两个单独的文档放在一起时遇到问题。

您可以从另一个文档中选择与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>

{
对于配方/配方中的$recipe
让$Components:=$doc2/配料/配料[$recipe/@id=recipe\u id]
回来
{
$recipe/(@*,*),
$Components/{@*,*配方除外}
}
}

您可以作为外部变量读入或使用
doc('components.xml')
,带有外部变量的完整示例(示例的紧凑性默认为内联xml)的另一个文档位于

,您的输入示例(例如
)和输出(例如
)都不是xml,您需要一个元素名和一个属性,
,因此请编辑您的问题并向我们展示一些格式良好的XML示例。我明白你的意思。我已经编辑了上面的文章,希望它现在是一个格式良好的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>
<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>