Skip to main content
xPath - Sum() function
Micah A. Parker avatar
Written by Micah A. Parker
Updated over 2 months ago

Product: Mapping Manager


The sum() function allows for us to total across multiple nodes, whether the same one across multiple instances or even different ones added/multiplied together and summing those results.

This is often useful for totaling up item prices, charges, allowances, or even shipping weights.


Example XML


The below example XML is that of an Advance Ship Notice (856) with an SOTPI (Shipment, Order, Tare, Pack, Item) format - which includes a handful of packs and items.

<AdvanceShipNotice>

<Shipment>

<ShipmentIdentification>123456789</ShipmentIdentification>

<ShipmentDate>2024-10-17</ShipmentDate>

<Carrier>UPS</Carrier>

<Addresses>

<ShipFrom>

<Name>Supplier Name</Name>

<Address>123 Supplier Street</Address>

<City>Supplier City</City>

<State>ST</State>

<PostalCode>12345</PostalCode>

<Country>US</Country>

</ShipFrom>

<ShipTo>

<Name>Receiver Name</Name>

<Address>789 Receiver Ave</Address>

<City>Receiver City</City>

<State>ST</State>

<PostalCode>67890</PostalCode>

<Country>US</Country>

</ShipTo>

</Addresses>

<Order>

<OrderNumber>PO123456</OrderNumber>

<OrderDate>2024-10-10</OrderDate>

<Tare>

<TareIdentification>T123456</TareIdentification>

<Pack>

<PackIdentification>PCK123456</PackIdentification>

<Item>

<ItemNumber>ITEM001</ItemNumber>

<ItemDescription>Widget A</ItemDescription>

<Quantity>10</Quantity>

<ItemWeight>

<Weight>50</Weight>

<UnitOfMeasure>LB</UnitOfMeasure>

</ItemWeight>

</Item>

<Item>

<ItemNumber>ITEM002</ItemNumber>

<ItemDescription>Widget B</ItemDescription>

<Quantity>5</Quantity>

<ItemWeight>

<Weight>25</Weight>

<UnitOfMeasure>LB</UnitOfMeasure>

</ItemWeight>

</Item>

</Pack>

<Pack>

<PackIdentification>PCK654321</PackIdentification>

<Item>

<ItemNumber>ITEM003</ItemNumber>

<ItemDescription>Widget C</ItemDescription>

<Quantity>7</Quantity>

<ItemWeight>

<Weight>35</Weight>

<UnitOfMeasure>LB</UnitOfMeasure>

</ItemWeight>

</Item>

<Item>

<ItemNumber>ITEM004</ItemNumber>

<ItemDescription>Widget D</ItemDescription>

<Quantity>3</Quantity>

<ItemWeight>

<Weight>15</Weight>

<UnitOfMeasure>LB</UnitOfMeasure>

</ItemWeight>

</Item>

</Pack>

</Tare>

</Order>

</Shipment>

</AdvanceShipNotice>


Sum() Function


The sum() function can be used to calculate various information across multiple XML Elements resulting in the total across them. This can be useful for determining things such as a Packs total weight, or totaling how many items it contains.

To use the sum() function you simply need to define the function and then insert the path to the XML Nodes you wish to sum from.

sum(path)

Calculating Total Quantity within the Shipment

If you need to calculate the total quantity across all Tares & Packs we can do so by summing up all Item Quantities into a single value.

By telling the system to sum all Item Quantities (Item/Quantity) you'll automatically calculate the total across all items. We will be assuming the xPath is placed high-enough in the hierarchy (Order/Tare level) that it will have access to all Packages in the XML.

sum(//Item/Quantity)

25

In this scenario our XML has 4 items with 10, 5, 7, & 3 quantities - which would total up to 25

ITEM001 - Quantity: 10
ITEM002 - Quantity: 5
ITEM003 - Quantity: 7
ITEM004 - Quantity: 3

Note: This is assuming within Mapping Manager that you placed the xPath at the Order or Tare level allowing it to search across all Packs and Items.

Calculate Total Weight

We can do the same thing to calculate the Total Weight of the shipment by performing a multiplication within the path to sum the total of all item's weight (per-unit) against the quantity defined for that item.

sum(//Item/Quantity * //ItemWeight/Weight)

915

We're taking each Item's Quantity and multiplying it against the Item Weight to then give us a total Shipment Weight across all Items regardless of Package.

ITEM001 - Quantity: 10 x Weight: 50 (500)
ITEM002 - Quantity: 5 x Weight: 25 (125)
ITEM003 - Quantity: 7 x Weight: 35 (245)
ITEM004 - Quantity: 3 x Weight: 15 (45)

10/17/2024

Did this answer your question?