➕ XQuery Add – Perform Arithmetic and Add Values in XML Queries
🧲 Introduction – Why Learn XQuery Add?
XQuery isn’t just about retrieving data—it also allows you to perform arithmetic operations, such as addition, directly within your XML queries. Whether you’re calculating totals, applying discounts, or adding dynamic fields, XQuery’s math capabilities make it easy to manipulate numeric data inside your queries.
🎯 In this guide, you’ll learn:
- How to use
+
for addition in XQuery - How to add numbers and values from XML nodes
- Combine arithmetic with
let
,for
, andreturn
- Real-world use cases with formatted output
🧾 Basic Syntax for Addition in XQuery
let $a := 100
let $b := 50
return $a + $b
✅ Output:
150
📄 Sample XML: books.xml
<catalog>
<book>
<title>Learn XQuery</title>
<price>499</price>
</book>
<book>
<title>Advanced XML</title>
<price>699</price>
</book>
</catalog>
💰 Example – Add 100 to Each Book Price
for $b in doc("books.xml")//book
let $newPrice := $b/price + 100
return
<priceUpdated>
<title>{$b/title/text()}</title>
<newPrice>{$newPrice}</newPrice>
</priceUpdated>
✅ Output:
<priceUpdated>
<title>Learn XQuery</title>
<newPrice>599</newPrice>
</priceUpdated>
💵 Example – Add Prices from Multiple Books
let $book1 := doc("books.xml")//book[1]/price
let $book2 := doc("books.xml")//book[2]/price
return <totalPrice>{$book1 + $book2}</totalPrice>
✅ Output:
<totalPrice>1198</totalPrice>
🧮 Common Arithmetic Operators
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | 10 + 20 | 30 |
- | Subtraction | 30 - 10 | 20 |
* | Multiplication | 10 * 5 | 50 |
div | Division | 10 div 2 | 5 |
mod | Modulo (remainder) | 10 mod 3 | 1 |
🧠 Combine Add with Return Structures
for $b in doc("books.xml")//book
return
<book>
<title>{$b/title/text()}</title>
<priceWithTax>{$b/price + 50}</priceWithTax>
</book>
✅ Adds ₹50 tax or markup to each book price.
❓ Type Safety Tip
- Use
xs:decimal()
ornumber()
if the price is treated as a string:
number($b/price) + 100
✅ Best Practices for XQuery Addition
- ✔️ Use
let
to store arithmetic results before returning - ✔️ Wrap values in XML tags for clean output
- ✔️ Convert strings to numbers if needed with
number()
- ✔️ Use formatting functions if outputting currency
- ❌ Don’t add incompatible types—ensure both are numeric
📌 Summary – Recap & Next Steps
XQuery lets you perform additions and numeric operations directly inside your XML transformations. This is useful for creating totals, updating prices, or doing basic business logic within your data queries.
🔍 Key Takeaways:
- Use
+
to add values and node contents in XQuery - Combine with
for
,let
, andreturn
for structured logic - Supports full arithmetic for XML-based calculations
⚙️ Real-world relevance: Used in price calculators, data aggregators, invoice generators, XML reports, and e-commerce feed generation.
❓ FAQs – XQuery Add
❓ Can I add values from XML nodes directly?
✅ Yes. Just use $node1 + $node2
, assuming both are numeric.
❓ What if values are strings?
✅ Use number($node)
or xs:decimal($node)
to cast them.
❓ Can I add in return
directly?
✅ Yes. You can write <total>{$a + $b}</total>
inside a return clause.
❓ Does XQuery support rounding?
✅ Use functions like round()
, ceiling()
, or floor()
.
❓ Can I calculate totals from a loop?
✅ Use sum()
or aggregate manually with let
inside a for
.
Share Now :