如何将XML中某些属性的值相加?

时间:2019-03-27 14:49:34

标签: mysql sql xquery

我有一个XQuery,它需要汇总特定库Branch的所有现有Book副本。分支编号为1,2,3和4,因此可以使用for循环来说明该编号。我需要找出每个分支机构(1、2、3或4)总共有多少本书。

我已经尝试过对XQuery使用sum函数,但是它并没有为每个Branch总计书籍数量,相反,它只是为所有Branchs总计添加了所有书籍,总数为99。表示分支1有99本书,分支2有99本书,依此类推。

我已经在SQL查询中完成了此操作,但是我需要将其转换为XQuery。

SQL查询:

SELECT SUM(OnHand) as onHand_sum 
from Inventory 
WHERE BranchNum ='$inputBranch'

我需要以下代码中“ Stock BookCopies =“部分的帮助。

XQuery:

<results>
{
for $x in doc("C:\basex\henry\Branch.xml")/dataroot/Branch
let $s := doc("C:\basex\henry\Inventory.xml")/dataroot/Inventory
where $x/BranchNum = $s/BranchNum
return 
<row>
<Branch Number="{$x/data(BranchNum)}" Name="{$x/data(BranchName)}"/>
<Stock Bookcopies="{sum($s/data(OnHand))}"/>
</row>
}
</results>

此代码汇总所有分支的所有书籍,并为每个分支输出99。

我需要为分支1分配27,为分支2分配30,等等。

Branch.xml

<?xml version="1.0" encoding="UTF-8" ?>
<dataroot>
  <Branch>
    <BranchNum>1</BranchNum>
    <BranchName>Henry Downtown</BranchName>
    <BranchLocation>16 Riverview</BranchLocation>
    <NumEmployees>10</NumEmployees>
  </Branch>
  <Branch>
    <BranchNum>2</BranchNum>
    <BranchName>Henry On The Hill</BranchName>
    <BranchLocation>1289 Bedford</BranchLocation>
    <NumEmployees>6</NumEmployees>
  </Branch>
  <Branch>
    <BranchNum>3</BranchNum>
    <BranchName>Henry Brentwood</BranchName>
    <BranchLocation>Brentwood Mall</BranchLocation>
    <NumEmployees>15</NumEmployees>
  </Branch>
  <Branch>
    <BranchNum>4</BranchNum>
    <BranchName>Henry Eastshore</BranchName>
    <BranchLocation>Eastshore Mall</BranchLocation>
    <NumEmployees>9</NumEmployees>
  </Branch>
</dataroot>

Inventory.xml

<?xml version="1.0" encoding="UTF-8" ?>
<dataroot>
  <Inventory>
    <BookCode>0180</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>0189</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>0200</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>0200</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>0378</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>079X</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>079X</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>079X</BookCode>
    <BranchNum>4</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>0808</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>1351</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>4</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>1351</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>1382</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>138X</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>2226</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>2226</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>2226</BookCode>
    <BranchNum>4</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>2281</BookCode>
    <BranchNum>4</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>2766</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>2908</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>2908</BookCode>
    <BranchNum>4</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>3350</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>3743</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>3906</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>3906</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>5163</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>5790</BookCode>
    <BranchNum>4</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>6128</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>4</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>6128</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>6328</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>669X</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>6908</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>7405</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>7443</BookCode>
    <BranchNum>4</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>7559</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>8092</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>8720</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9611</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9627</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>5</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9627</BookCode>
    <BranchNum>4</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9701</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9701</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>1</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9701</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9701</BookCode>
    <BranchNum>4</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9882</BookCode>
    <BranchNum>3</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9883</BookCode>
    <BranchNum>2</BranchNum>
    <OnHand>3</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9883</BookCode>
    <BranchNum>4</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
  <Inventory>
    <BookCode>9931</BookCode>
    <BranchNum>1</BranchNum>
    <OnHand>2</OnHand>
  </Inventory>
</dataroot>

1 个答案:

答案 0 :(得分:1)

当您执行sum($s/data(OnHand)时,$s绑定到doc("C:\basex\henry\Inventory.xml")/dataroot/Inventory,即整个清单。尝试更换

let $s := doc("C:\basex\henry\Inventory.xml")/dataroot/Inventory
where $x/BranchNum = $s/BranchNum

作者

let $s := doc("C:\basex\henry\Inventory.xml")/dataroot/Inventory
[BranchNum =  $x/BranchNum]

因此变量仅绑定到清单的选定子集。

顺便说一句,doc()的参数应该是URI,而不是Windows文件名,但是我想您选择的XQuery处理器会让您无所适从,否则您将无法解决。