XPath usage and testing

XPath understanding

XPath is used to retrieve a specific node from the XML Document.

For example, if we would like to get the value of the ProfileID we would have the XPath - /Invoice/ProfileID

XPath can also be used to retrieve the value of an attribute of a specific node (<Invoice Name=”Test”>). This attribute value ca be retrieved by using the “@” sign, for example / Invoice /@Name.

XPath can also handle logical expressions that must be placed between square parentheses – “[” “]”

So, for example if you would like find the AllowanceCharge Node that must have an undernode called ChargeIndicator we would use the following XPath - /Invoice/AllowanceCharge[ChargeIndicator]

Furthermore, we can also have logical expressions on the values of the Xml Node.

For example if we would like to find all the AllowanceCharge nodes that have the undernode ChargeIndicator and the value of this undernode is true we would use - /Invoice/AllowanceCharge[ChargeIndicator=’true’]

In the same way we can make multiple checks by using the operators “and” and “or”, remember they must be lowercase. It is important to note that is we multiple operators are used, the “and” operators will have priority. Just like multiply in math. If you think of the “and” as the multiplier/divider and “or” as the addition/subtraction. So the logical expression [TRUE or FALSE and TRUE] will become [TRUE or FALSE] and not [TRUE and TRUE] If we want the “or” operator to be calculated first we must set parentheses like [(TRUE or FALSE) and TRUE] so it will become [TRUE and TRUE].

Let’s say we want all the nodes that that have the undernode ChargeIndicator and the value of this undernode is false and the undernode AllowanceChargeReasonCode different than “CG” we would use –

/Invoice/AllowanceCharge[ChargeIndicator=’false’ and AllowanceChargeReasonCode!=’CG’].

In Document Capture we will only use 4 comparison operators :

  • Greater than – “>”

  • Greater than or Equal – ”>=”

  • Less than – ”<”

  • Less than or Equal – ”<=”

  • Equal – ”=”

  • Not Equal – ”!=” (The only one that is different from NAV ”<>”)

You can also set a specific node to retrieve after the logical expression, in our examples AllowanceCharge node does not have any value, it is an XML node with multiple undernotes. AllowanceCharge nodes represent the Discounts and Charges of an invoice, so we need to get the amount, therefore we can continue the XPath with the node we want the value from the undernode Amount - /Invoice/AllowanceCharge[ChargeIndicator=’false’ and AllowanceChargeReasonCode!=’CG’]/Amount

We could go even deeper and retrieve then VAT percentage by using the Xpath - /Invoice/AllowanceCharge[ChargeIndicator=’false’ and AllowanceChargeReasonCode!=’CG’]/TaxCategory/Percent

This way we can build complex Xpath that help us get the right XML nodes from the XML documents.

We use this logic in our new module to handle the Discounts and Charges that can appear in the XML document.

Custom Operators

  • SUM(%1)

    • Only used on Fields of type Number

    • It Sums up all found XML nodes values from a given XML Path

    • Ex. “SUM(/AllowanceCharge/Amount)”

  • COUNT(%1)

    • It counts all the occurrences of a given XML Path

    • Ex. COUNT(/InvoiceLine)

  • EXISTS

    • It will return TRUE if the given XML Path is found, FALSE if not

OR operator with XML Path

Another Operator is the “|”.

It is used when the value we are looking for can appear in different XML path.

The best example is the XML Node indicating the Quantity for a Document Line. For example for a PEPPOL Invoice the quantity of a line can be found with the XML Path “/Invoice/InvoiceLine/InvoicedQuantity”, but for a PEPPOL Credit Memo the quantity has the XML Path “/CreditNote/CreditNoteLine/CreditedQuantity”. The begging of the path (“/Invoice/InvoiceLine” or “/CreditNote/CreditNoteLine”) will be handled by code, since we setup on the Template the Header and Line tag for both Invoice and Credit Memo. In this case we can use the following XPath:

“/Invoice/InvoiceLine/InvoicedQuantity | /CreditNote/CreditNoteLine/CreditedQuantity”

Auto-formatting of XML Paths

The system should auto-format the XML path consists of:

  • Removing the Header and Line Tag of the XML Document

Consider the following Header Tag – “/Invoice” and Line Tag “/Invoice/InvoiceLine”

Typed

Auto-formatted

/Invoice/AllowanceCharge/Amount

/AllowanceCharge/Amount

/Invoice/OtherDocumentReference/Invoice/ID

/OtherDocumentReference/Invoice/ID

/Invoice/InvoiceLine/Item/Description

/Item/Description

/Invoice/OtherDocumentReference/Invoice/InvoiceLine/ID

/OtherDocumentReference/Invoice/InvoiceLine/ID

/Invoice/InvoiceLine/InvoicedQuantity | CreditNote/CreditNoteLine/CreditedQuantity

/InvoicedQuantity | /CreditedQuantity

  • Keeping the XML path Structure – Always starts with the character “/”, never ends in “/”

Typed

Auto-formatted

AllowanceCharge/Amount

/AllowanceCharge/Amount

/OtherDocumentReference/Invoice/ID/

/OtherDocumentReference/Invoice/ID

Item/Description/

/Item/Description

The same formatting will happen in the case of Complex XML Path and XML Paths containing Custom operators

Last updated

Was this helpful?