如何使用 FLWOR 计算 XML 文件中的特定元素?
How to count specific elements in an XML file using FLWOR?
我正在为下面的示例 xml
计算 Orders
中所有标签 Order
的总和。总金额。
如何return元素标签的数量foo
?在这里,一种计数是:
1
1
而我只求总和:2.
查询:
for $i in doc("foo.xml")/windward-studios/Orders/Order
return count($i)
sample xml:
<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>
您正在遍历每个 Order
元素,然后询问每个元素的计数。
相反,要求 Order
个元素的计数:
let $orders := doc("foo.xml")/windward-studios/Orders/Order
return count($orders)
您可以在没有 FLWOR 的情况下更简单地执行此操作:
count(doc("foo.xml")/windward-studios/Orders/Order)
邀请评论:
for $x at $i in db:open("southwind")/windward-studios/Orders/Order
return $i
我觉得这很有趣,因为索引可以用作属性。见:
我正在为下面的示例 xml
计算 Orders
中所有标签 Order
的总和。总金额。
如何return元素标签的数量foo
?在这里,一种计数是:
1
1
而我只求总和:2.
查询:
for $i in doc("foo.xml")/windward-studios/Orders/Order
return count($i)
sample xml:
<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>
您正在遍历每个 Order
元素,然后询问每个元素的计数。
相反,要求 Order
个元素的计数:
let $orders := doc("foo.xml")/windward-studios/Orders/Order
return count($orders)
您可以在没有 FLWOR 的情况下更简单地执行此操作:
count(doc("foo.xml")/windward-studios/Orders/Order)
邀请评论:
for $x at $i in db:open("southwind")/windward-studios/Orders/Order
return $i
我觉得这很有趣,因为索引可以用作属性。见: