【问题标题】:XQuery combine two xml filesXQuery 合并两个 xml 文件
【发布时间】:2019-04-15 06:50:51
【问题描述】:

目前有两个 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 个单独的文档放在一起。

【问题讨论】:

  • 恐怕您的输入样本都没有,例如&lt;id= "182"&gt; 也不是您的输出,例如&lt;id = "182"&gt; 是 XML,您需要一个元素名称和一个属性 &lt;item id="182"&gt;,所以请编辑您的问题并向我们展示一些格式正确的 XML 示例。
  • 糟糕,我的错。我明白你的意思了。我已经编辑了上面的帖子,希望它现在符合格式良好的 XML 表。

标签: xquery


【解决方案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

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 2021-04-29
    • 2013-10-02
    • 2010-10-13
    相关资源
    最近更新 更多