2014-05-19 31 views
0

我在分组“表单”标签时遇到了一些麻烦。有四种类型的表格,每种表格可以有一个以上的作品。尽管我在下面的示例输出中只显示了两种形式。正如你可以在我的输出中看到的画作是而不是分组在一起。在期望的输出他们是。任何帮助或暗示,让我的头附近,是值得赞赏的。在XQuery中分组

我的输出:

<author> 
    <name>BRAMANTE, Donato</name> 
    <born-died>b. 1444, Fermignano, d. 1514, Roma</born-died> 
    <nationality>Italian</nationality> 
    <biography>Donato Bramante was an Italian architect, who introduced the Early Renaissance style to Milan and the High Renaissance style to Rome, where his most famous design was St. Peter's Basilica.</biography> 
    <artworks form="painting"> 
     <artwork date="1477"> 
     <form>painting</form> 
     <artworkForm>painting</artworkForm> 
     <title>Heraclitus and Democritus</title> 
     <technique>Fresco transferred to canvas</technique> 
     <location>Pinacoteca di Brera, Milan</location> 
     </artwork> 
    </artworks> 
    <artworks form="painting"> 
     <artwork date="1477"> 
     <form>painting</form> 
     <artworkForm>painting</artworkForm> 
     <title>Heraclitus and Democritus</title> 
     <technique>Fresco transferred to canvas</technique> 
     <location>Pinacoteca di Brera, Milan</location> 
     </artwork> 
    </artworks> 
    <artworks form="architecture"> 
     <artwork date="1485"> 
     <form>architecture</form> 
     <artworkForm>architecture</artworkForm> 
     <title>Interior view toward choir</title> 
     <technique>Fresco, height of arch 10,6 m</technique> 
     <location>Santa Maria presso San Satiro, Milan</location> 
     </artwork> 
    </artworks> 
    </author> 

所需的输出

<author> 
    <name>BRAMANTE, Donato</name> 
    <born-died>b. 1444, Fermignano, d. 1514, Roma</born-died> 
    <nationality>Italian</nationality> 
    <biography>Donato Bramante was an Italian architect, who introduced the Early Renaissance style to Milan and the High Renaissance style to Rome, where his most famous design was St. Peter's Basilica.</biography> 
    <artworks form="painting"> 
     <artwork date="1477"> 
     <form>painting</form> 
     <artworkForm>painting</artworkForm> 
     <title>Heraclitus and Democritus</title> 
     <technique>Fresco transferred to canvas</technique> 
     <location>Pinacoteca di Brera, Milan</location> 
     </artwork> 
     <artwork date="1477"> 
     <form>painting</form> 
     <artworkForm>painting</artworkForm> 
     <title>Heraclitus and Democritus</title> 
     <technique>Fresco transferred to canvas</technique> 
     <location>Pinacoteca di Brera, Milan</location> 
     </artwork> 
    </artworks> 
    <artworks form="architecture"> 
     <artwork date="1485"> 
     <form>architecture</form> 
     <artworkForm>architecture</artworkForm> 
     <title>Interior view toward choir</title> 
     <technique>Fresco, height of arch 10,6 m</technique> 
     <location>Santa Maria presso San Satiro, Milan</location> 
     </artwork> 
    </artworks> 
    </author> 

我的XQuery:

<authors> 
    { 
    for $author in doc("authors.xml")/authors/author 
    let $artworks := doc("artworks.xml")/artworks/artwork 
     return 
      <author> 
       <name>{$author/name/text()}</name> 
       <born-died>{$author/born-died/text()}</born-died> 
       <nationality>{$author/nationality/text()}</nationality> 
       <biography>{$author/biography/text()}</biography> 
       { 
        for $artwork in $artworks 
        let $nameInAuthor := $author/name/text() 
        let $nameInArtwork := $artwork/author/text() 
        where $nameInAuthor = $nameInArtwork 
         return 
          <artworks form="{$artwork/form/text()}"> 
           <artwork date="{$artwork/date/text()}"> 
            <title>{$artwork/title/text()}</title> 
            <technique>{$artwork/technique/text()}</technique> 
            <location>{$artwork/location/text()}</location> 
           </artwork> 
          </artworks> 
       } 
      </author> 
    } 
</authors> 

我试过ü唱不同的值(),但我刚刚得到上述

<authors> 
    { 
    for $author in doc("authors.xml")/authors/author 
    let $artworks := doc("artworks.xml")/artworks/artwork 
     return 
      <author> 
       <name>{$author/name/text()}</name> 
       <born-died>{$author/born-died/text()}</born-died> 
       <nationality>{$author/nationality/text()}</nationality> 
       <biography>{$author/biography/text()}</biography> 
       { 
        for $artwork in $artworks 
        let $nameInAuthor := $author/name/text() 
        let $nameInArtwork := $artwork/author/text() 
        where $nameInAuthor = $nameInArtwork 
         return 
          for $artworkForm in distinct-values($artworks/form/text()) 
          let $form := $artwork/form/text() 
          where $form = $artworkForm 
           return 
            <artworks form="{$artworkForm}"> 
            {   
             <artwork date="{$artwork/date/text()}"> 
              <form>{$form}</form> 
              <artworkForm>{$artworkForm}</artworkForm> 
              <title>{$artwork/title/text()}</title> 
              <technique>{$artwork/technique/text()}</technique> 
              <location>{$artwork/location/text()}</location> 
             </artwork> 
            } 
            </artworks> 

       } 
      </author> 
    } 
</authors> 

回答

3
<authors>{ 
    let $artworks := doc("artworks.xml")/artworks/artwork 
    let $atrwork-forms := distinct-values($artworks/form) 
    for $author in doc("authors.xml")/authors/author 
    return 
    <author> 
     { $author/name, 
      $author/born-died, 
      $author/nationality, 
      $author/biography, 

      for $form in $artwork-forms 
      let $art := $artwork[form = $form][author = $author] 
      where $art 
      return 
      <artworks form="{ $form }">{  
       for $a in $art 
       return 
        <artwork date="{ $a/date }"> 
        <form>{ $form }</form> 
        <artworkForm>{ $form }</artworkForm> 
        { $a/title, 
         $a/technique, 
         $a/location 
        }</artwork> 
      }</artworks> 
     }</author> 
}</authors> 

请注意,如果你只是想复制的元素,你可以简单地使用XPath像上面选择它的输出作为我以前的输出相同的,而不是输出一个字符串并将其重新包装在一个元素中。这应该使事情更容易阅读。此外,text()将选择所有文本节点,有时不止一个,通常不是您想要的。在大多数情况下,除非您有特定原因string()是您想要的。

+0

谢谢。它没有抓住没有艺术品的作品数据的作品。也许是一个路径问题,但它是一个正确的方向开始我认为 – bigubosu

+0

得到它的工作,这只是一个元素名称的问题。谢谢。 – bigubosu