首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XQuery中的分组

XQuery中的分组
EN

Stack Overflow用户
提问于 2014-05-19 19:39:00
回答 1查看 157关注 0票数 0

我在分组“表单”标签时遇到了一些麻烦。有4种形式,每种形式可以有超过1件艺术品。尽管我在下面的示例输出中只显示了两个表单。正如您在我的输出中所看到的,这些画是而不是组合在一起的。在期望的输出中它们是。任何帮助或暗示,使我的头绕着它是非常感谢的。

My输出:

代码语言:javascript
复制
  <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>

期望输出

代码语言:javascript
复制
<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>

My XQuery:

代码语言:javascript
复制
<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-value(),但我得到的输出与上面的输出相同

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-19 19:50:05

代码语言:javascript
复制
<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()是您想要的。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23745868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档