首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用FLWOR对XML文件中的特定元素进行计数?

如何使用FLWOR对XML文件中的特定元素进行计数?
EN

Stack Overflow用户
提问于 2020-06-20 11:34:08
回答 2查看 30关注 0票数 1

我希望为下面的示例xml计算Orders中所有标记Order的总和。总和。

如何返回元素标签的数量foo?这里,一种计数是:

代码语言:javascript
复制
1
1

而我只想求和: 2。

查询:

代码语言:javascript
复制
for $i in doc("foo.xml")/windward-studios/Orders/Order
return count($i)

sample xml:

代码语言:javascript
复制
<windward-studios>
  <Orders>
    <Order OrderID="10248">
      <CustomerID>VINET</CustomerID>
      <EmployeeID>5</EmployeeID>
      <OrderDate>1996-07-04T14:25:55</OrderDate>
      <RequiredDate>1996-08-01T06:43:44</RequiredDate>
      <ShippedDate>1996-07-16T04:00:12</ShippedDate>
      <ShipVia>3</ShipVia>
      <Freight>32.3800</Freight>
      <ShipName>Vins et alcools Chevalier</ShipName>
      <ShipAddress>59 rue de l'Abbaye</ShipAddress>
      <ShipCity>Reims</ShipCity>
      <ShipRegion/>
      <ShipPostalCode>51100</ShipPostalCode>
      <ShipCountry>France</ShipCountry>
      <OrderDetails>
        <OrderDetail>
          <ProductID>11</ProductID>
          <UnitPrice>14.0000</UnitPrice>
          <Quantity>12</Quantity>
          <Discount>0</Discount>
        </OrderDetail>
        <OrderDetail>
          <ProductID>42</ProductID>
          <UnitPrice>9.8000</UnitPrice>
          <Quantity>10</Quantity>
          <Discount>0</Discount>
        </OrderDetail>
        <OrderDetail>
          <ProductID>72</ProductID>
          <UnitPrice>34.8000</UnitPrice>
          <Quantity>5</Quantity>
          <Discount>0</Discount>
        </OrderDetail>
      </OrderDetails>
    </Order>
    <Order OrderID="10249">
      <CustomerID>TOMSP</CustomerID>
      <EmployeeID>6</EmployeeID>
      <OrderDate>1996-07-05T06:39:18</OrderDate>
      <RequiredDate>1996-08-16T03:39:38</RequiredDate>
      <ShippedDate>1996-07-10T14:39:39</ShippedDate>
      <ShipVia>1</ShipVia>
      <Freight>11.6100</Freight>
      <ShipName>Toms Spezialitäten</ShipName>
      <ShipAddress>Luisenstr. 48</ShipAddress>
      <ShipCity>Münster</ShipCity>
      <ShipRegion/>
      <ShipPostalCode>44087</ShipPostalCode>
      <ShipCountry>Germany</ShipCountry>
      <OrderDetails>
        <OrderDetail>
          <ProductID>14</ProductID>
          <UnitPrice>18.6000</UnitPrice>
          <Quantity>9</Quantity>
          <Discount>0</Discount>
        </OrderDetail>
        <OrderDetail>
          <ProductID>51</ProductID>
          <UnitPrice>42.4000</UnitPrice>
          <Quantity>40</Quantity>
          <Discount>0</Discount>
        </OrderDetail>
      </OrderDetails>
    </Order>
  </Orders>
</windward-studios>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-20 11:47:37

您迭代了每个Order元素,然后请求每个元素的计数。

相反,可以询问Order元素的数量:

代码语言:javascript
复制
let $orders := doc("foo.xml")/windward-studios/Orders/Order
return count($orders)

您可以在不使用FLWOR的情况下更简单地完成此操作:

代码语言:javascript
复制
count(doc("foo.xml")/windward-studios/Orders/Order)
票数 1
EN

Stack Overflow用户

发布于 2020-06-20 13:08:54

邀请对以下内容发表评论:

代码语言:javascript
复制
for $x at $i in db:open("southwind")/windward-studios/Orders/Order
return $i

我发现这很有趣,因为索引可以用作属性。请参见:

https://www.w3schools.com/xml/xquery_select.asp

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

https://stackoverflow.com/questions/62481300

复制
相关文章

相似问题

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