Skip to main content
xPath - Count() function

Count how many times an element occurs

Micah A. Parker avatar
Written by Micah A. Parker
Updated over 2 months ago

Product: Mapping Manager


The Count() function within an xPath allows you to count how many times a particular Element occurs within the XML File. This is most useful for determining things like how many Packs, or Items are within the file for example.


Example XML


The below example XML is that of an Advance Ship Notice (856) that is in a SOPI (Shipment, Order, Pack, Item) format. Containing multiple packs and items.

<EDI856>

<Shipment>

<ShipmentID>SHIP12345</ShipmentID>

<ShipmentDate>2024-10-16</ShipmentDate>

<Carrier>FedEx</Carrier>

<Order>

<OrderID>ORD98765</OrderID>

<CustomerID>CUST123</CustomerID>

<Pack>

<PackID>PKG001</PackID>

<UCC>123456789012345678</UCC>

<Item>

<ItemID>ITEM001</ItemID>

<Description>Widget A</Description>

<Quantity>10</Quantity>

</Item>

<Item>

<ItemID>ITEM002</ItemID>

<Description>Widget B</Description>

<Quantity>5</Quantity>

</Item>

</Pack>

<Pack>

<PackID>PKG002</PackID>

<UCC>123456789012345679</UCC>

<Item>

<ItemID>ITEM003</ItemID>

<Description>Widget C</Description>

<Quantity>3</Quantity>

</Item>

</Pack>

<Pack>

<PackID>PKG003</PackID>

<UCC>123456789012345680</UCC>

<Item>

<ItemID>ITEM001</ItemID>

<Description>Widget A</Description>

<Quantity>7</Quantity>

</Item>

</Pack>

</Order>

</Shipment>

</EDI856>


Count()


The count() function is used specifically to count how many times something occurs within the XML regardless of the value.

To use it, simply use the count() function and enter in the pathing you wish to count the results of.

count(path)

Count Number of Packs

Using the above example - we can count how many packs show up within the Shipment by simply counting the Pack nodes.

count(//Pack)

3

Since there are three packs within the shipment - the result is 3

Count Number of Items

count(//Item)

4

There are 4 items across all packs - resulting in 4

rev 10/16/2024

Did this answer your question?