为什么这个XQuery flwor表达式没有选择预期的结果?

时间:2015-10-24 13:17:13

标签: xquery

我正在尝试搜索风格为“真人动作”的电影。我有一个包含许多节点的巨大电影文件。我只包含了一个相关节点来解释我的问题。

我有movies.xml如下:

<movies><movie id="movie89" lang="hebrew">
  <label>Metro-Goldwyn-Myer</label>
  <title>Purus massa pede gravida erat ad etiam eu auctor blandit laoreet.</title>
  <directors>
    <director id="person166">Suzanne Gipson</director>
    <director id="person912" award="win">Kurt Meadows</director>
    <director id="person26" award="win">Harry Jacobs</director>
  </directors>
  <actors>
    <actor role="deuteragonist" id="person337">Luis Munoz</actor>
    <actor role="supporting" id="person207" award="win">Hazel Boucher</actor>
    <actor role="deuteragonist" id="person595">Lori Neace</actor>
    <actor role="supporting" id="person293">Lauren Bradburn</actor>
    <actor role="deuteragonist" id="person591">Leona Rosenzweig</actor>
    <actor role="supporting" id="person285">Susan Peterson</actor>
    <actor role="supporting" id="person242" award="nom">Larry Gaudin</actor>
    <actor role="protagonist" id="person76">Thelma Getter</actor>
    <actor role="deuteragonist" id="person2">Zachery Cooley</actor>
    <actor role="protagonist" id="person834">Harold James</actor>
    <actor role="deuteragonist" id="person956">William Hayes</actor>
    <actor role="supporting" id="person931">Christi Abbott</actor>
    <actor role="protagonist" id="person666">Larry James</actor>
    <actor role="protagonist" id="person253">Gary Scanlon</actor>
    <actor role="deuteragonist" id="person744" award="win">Rachel Deloach</actor>
    <actor role="supporting" id="person183">Patricia Wilmoth</actor>
    <actor role="supporting" id="person831">Linda Rayner</actor>
    <actor role="protagonist" id="person115">Sadie King</actor>
    <actor role="protagonist" id="person949">Fred Breden</actor>
    <actor role="deuteragonist" id="person45">Fern Seibold</actor>
    <actor role="protagonist" id="person337">Luis Munoz</actor>
  </actors>
  <extras>
    <sponsor id="person130">David Hellman</sponsor>
    <stunt_person id="person335">Robert Harris</stunt_person>
    <screenwriter id="person120">Toni Carlin</screenwriter>
    <artist id="person433" award="nom">James Munroe</artist>
    <screenwriter id="person113">Latoya Martin</screenwriter>
    <executive_producer id="person840">Mirian Ritchie</executive_producer>
    <sponsor id="person43">William Wilder</sponsor>
    <choreographer id="person143">Lola Myklebust</choreographer>
    <stunt_person id="person781" award="nom">Melvin Garza</stunt_person>
    <dialogue_writer id="person50">Maude Ward</dialogue_writer>
    <casting id="person422">William Bullock</casting>
    <costume_designer id="person169">Luke Robinson</costume_designer>
    <cinematographer id="person549">Murray Mosser</cinematographer>
    <cinematographer id="person518" award="nom">James Davis</cinematographer>
    <casting id="person90" award="nom">Barbara Sheppard</casting>
    <business_partner id="person269">Kevin Rhodes</business_partner>
    <stunt_person id="person986" award="nom">Jeremy Earp</stunt_person>
    <screenwriter id="person848">Mary Hall</screenwriter>
    <sponsor id="person925">Jennifer Hager</sponsor>
    <sponsor id="person758" award="win">William Austin</sponsor>
    <sponsor id="person440">Pauline Carter</sponsor>
    <costume_designer id="person259">Wallace Gravatt</costume_designer>
    <business_partner id="person716">David Crowder</business_partner>
    <casting id="person916">Derek Thompson</casting>
    <artist id="person89" award="nom">Patricia Sloan</artist>
    <stunt_person id="person610">James Garrett</stunt_person>
    <sponsor id="person861">Richard Moeller</sponsor>
    <locations id="person982">Helen Fountain</locations>
    <choreographer id="person618">Roy Penick</choreographer>
    <dialogue_writer id="person630">Mary Hernandez</dialogue_writer>
    <executive_producer id="person885">Linda Welborn</executive_producer>
    <cinematographer id="person326">Wava Huntsinger</cinematographer>
    <executive_producer id="person60" award="win">Jennifer Gibson</executive_producer>
    <executive_producer id="person45" award="nom">Fern Seibold</executive_producer>
    <casting id="person233">Clifford Hall</casting>
    <cinematographer id="person120">Toni Carlin</cinematographer>
    <business_partner id="person764">Emily Hicks</business_partner>
    <business_partner id="person229">Robert Campbell</business_partner>
    <choreographer id="person940">Jeffrey Vaughan</choreographer>
    <dialogue_writer id="person328">Benjamin Riekena</dialogue_writer>
    <dialogue_writer id="person634">Michael Smartt</dialogue_writer>
    <casting id="person927">Tommie Young</casting>
    <choreographer id="person908">Dorothy Varner</choreographer>
    <costume_designer id="person489">Naomi Kempinski</costume_designer>
    <cinematographer id="person714">Sarah Wilgus</cinematographer>
    <producer id="person170">Janet Zaiser</producer>
    <casting id="person572">Brianna Price</casting>
    <executive_producer id="person35">Suzanne Wright</executive_producer>
    <sponsor id="person645">Paula Montoya</sponsor>
    <cinematographer id="person282">Stacy Espinoza</cinematographer>
    <choreographer id="person965" award="nom">Charles Salamone</choreographer>
    <business_partner id="person547">Annie Rafferty</business_partner>
  </extras>
  <genres>
    <genre>musical</genre>
  </genres>
  <styles>
    <style>live action</style>
  </styles>
  <released>
    <release region="NA">1930-2-27</release>
    <release region="CAUC">1982-10-19</release>
  </released>
  <formats>
    <format type="CD">
      <description region="AF">LD</description>
    </format>
    <format type="DVD">
      <description region="ME">3D</description>
    </format>
    <format type="BD">
      <description region="CAUC">3D</description>
    </format>
    <format type="CD">
      <description region="EU">3D</description>
    </format>
    <format type="DVD">
      <description region="SEA">LD</description>
    </format>
    <format type="Stream">
      <description region="SEA">3D</description>
    </format>
    <format type="CD">
      <description region="EE">3D</description>
    </format>
    <format type="DVD">
      <description region="ME">4K</description>
    </format>
  </formats>
  <budget>19831177</budget>
  <earnings>
    <ticket_sales>38287157</ticket_sales>
    <digital_sales>
      <ppv_sales>18822458</ppv_sales>
      <stream_sales>34280453</stream_sales>
      <disc_sales>3977022</disc_sales>
    </digital_sales>
  </earnings>
  <length unit="mins">310</length>
  <country>Falkland Islands (Malvinas)</country>
  <reviews>
    <review proid="critic1">
      <text/>
    </review>
    <review proid="critic2">
      <text>Vitae massa.<emph>Curae fames elit gravida libero nibh suspendisse cubilia dis.</emph>
        <br>Massa porta gravida.</br>
        <bold>Proin neque pede commodo et leo ve pellentesque iaculis quam nunc diam id fames massa suspendisse eleifend.</bold>
        <strong>Lacus proin netus inceptos class justo nunc pharetra sollicitudin.</strong>
      </text>
    </review>
    <review proid="critic3">
      <text>Neque class morbi rhoncus iaculis duis habitant.Nulla class primis aenean proin blandit vulputate.Curae morbi.<keyword>Class porta tellus litora nascetur odio sed litora.</keyword>
        <strong>Proin curae.</strong>
        <bold>Magna nulla lobortis urna sagittis fames nisl fermentum.</bold>Justo risus fermentum pharetra diam posuere ac enim congue mus egestas.<keyword>Class risus.</keyword>
      </text>
    </review>
    <review proid="critic4">
      <text>
        <bold>Justo morbi libero aptent cum pretium ve fermentum velit duis sem penatibus velit fermentum vel blandit donec.</bold>
        <strong>Lorem fames platea vel.</strong>
        <emph>Dolor morbi lacus ullamcorper cum at congue.</emph>Porta donec sociosqu est tempor a adipiscing.Magna felis.<keyword>Nulla risus etiam metus nostra.</keyword>
        <strong>Donec dolor volutpat massa felis mattis sollicitudin penatibus class ve pellentesque nullam.</strong>
      </text>
    </review>
    <review proid="critic5">
      <text>
        <bold>Morbi magna pretium orci a ante justo tristique est lacus.</bold>
        <bold>Morbi purus ullamcorper.</bold>Felis augue.Donec etiam nibh hendrerit luctus a nascetur amet dapibus venenatis amet natoque.</text>
    </review>
    <review proid="critic6">
      <text>
        <bold>Lorem risus platea mi.</bold>Curae purus scelerisque facilisi ante.Porta nulla elit cras sed montes.<keyword>Ipsum metus sit ipsum enim cubilia etiam feugiat eu pede massa tortor nisl urna.</keyword>Ipsum justo.<emph>Neque ipsum neque dis integer placerat at.</emph>Risus augue aliquam nisi etiam.</text>
    </review>
    <review proid="critic7">
      <text>Fames justo.Augue nulla.<bold>Velit morbi neque lobortis nec donec.</bold>
        <keyword>Fusce morbi bibendum dis justo dui aliquam placerat.</keyword>Class fusce.Netus proin aptent et elit lobortis iaculis eget ad.</text>
    </review>
  </reviews>
  <synopsis>
    <text>
      <emph>Augue metus vel lacus tristique viverra tellus mi mus hymenaeos sodales pellentesque malesuada etiam tortor felis at montes commodo rhoncus.</emph>Proin donec litora eu ligula nonummy ut cras suspendisse id cursus egestas venenatis eu elementum.Class donec ornare nulla tellus eget natoque.<strong>Porta fames massa purus donec augue magna.</strong>
    </text>
  </synopsis>
  <ratings max="5">
    <rating proid="critic1">4</rating>
    <rating proid="critic2">2</rating>
    <rating proid="critic3">1</rating>
    <rating proid="critic4">3</rating>
    <rating proid="critic5">4</rating>
    <rating proid="critic6">2</rating>
  </ratings>
  <tracklist>
    <track position="1" duration="1265">
      <title>Metus felis interdum nibh ad.</title>
    </track>
    <track position="2" duration="387">
      <title>Morbi porta hendrerit commodo consectetuer.</title>
    </track>
    <track position="3" duration="403">
      <title>Velit metus porta cursus ornare interdum sed.</title>
    </track>
    <track position="4" duration="1519">
      <title>Etiam fames risus a orci augue consequat odio condimentum.</title>
    </track>
    <track position="5" duration="468">
      <title>Donec dolor libero platea integer natoque.</title>
    </track>
    <track position="6" duration="781">
      <title>Proin lorem rutrum magna neque fames purus.</title>
    </track>
  </tracklist>
  <awards>
    <award type="nomination">BAFTA Best Film</award>
    <award type="nomination">Cannes Best Film</award>
    <award type="win">Cannes Best Film</award>
  </awards>
</movie></movies>

我正在尝试使用以下查询:

for $doc in db:open("movies","movies.xml")/movies/movie/styles
where style="live action"
return $doc/..

但是,即使上面的电影节点包含带有“实时动作”的样式,也不会选择任何内容。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可以访问不受flwor-expressions约束的上下文项。改为使用变量:

for $doc in db:open("movies","movies.xml")/movies/movie/styles
where $doc/style="live action"
return $doc/..

此外,如果你想要匹配的电影,我会选择循环播放电影,这样可以使代码更加可读,结果更紧密地结合在一起进行循环。此外,我将变量名称更改为更“说”的名称:

for $movie in db:open("movies","movies.xml")/movies/movie
where $movie/styles/style="live action"
return $movie

最后,一个简单的谓词也可以没有显式循环(现在,上下文项被绑定,你不需要再使用变量):

db:open("movies","movies.xml")/movies/movie[styles/style="live action"]