在XQuery中分组

时间:2014-05-19 19:39:01

标签: xml

我在分析“表单”标签时遇到了一些麻烦。有4种形式,每种形式可以有超过1种艺术品。虽然我在下面的示例输出中只显示了2个表单。正如您在我的输出中所看到的那样,绘画组合在一起。在他们想要的输出中。我们非常感谢您提供帮助或提示。

我的输出:

  <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>

我尝试过使用distinct-values(),但我只得到与上一次输出相同的输出

<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>

1 个答案:

答案 0 :(得分: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()是您想要的。