Oops, this must be the longest and worst blog post title you have ever seen. Let’s quickly make clear what I mean:
Recently I needed to update multiple elements using a single rule in the Business Rule Engine (BRE). To most BizTalkers this is nothing special. The thing that complicated this particular scenario was that the elements to update where in different hierarchical levels within the xml instance and thus had different parent nodes.
See the following XML and corresponding XSD instance for an example of this scenario:
<ns0:Customer xmlns:ns0=“http://Samples.BRE.Customer“>
<Name>John</Name>
<Discount>10</Discount>
<Accounts>
<Account>
<ID>12</ID>
<Discount>10</Discount>
</Account>
<SubAccounts>
<SubAccount>
<ID>34</ID>
<Discount>10</Discount>
</SubAccount>
<SubAccount>
<ID>56</ID>
<Discount>20</Discount>
</SubAccount>
<SubAccount>
<ID>78</ID>
<Discount>10</Discount>
</SubAccount>
</SubAccounts>
</Accounts>
</ns0:Customer>
<?xml version=“1.0“encoding=“utf-16“?>
<xs:schema xmlns:b=“http://schemas.microsoft.com/BizTalk/2003” xmlns=“http://Samples.BRE.Customer“
targetNamespace=“http://Samples.BRE.Customer” xmlns:xs=“http://www.w3.org/2001/XMLSchema“>
<xs:element name=“Customer“>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs=“1” maxOccurs=“1” name=“Name” type=“xs:string“ />
<xs:element minOccurs=“1” maxOccurs=“1” name=“Discount” type=“xs:string“ />
<xs:element minOccurs=“0” maxOccurs=“1” name=“Accounts“>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs=“1” maxOccurs=“1” name=“Account“
type=“_Account“ />
<xs:element minOccurs=“0” maxOccurs=“1” name=“SubAccounts“>
<xs:complexType>
<xs:sequence>
<xs:element minOccurs=“1” maxOccurs=“unbounded“
name=“SubAccount” type=“_Account“ />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name=“_Account“>
<xs:sequence>
<xs:element minOccurs=“1” maxOccurs=“1” name=“ID” type=“xs:string“ />
<xs:element minOccurs=“1” maxOccurs=“1” name=“Discount” type=“xs:string“ />
</xs:sequence>
</xs:complexType>
</xs:schema>
As you can see, the Discount node in this instance is on a different level in the XML structure. Also it has a different parent (in this case Customer, Account and SubAccount). Now let’s say we need a rule that updates ALL the Discount nodes that have a value of 10. Of course this could be done easily using three separate rules but that would be a bad solution and could be done more simply.
When we drag and drop a new rule in the BRE based on the above schema. The condition of the rule looks something like this:

the corresponding action would be:

The problem is that, as the xpath statement indicates, this will only affect the Discount nodes directly under the root node. How do we adjust the role so that it will apply to all the Discount nodes in the XML instance?
The solution is to change the XPath Selector and XPath Field properties of the schema. After that we rewrite the rule based on the adjusted values of those properties.
The XPath selector:
The value is by default set to:
/*[local-name()='Customer' and namespace-uri()='http://Samples.BRE.Customer']
We change this to:
//*[local-name()='Discount' and namespace-uri()='']
This will select all the Discount nodes on any level regardless of their parent.
Next we have to think of what to fill in for the Xpath Field property. The Xpath Selector now has the complete xpath statement to get to the desired node set. together the XPath Selector and XPath Field are used by the BRE to reference nodes, so ideally would like to leave the Xpath field empty and remove the default value ‘*[local-name()='Discount' and namespace-uri()='']‘ but cannot because the BRE composer won’t allow an empty Xpath Field property. This means we need a statement that’s not empty and doesn’t affect the nodes selected by the XPath Selector. The ‘self::node()‘ expression will solve this.
The figure below shows the modified values in the Business Rules Composer:


After setting these properties, (re)drag the Discount element to the Conditions and Actions sections. The rule looks like this:

Testing the rule in the Business Rule Composer shows that this in fact works:
XML instance before BRE:
<ns0:Customer xmlns:ns0=“http://Samples.BRE.Customer“>
<Name>John</Name>
<Discount>10</Discount>
<Accounts>
<Account>
<ID>12</ID>
<Discount>10</Discount>
</Account>
<SubAccounts>
<SubAccount>
<ID>34</ID>
<Discount>10</Discount>
</SubAccount>
<SubAccount>
<ID>56</ID>
<Discount>20</Discount>
</SubAccount>
<SubAccount>
<ID>78</ID>
<Discount>10</Discount>
</SubAccount>
</SubAccounts>
</Accounts>
</ns0:Customer>
XML instance after BRE:
<ns0:Customer xmlns:ns0=“http://Samples.BRE.Customer“>
<Name>John</Name>
<Discount>40</Discount>
<Accounts>
<Account>
<ID>12</ID>
<Discount>40</Discount>
</Account>
<SubAccounts>
<SubAccount>
<ID>34</ID>
<Discount>40</Discount>
</SubAccount>
<SubAccount>
<ID>56</ID>
<Discount>20</Discount>
</SubAccount>
<SubAccount>
<ID>78</ID>
<Discount>40</Discount>
</SubAccount>
</SubAccounts>
</Accounts>
</ns0:Customer>
Nice work there.. This is quite an elegant way .. Well done..
I have somewhat similar situation in my xml, having the business rules on multiple nodes with optional elements and do the updates on another xml.
Business rule is ExpirationDate can not be less than IssueDate (If both elements are present). At the same time both of these elements are optional, if I pass the following xml to BRE.
abcder12
1980-01-01T00:00:00
1985-01-01T00:00:00
xyz456
I’m getting the following error
Field “*[local-name()='IssueDate' and namespace-uri()='urn:GeoAccess.PDE.Provider']” does not exist in XML document “Provider”, selector “/Provider/ProviderLicense”.
However the rules works fine, if there is only one node or all elements are present inside the node.
AND
IssueDate Exists in ProviderLicense
ExpirationDate Exists in ProviderLicense
IssueDate is greater than or equal to ExpirationDate
Thanks in advance.