XQuery计数操作

时间:2016-12-01 15:13:15

标签: xml xquery

有人可以帮我指点正确的方向吗?我正在研究一些XQuery表达式,但到目前为止,我花了几个小时试图评估以下内容:

  1. 列出以四十个或更多字符播放的所有字符
  2. 列出具有最大演员阵容的游戏,即包含最多字符数
  3. 我将其分别解释为:

    1. 查找属性'characterName'的所有值,其中每个不同'playName'属性的值出现40次或更多
    2. 查找属性'playName'的所有值,其中属性'characterName'的不同值的数量是最高的
    3. 鉴于以下XML文档:

      提前感谢您的任何建议。

      <charactersTable>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c1" characterName="Falstaff">
               <plays>
                  <Title playName="Henry4p1" />
                  <Title playName="Henry4p2" />
                  <Title playName="Henry5" />
                  <Title playName="Wives" />
               </plays>
               <speeches>
                  <numberOfSpeeches>471</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c2" characterName="Richard III (Duke of Gloucester)">
               <plays>
                  <Title playName="Henry6p3" />
                  <Title playName="Richard3" />
               </plays>
               <speeches>
                  <numberOfSpeeches>409</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c3" characterName="Henry V">
               <plays>
                  <Title playName="Henry4p1" />
                  <Title playName="Henry4p2" />
                  <Title playName="Henry5" />
               </plays>
               <speeches>
                  <numberOfSpeeches>377</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c4" characterName="Hamlet">
               <plays>
                  <Title playName="Hamlet" />
               </plays>
               <speeches>
                  <numberOfSpeeches>358</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c5" characterName="Othello">
               <plays>
                  <Title playName="Othello" />
               </plays>
               <speeches>
                  <numberOfSpeeches>274</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c6" characterName="Iago">
               <plays>
                  <Title playName="Othello" />
               </plays>
               <speeches>
                  <numberOfSpeeches>272</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c7" characterName="Antony">
               <plays>
                  <Title playName="Antony" />
                  <Title playName="Caesar" />
               </plays>
               <speeches>
                  <numberOfSpeeches>253</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c8" characterName="Timon">
               <plays>
                  <Title playName="Timon" />
               </plays>
               <speeches>
                  <numberOfSpeeches>210</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c9" characterName="Cleopatra">
               <plays>
                  <Title playName="Antony" />
               </plays>
               <speeches>
                  <numberOfSpeeches>204</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c10" characterName="Rosalind">
               <plays>
                  <Title playName="AYLI" />
               </plays>
               <speeches>
                  <numberOfSpeeches>201</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c11" characterName="Brutus">
               <plays>
                  <Title playName="Caesar" />
               </plays>
               <speeches>
                  <numberOfSpeeches>194</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c12" characterName="Vincentio">
               <plays>
                  <Title playName="Measure" />
               </plays>
               <speeches>
                  <numberOfSpeeches>194</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c13" characterName="Coriolanus">
               <plays>
                  <Title playName="Coriolanus" />
               </plays>
               <speeches>
                  <numberOfSpeeches>189</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c14" characterName="Lear">
               <plays>
                  <Title playName="Lear" />
               </plays>
               <speeches>
                  <numberOfSpeeches>188</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c15" characterName="Henry VI">
               <plays>
                  <Title playName="Henry6p1" />
                  <Title playName="Henry6p2" />
                  <Title playName="Henry6p3" />
                  <Title playName="Richard3" />
               </plays>
               <speeches>
                  <numberOfSpeeches>183</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c16" characterName="Earl of Warwick">
               <plays>
                  <Title playName="Henry4p2" />
                  <Title playName="Henry5" />
                  <Title playName="Henry6p1" />
                  <Title playName="Henry6p2" />
                  <Title playName="Henry6p3" />
               </plays>
               <speeches>
                  <numberOfSpeeches>182</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c17" characterName="Queen Margaret">
               <plays>
                  <Title playName="Henry6p1" />
                  <Title playName="Henry6p2" />
                  <Title playName="Henry6p3" />
                  <Title playName="Richard3" />
               </plays>
               <speeches>
                  <numberOfSpeeches>169</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c18" characterName="Richard Plantagenet (Duke of Gloucester)">
               <plays>
                  <Title playName="Henry6p1" />
                  <Title playName="Henry6p2" />
                  <Title playName="Henry6p3" />
                  <Title playName="Richard3" />
               </plays>
               <speeches>
                  <numberOfSpeeches>166</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c19" characterName="Desdemona">
               <plays>
                  <Title playName="Othello" />
               </plays>
               <speeches>
                  <numberOfSpeeches>165</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c20" characterName="Romeo">
               <plays>
                  <Title playName="Romeo" />
               </plays>
               <speeches>
                  <numberOfSpeeches>163</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c21" characterName="Menenius Agrippa">
               <plays>
                  <Title playName="Coriolanus" />
               </plays>
               <speeches>
                  <numberOfSpeeches>162</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c22" characterName="Biron">
               <plays>
                  <Title playName="LLL" />
               </plays>
               <speeches>
                  <numberOfSpeeches>159</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c23" characterName="Hostess Quickly">
               <plays>
                  <Title playName="Henry4p1" />
                  <Title playName="Henry4p2" />
                  <Title playName="Henry5" />
                  <Title playName="Wives" />
               </plays>
               <speeches>
                  <numberOfSpeeches>158</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c24" characterName="Petruchio">
               <plays>
                  <Title playName="Shrew" />
               </plays>
               <speeches>
                  <numberOfSpeeches>158</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c25" characterName="Henry IV">
               <plays>
                  <Title playName="Henry4p1" />
                  <Title playName="Henry4p2" />
                  <Title playName="Richard2" />
               </plays>
               <speeches>
                  <numberOfSpeeches>154</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c26" characterName="Pandarus">
               <plays>
                  <Title playName="Troilus" />
               </plays>
               <speeches>
                  <numberOfSpeeches>153</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c27" characterName="Cressida">
               <plays>
                  <Title playName="Troilus" />
               </plays>
               <speeches>
                  <numberOfSpeeches>152</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c28" characterName="Sir Toby Belch">
               <plays>
                  <Title playName="12Night" />
               </plays>
               <speeches>
                  <numberOfSpeeches>152</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c29" characterName="Valentine">
               <plays>
                  <Title playName="2Gents" />
               </plays>
               <speeches>
                  <numberOfSpeeches>149</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c30" characterName="Proteus">
               <plays>
                  <Title playName="2Gents" />
               </plays>
               <speeches>
                  <numberOfSpeeches>147</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c31" characterName="Macbeth">
               <plays>
                  <Title playName="Macbeth" />
               </plays>
               <speeches>
                  <numberOfSpeeches>146</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c32" characterName="King Edward IV (Plantagenet)">
               <plays>
                  <Title playName="Henry6p2" />
                  <Title playName="Henry6p3" />
                  <Title playName="Richard3" />
               </plays>
               <speeches>
                  <numberOfSpeeches>144</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c33" characterName="Parolles">
               <plays>
                  <Title playName="AWW" />
               </plays>
               <speeches>
                  <numberOfSpeeches>141</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c34" characterName="Cassius">
               <plays>
                  <Title playName="Caesar" />
               </plays>
               <speeches>
                  <numberOfSpeeches>140</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c35" characterName="Robert Shallow">
               <plays>
                  <Title playName="Henry4p2" />
                  <Title playName="Wives" />
               </plays>
               <speeches>
                  <numberOfSpeeches>136</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c36" characterName="Don Pedro">
               <plays>
                  <Title playName="Ado" />
               </plays>
               <speeches>
                  <numberOfSpeeches>135</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c37" characterName="Benedick">
               <plays>
                  <Title playName="Ado" />
               </plays>
               <speeches>
                  <numberOfSpeeches>134</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c38" characterName="Troilus">
               <plays>
                  <Title playName="Troilus" />
               </plays>
               <speeches>
                  <numberOfSpeeches>131</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c39" characterName="Isabella">
               <plays>
                  <Title playName="Measure" />
               </plays>
               <speeches>
                  <numberOfSpeeches>129</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c40" characterName="Queen Elizabeth">
               <plays>
                  <Title playName="Henry6p3" />
                  <Title playName="Richard3" />
               </plays>
               <speeches>
                  <numberOfSpeeches>129</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c41" characterName="Earl of Kent">
               <plays>
                  <Title playName="Lear" />
               </plays>
               <speeches>
                  <numberOfSpeeches>127</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c42" characterName="Claudio">
               <plays>
                  <Title playName="Ado" />
               </plays>
               <speeches>
                  <numberOfSpeeches>125</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c43" characterName="Leontes">
               <plays>
                  <Title playName="WT" />
               </plays>
               <speeches>
                  <numberOfSpeeches>125</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c44" characterName="Duke of Gloucester">
               <plays>
                  <Title playName="Henry5" />
                  <Title playName="Henry6p1" />
                  <Title playName="Henry6p2" />
               </plays>
               <speeches>
                  <numberOfSpeeches>122</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c45" characterName="Pistol">
               <plays>
                  <Title playName="Henry4p2" />
                  <Title playName="Henry5" />
                  <Title playName="Wives" />
               </plays>
               <speeches>
                  <numberOfSpeeches>122</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
         <characters>
            <character characterUri="http://cct.brookes.ac.uk/p00601/characters/c46" characterName="Pericles">
               <plays>
                  <Title playName="Pericles" />
               </plays>
               <speeches>
                  <numberOfSpeeches>121</numberOfSpeeches>
               </speeches>
            </character>
         </characters>
      </charactersTable>
      

