XQuery表达式。有关订购的事情

时间:2014-01-09 21:59:14

标签: xquery

我必须先订购,然后打印出部分文字。我尝试了一些表达式,最后成为了帮助我的代码,但我认为它并不是很好。

我将发布部分文本,然后发布我已经制作的代码。

<?xml version="1.0"?>
<recipes>
  <recipe>
    <title>Beef Parmesan with Garlic Angel Hair Pasta</title>
    <ingredient name="beef cube steak" amount="1.5" unit="pound"/>
    <ingredient name="onion, sliced into thin rings" amount="1"/>
    <ingredient name="green bell pepper, sliced in rings" amount="1"/>
    <ingredient name="Italian seasoned bread crumbs" amount="1" unit="cup"/>
    <ingredient name="grated Parmesan cheese" amount="0.5" unit="cup"/>
    <ingredient name="olive oil" amount="2" unit="tablespoon"/>
    <ingredient name="spaghetti sauce" amount="1" unit="jar"/>
    <ingredient name="shredded mozzarella cheese" amount="0.5" unit="cup"/>
    <ingredient name="angel hair pasta" amount="12" unit="ounce"/>
    <ingredient name="minced garlic" amount="2" unit="teaspoon"/>
    <ingredient name="butter" amount="0.25" unit="cup"/>
    <preparation>
      <step>
        Preheat oven to 350 degrees F (175 degrees C).
      </step>
      <step>
        Cut cube steak into serving size pieces. Coat meat with the bread crumbs
        and parmesan cheese. Heat olive oil in a large frying pan, and saute 1
        teaspoon of the garlic for 3 minutes. Quick fry (brown quickly on both sides)
        meat. Place meat in a casserole baking dish, slightly overlapping edges.
        Place onion rings and peppers on top of meat, and pour marinara sauce
        over all.
      </step>
      <step>
        Bake at 350 degrees F (175 degrees C) for 30 to 45 minutes, depending on
        the thickness of the meat. Sprinkle mozzarella over meat and leave in the
        oven till bubbly.
      </step>
      <step>
        Boil pasta al dente. Drain, and toss in butter and 1 teaspoon garlic. For a
        stronger garlic taste, season with garlic powder. Top with grated parmesan
       and parsley for color. Serve meat and sauce atop a mound of pasta!
      </step>
    </preparation>
    <comment>
      Make the meat ahead of time, and refrigerate over night, the acid in the
      tomato sauce will tenderize the meat even more. If you do this, save the
      mozzarella till the last minute.
    </comment>
    <nutrition calories="1167" fat="23" carbohydrates="45" protein="32"/>
  </recipe>
<recipe>
    <title>Ricotta Pie</title> 
    <ingredient name="filling">
      <ingredient name="ricotta cheese" amount="3" unit="pound"/> 
      <ingredient name="eggs" amount="12"/> 
      <ingredient name="white sugar" amount="2" unit="cup"/> 
      <ingredient name="vanilla extract" amount="2" unit="teaspoon"/> 
      <ingredient name="semisweet chocolate chips" amount="0.25" unit="cup"/> 
      <preparation>
        <step>
          Beat the 12 eggs, 2 cups sugar and vanilla extract together. Stir in
          the ricotta cheese and the chocolate chips. Set aside.
        </step>
      </preparation>
    </ingredient>
    <ingredient name="dough">
      <ingredient name="flour" amount="4" unit="cup"/> 
      <ingredient name="baking powder" amount="5" unit="teaspoon"/> 
      <ingredient name="white sugar" amount="1" unit="cup"/> 
      <ingredient name="shortening" amount="0.5" unit="cup"/> 
      <ingredient name="eggs, lightly beaten" amount="4"/> 
      <ingredient name="vanilla extract" amount="1" unit="teaspoon"/> 
      <preparation>
        <step>
          Combine the flour, baking powder, and 1 cup of the sugar together. Cut in the
          shortening and mix until the mixture resembles coarse crumbs. Mix in 4 of
          the eggs and 1 teaspoon of the vanilla. Divide dough into 4 balls and chill (if
          needed).
        </step>
      </preparation>
    </ingredient>
    <ingredient name="milk" amount="*"/>
    <preparation>
      <step>
        Preheat oven to 325 degrees F (165 degrees C). Grease two deep dish pie
        plates.
      </step>
      <step>
        Roll out 2 of the balls to fit into the pie pans. Do not make the crust too thick
        as it will expand during cooking and get too thick. Do not flute the edges of
        the dough. Roll out the other 2 balls of dough and cut each into 8 narrow
        strips for the top of the crust. Alternately you can use cookie cutters and
        place the cutouts on the top of the pies.
      </step>
      <step>
        Pour the filling evenly into the pie crusts. Top each pie with 8 narrow strips
        of dough or cookie cut-outs. Brush top of pie with milk for shine. Place foil on
        the edge of crust.
      </step>
      <step>
        Bake at 325 degrees F (165 degrees C) for 20 to 30 minutes then remove
        foil. Continue to bake for another 25 or 30 minutes or until a knife inserted in
        the center comes out clean.
      </step>
    </preparation>
    <nutrition calories="349" fat="18" carbohydrates="64" protein="18"/>
  </recipe>
> </recipes>

我所做的代码是

<result>
    {
        for $x in doc()/recipes/recipe
        order by $x/nutrition[@calories>600]
        return 
            ($x/title) |($x/nutrition[@calories])
    }
</result>

XQuery表达式的主要思想是在<result>标签中显示按卡路里排序的标题和卡路里。

2 个答案:

答案 0 :(得分:0)

你的第一次尝试看起来并不那么糟糕,除此之外,结果XML看起来并不是很有用。您可能想要在配方元素中重新包装每个标题和营养。其次,您的订​​单条款可能做得不多。你写道:

order by $x/nutrition[@calories>600]

但是这样你就可以通过营养元素的文本内容进行排序,它没有任何内容。你可能意味着:

order by $x/nutrition[@calories>600]/@calories

或许甚至更好,你想用数字来订购卡路里:

order by $x/nutrition[@calories>600]/@calories/fn:number(.)

HTH!

答案 1 :(得分:0)

我不太清楚你想要达到的目标。那么calories > 600条件代表什么(它在order by子句中似乎没什么用处,因为它只适用于排序条件)。我想,你想过滤掉所有低于600卡路里的营养价值(你当前的声明会订购超过600卡路里的所有食物,但其他的仍会出现在结果中)。

此外,您确实错过了将@calories属性作为排序属性,我想这就是您要实现的目标。

<result>
    {
        for $x in doc()/recipes/recipe
        return ($x/title,
          for $n in $x/nutrition[@calories>600]
          order by $n/@calories
          return $n
        )
    }
</result>