B - Advanced 5 - Tree traversal

Purpose Purpose of this section is to introduce tree traversal within XML tree in validation rule definitions.


In short, traversal is done with dot notation, by using. between elements. As comparison, tree traversal in XPath is done with a forward slash /. 

Steps we are making in this section are: 

  1. Creating a rule which requires traversing XML tree(GrpHdr/Dbtr/Nm mandatory)
  2. Building and testing


1: Creating a rule which requires traversing XML tree (Header/Dbtr/Nm mandatory)

In this phase, we are focusing on the content in Header as our Transaction should be quite full of rules already. 

First, let's make Debtor mandatory by using the dropdown menu. Opening this rule should contain the following:

Context: HeaderType1
Definition: self.Debtor->size() >= 1

When we break this into sections, we see context to be HeaderType1, meaning that code in definition is always ran when element of this type is found within XML. We refer to this type in OCL section as well, by using the keyword "self" (this is optional but we recommend it to be used to make validation rule more clear)-

Next, traversing to Debtor is done by using dot notation in between of self and Debtor. Restriction for the element is done after the traversal. 

We can modify this rule to go to Name and to see what happens. Code then should be:

Definition: self.Debtor.Name->size() >= 1

2: Building and testing

Before testing, we should modify the error message to be more clear by adding Name after Debtor.  

When testing this, we should be able to see that validation rule itself works as we have defined (Debtor/Name is mandatory, meaning both Debtor and Name must be given. However, we see that the message is always given in location Header. 

In the next section we'll fix this to be more clear.