1 个答案:

答案 0 :(得分:1)

对我来说,40个字符的阈值似乎非常高,您的数据包含最多7个字符的播放。查询

for $character in /charactersTable/characters/character
for $title in $character/plays/Title/@playName
group by $title
order by count($character) descending, $title
return concat($title, ': n = ',  count($character), '; ',
    string-join(sort($character/@characterName), ', '))

返回:

Henry4p2: n = 7; Earl of Warwick, Falstaff, Henry IV, Henry V, Hostess Quickly, Pistol, Robert Shallow
Henry6p3: n = 7; Earl of Warwick, Henry VI, King Edward IV (Plantagenet), Queen Elizabeth, Queen Margaret, Richard III (Duke of Gloucester), Richard Plantagenet (Duke of Gloucester)
Henry5: n = 6; Duke of Gloucester, Earl of Warwick, Falstaff, Henry V, Hostess Quickly, Pistol
Henry6p2: n = 6; Duke of Gloucester, Earl of Warwick, Henry VI, King Edward IV (Plantagenet), Queen Margaret, Richard Plantagenet (Duke of Gloucester)
Richard3: n = 6; Henry VI, King Edward IV (Plantagenet), Queen Elizabeth, Queen Margaret, Richard III (Duke of Gloucester), Richard Plantagenet (Duke of Gloucester)
Henry6p1: n = 5; Duke of Gloucester, Earl of Warwick, Henry VI, Queen Margaret, Richard Plantagenet (Duke of Gloucester)
Henry4p1: n = 4; Falstaff, Henry IV, Henry V, Hostess Quickly
Wives: n = 4; Falstaff, Hostess Quickly, Pistol, Robert Shallow
Ado: n = 3; Benedick, Claudio, Don Pedro
Caesar: n = 3; Antony, Brutus, Cassius
Othello: n = 3; Desdemona, Iago, Othello
Troilus: n = 3; Cressida, Pandarus, Troilus
2Gents: n = 2; Proteus, Valentine
Antony: n = 2; Antony, Cleopatra
Coriolanus: n = 2; Coriolanus, Menenius Agrippa
Lear: n = 2; Earl of Kent, Lear
Measure: n = 2; Isabella, Vincentio
12Night: n = 1; Sir Toby Belch
AWW: n = 1; Parolles
AYLI: n = 1; Rosalind
Hamlet: n = 1; Hamlet
LLL: n = 1; Biron
Macbeth: n = 1; Macbeth
Pericles: n = 1; Pericles
Richard2: n = 1; Henry IV
Romeo: n = 1; Romeo
Shrew: n = 1; Petruchio
Timon: n = 1; Timon
WT: n = 1; Leontes

您可以使用与上面显示的相似的风格来表达您的两个查询。第一个收集每个游戏的所有角色,按照投射大小过滤游戏,然后返回所有不同的角色(尝试更合理的投射大小,例如5):

for $character in /charactersTable/characters/character
for $title in $character/plays/Title/@playName
group by $title
group by $cast := count($character)
where $cast >= 40
return distinct-values($character/@characterName)

第二个找出所有游戏的投射大小,组一起播放相同的大小并返回具有最高值的组:

(
  for $character in /charactersTable/characters/character
  for $title in $character/plays/Title/@playName
  group by $title
  group by $cast := count($character)
  order by $cast descending
  return string-join($title, ', ')
)[1]