X

The Integration blog covers the latest in product updates, best practices, customer stories, and more.

Data Stitch Append for Complex Content Elements

Jorge Herreria
Software Developer 5.

Summary

This blog explains how to do Append for Complex Elements in Data Stitch

In a nutshell:

In order to append instances into an unbounded (aka repeating) Complex Content Element, the instance has to be created in another variable, and then the instance can be appended into the variable having the unbounded complex content element.

I'll explain in more detail, so do not get spook by the nutshell.... Expect some humor along the way.... Perfection is boring; l laugh of my mistakes.

What is Complex Content Element?

There are two types of complex types:

  1. Complex Type Simple Content
  2. Complex Type Complex Content                   

Who came up with this twisted word play, eh? :-0

Complex Type Simple Content

An example of  Complex Type Simple Content Element: The names by language for an Item of the international store is one easy to grasp. Here's the Schema code snippet

On the OIC Orchestration Editor you will see the Schema Tree rendering the name node. On Context Menu (right click) the Node Info will tell you these information. Here's how the schema looks like

Each name is in a specific language. The name is a scalar type: xsd:string. And it is complex because it has an attribute: lang 

To Append a name into the item, you can use the coding pattern for Complex Content (described below in this blog) or use the coding pattern shown in my blog called "Data Stitch Append and Assign for repeating elements"

Complex Type Complex Content

An example of a Complex Type Complex Content Element can be the items of an international store (following the above example). Here is the Schema code snipped. I'm not showing all the xsd details, just the needed to get the idea that item is unbounded ()aka repeating) and it has many child elements.

schema snipped for item

On ICS Orchestration editor it looks like

As you may notice,  item element is complex content: item has name, quantity, prices, addedDate elements as children. 

 

Appending a Complex Type Complex Content

Note: I'll use the following Syntax For Data Stitch Sentences

(TO XPath expression).OPERATION(FROM XPath expression)

Example: ($v1/ns0:customer/ns0:name).ASSIGN(“Jorge Herreria”)

Finally we are in business!

Now that you and I are experts (yeah, right!) on differentiating Complex Content versus Simple Content on Complex Elements, let me walk you on an example on how to add/append content to a Complex Content element: 

I'll append 2 items into the $vc/cart variable of International Store schema (above): a MacBook, and a never gets obsolete, good fashion Pencil 

First will I declare  "vc" variable in Global Variables. vc will be based on the "cart" element .  To lear to declare global variables see "Use Global Variables and Data Stitch ...

Next will I declare a "v1" variable in Global variables. v1 will be based on the item element (cart/item).

Now will I create a Stitch action and do the following statements  (In tailor terms: cuts, folds and stitches):

Statement Explanation
($v1/ns1:item/ns1:name).ASSIGN("MacBook")   Creates the MacBook item instance
($v1/ns1:item/ns1:quantity).ASSIGN(2) continue building the MacBook item instance. (One MacBook you and another for me :-) 
($v1/ns1:item/ns1:prices/ns1:price).ASSIGN(799.89)   continue building the MacBook item instance
  // Now I'm going to append the item instance
($vc/ns1:cart/ns1:item).APPEND($v1/ns1:item)

Appends Macbook instance to $vc/cart/item. Be aware the APPEND happens by value and not by reference.

   
($v1/ns1:item/ns1:name).ASSIGN("Pencil")   Building the pencil instance. Because the MacBook instance was appended by value, then  $vc/cart will not be affected, it is safe to reuse the $vi variable.
($v1/ns1:item/ns1:quantity).ASSIGN(5) continue building Pencil instance
($v1/ns1:item/ns1:prices/ns1:price).ASSIGN(5.45) continue building Pencil instance
($vc/cart/ns1:item).APPEND($v1/ns1:item)

Appends Pencil instance to $vc/cart/item.

At this point the $vc/cart has 2 item instances. 

*Note: To keep the list short I did not assign item/name/@lang and item/addedDate. 

Some experts may ask: Can I use index predicates to avoid having to declare $v1?

Unfortunately nope. Reason being, if you try to append/assign indicating instance 1 in the predicate the runtime understands you want to append the item after the instance 1 or assign on instance 1, so if instance does not exists, then it will throw an exception.

If you know Java is similar dealing with a List<T> 

For the not that experts, here's the explanation on our expert's question:

Our expert is trying to do the following. The following will NOT work if the instances does not exist on the $vc variable :

  1. ($vc/cart/ns1:item[1]/ns1:name).ASSIGN("MacBook")
  2. ($vc/cart/ns1:item[1]/ns1:quantity).ASSIGN(2)
  3. ($vc/cart/ns1:item[1]/ns1:prices/ns1:price).ASSIGN(799.89)
  4. ($vc/cart/ns1:item[2]/ns1:name).ASSIGN("Pencil")
  5. ($vc/cart/ns1:item[2]/ns1:quantity).ASSIGN(5)
  6. ($vc/cart/ns1:item[2]/ns1:prices/ns1:price).ASSIGN(5.45)

What about changing existing data? Do I use predicates?

Yes!!! If the instances exists in the variable, then changing the data can be done by using predicates to identify the instance.

For example, after executing above sentences, the $vc ha the two instances, So now to change the number of MacBooks from 2 to 3, because my wife just asked me why she was not included, and to change the Pencil price to 4.99, here one way to do it:

  1. ($vc/cart/ns1:item[1]/ns1:quantity).ASSIGN(3)
  2. ($vc/cart/ns1:item[2]/ns1:prices[1]/ns1:price).ASSIGN(4.99)

Here's a more elegant one

  1. $vc/cart/ns1:item[ns1:name = 'MacBook']/ns1:quantity).ASSIGN(3)
  2. ($vc/cart/ns1:item[ns1:name ='Pencil']/ns1:prices[1]/ns1:price).ASSIGN(4.99)

oh, oh, I'm getting into predicates... predicates is a whole new blog and I'm ready for a tequila!

All right, I hope I did a good job explaining it. If not, please let me know (you can use the comment section or other means)

 

 

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.