XML XQuery Add
In XQuery, you can use the element constructor to create new XML elements or nodes.
Syntax
fn:sum($arg1 as xs:anyAtomicType*, $arg2 as xs:anyAtomicType?) as xs:anyAtomicType?
Example
Suppose we have the following XML document:
<products>
<product id="101">
<name>Product A</name>
<price>10</price>
</product>
<product id="102">
<name>Product B</name>
<price>20</price>
</product>
<product id="103">
<name>Product C</name>
<price>30</price>
</product>
</products>
We want to add the prices of all products and retrieve the total sum using XQuery Add function.
sum(/products/product/price)
Output
The output will be:
60
Explanation
The XQuery Add function is used to add two or more values. The first argument is used to specify an array of values that needs to be added. The second argument is optional and is used to specify an initial value.
In our example, we have used the sum function to add all the prices of all products. The argument passed to the sum function specifies the path to the price element in the XML document.
Use
The XQuery Add function is used to sum up values. This function is commonly used in aggregating data from XML documents.
Important Points
- The first argument to the XQuery Add function must be an array of values.
- The second argument to the XQuery Add function is optional.
- The XQuery Add function works with any kind of atomic value.
Summary
In XQuery, the Add function is used to add two or more values. The function takes an array of values as the first argument and an optional initial value as the second argument. The function is useful in aggregating data from XML documents.