XQuery - 返回没有distinct-values的唯一值

时间:2012-01-10 16:45:21

标签: xml xquery

对于某个XQuery问题,我已经完全停滞不前了。一开始,我在XML文档中没有多个具有相同标题的展品 - 当我改变时我注意到某个问题:结果(在XQuisitor中)显示指南的所有名称两次或每次独特的展览三次,取决于有多少展品共享相同的标题。

我不知道为什么并且会感激一些意见。

这是我尝试过的XQuery代码:

for $u in (//Exhibit/@title)
let $guide := for $g in //Guide[../@title=$u]/@gname return element Guide {$g}
return element Exhibit {attribute Title {$u}, $guide }

示例XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<Exhibits>
<Exhibit title = "Renaissance"  sdate= "2011-09-05"  
edate= "2012-02-05"  sarea = "2400">
<Guide gname = "John Smith" >
<Lang sname ="English"/>
<Lang sname ="German"/>
</Guide>
<Guide gname = "Weber Schneider">
<Lang sname = "German"/>
</Guide>
<Guide gname = "Robert Lundstrom">
<Lang sname = "Swedish"/>
</Guide>
<Guide gname = "Paula Munoz">
<Lang sname = "Spanish"/>
<Lang sname = "German"/>
<Lang sname = "English"/>
</Guide>
</Exhibit>
<Exhibit title = "Renaissance"  sdate= "2011-10-06" 
edate= "2011-11-26" sarea = "2000">
<Guide gname = "John Smith">
<Lang sname ="English"/>
<Lang sname ="German"/>
</Guide>
<Guide gname = "Paula Munoz">
<Lang sname = "Spanish"/>
<Lang sname = "German"/>
<Lang sname = "English"/>
</Guide>
</Exhibit>
</Exhibits>

谢谢!

2 个答案:

答案 0 :(得分:2)

使用

let $vals := /*/Exhibit/@title
  return
     for $t in  $vals[index-of($vals, .)[1]]
       return
        element Exhibit 
           { attribute Title {$t},
             let $gvals := /*/Exhibit[@title eq $t]/Guide/@gname
              return
                for $gname in $gvals[index-of($gvals, .)[1]]
                  return
                    element Guide {attribute gname {$gname}} 
           }

应用于此XML文档(提供的文档稍微复杂一点)

<Exhibits>
  <Exhibit title = "Renaissance"  sdate= "2011-09-05"   edate= "2012-02-05"  sarea = "2400">
    <Guide gname = "John Smith" >
      <Lang sname ="English"/>
      <Lang sname ="German"/>
    </Guide>
    <Guide gname = "Weber Schneider">
      <Lang sname = "German"/>
    </Guide>
    <Guide gname = "Robert Lundstrom">
      <Lang sname = "Swedish"/>
    </Guide>
    <Guide gname = "Paula Munoz">
      <Lang sname = "Spanish"/>
      <Lang sname = "German"/>
      <Lang sname = "English"/>
    </Guide>
  </Exhibit>
  <Exhibit title = "Renaissance"  sdate= "2011-10-06"  edate= "2011-11-26" sarea = "2000">
    <Guide gname = "John Smith">
      <Lang sname ="English"/>
      <Lang sname ="German"/>
    </Guide>
    <Guide gname = "Paula Munoz">
      <Lang sname = "Spanish"/>
      <Lang sname = "German"/>
      <Lang sname = "English"/>
    </Guide>
  </Exhibit>
  <Exhibit title = "Middle Age"  sdate= "2011-10-06"  edate= "2011-11-26" sarea = "2000">
    <Guide gname = "John Smith">
      <Lang sname ="English"/>
      <Lang sname ="German"/>
    </Guide>
    <Guide gname = "Paula Munoz">
      <Lang sname = "Spanish"/>
      <Lang sname = "German"/>
      <Lang sname = "English"/>
    </Guide>
  </Exhibit>
  <Exhibit title = "Middle Age"  sdate= "2011-09-05"   edate= "2012-02-05"  sarea = "2400">
    <Guide gname = "John Smith" >
      <Lang sname ="English"/>
      <Lang sname ="German"/>
    </Guide>
    <Guide gname = "Weber Schneider">
      <Lang sname = "German"/>
    </Guide>
    <Guide gname = "Robert Lundstrom">
      <Lang sname = "Swedish"/>
    </Guide>
    <Guide gname = "Paula Munoz">
      <Lang sname = "Spanish"/>
      <Lang sname = "German"/>
      <Lang sname = "English"/>
    </Guide>
  </Exhibit>
</Exhibits>

产生了想要的正确结果

<Exhibit Title="Renaissance">
   <Guide gname="John Smith"/>
   <Guide gname="Weber Schneider"/>
   <Guide gname="Robert Lundstrom"/>
   <Guide gname="Paula Munoz"/>
</Exhibit>
<Exhibit Title="Middle Age">
   <Guide gname="John Smith"/>
   <Guide gname="Paula Munoz"/>
   <Guide gname="Weber Schneider"/>
   <Guide gname="Robert Lundstrom"/>
</Exhibit>

<强>解释

我们使用的事实是,对于一系列原子值$vals

$vals[index-of($vals, .)[1]]

生成一组包含$vals中所有不同值的项目。

答案 1 :(得分:1)

对于每个独特的展览,您的查询会查找具有相同名称​​的任何展览下方的所有指南并显示这些指南。如果您只想要当前的那些,请尝试以下查询:

for $e in //Exhibit
let $guide := for $g in $e/Guide/@gname return element Guide {$g}
return element Exhibit {attribute Title { $e/@title }, $guide }

使用您提供的输入文件,您的查询将返回:

<Exhibit Title="Renaissance">
  <Guide gname="John Smith"/>
  <Guide gname="Weber Schneider"/>
  <Guide gname="Robert Lundstrom"/>
  <Guide gname="Paula Munoz"/>
  <Guide gname="John Smith"/>
  <Guide gname="Paula Munoz"/>
</Exhibit>
<Exhibit Title="Renaissance">
  <Guide gname="John Smith"/>
  <Guide gname="Weber Schneider"/>
  <Guide gname="Robert Lundstrom"/>
  <Guide gname="Paula Munoz"/>
  <Guide gname="John Smith"/>
  <Guide gname="Paula Munoz"/>
</Exhibit>

并且更正后的产生:

<Exhibit Title="Renaissance">
  <Guide gname="John Smith"/>
  <Guide gname="Weber Schneider"/>
  <Guide gname="Robert Lundstrom"/>
  <Guide gname="Paula Munoz"/>
</Exhibit>
<Exhibit Title="Renaissance">
  <Guide gname="John Smith"/>
  <Guide gname="Paula Munoz"/>
</Exhibit>