2014-05-19 17 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