Use with XSLT
EvaluateXml is a great service to use with complex XSLT functions. Below are examples of the services used for this reason.
Using For-Each to Create Nodes
Sometimes an existing node set will need to be referenced and will be used to determine the output. For example, for each item passed in, to this pricing process, an item node must be returned in the output with the item’s price and other info. If an item cannot be found or priced, each item must return in the output with its own node and information about why it failed.
With this requirement, it is easy to see why a developer would want to say “for each item passed in, output an xml node”. This can be done to dynamically build the Xml of the output depending on the input using Evaluate Xml and the XSLT For-Each function.
Here is that EvaluateXml. The nodes dynamically created based on the input are circled. This is the output node when items fail. Each node (named either NotInUserCatalog or NotFound) returned corresponds to an item node passed (WorkData/Item) in which was given an attribute (NotInUserCatalog and NotFound) to be used to identify the failed items.
Using XSLT Copy-Of to Create Nodes
Using the XSLT Copy-Of function, it copies all the existing XML at the target node. The asterisk is then used to copy all children nodes.
In this example, we have a pricing process. The input to the process is given in WorkData/Item. Then each item is taken and given a “Valid” attribute with either “True” or “False”. This is used later in this output EvaluateXml. There is a for-each which will iterate through all the item nodes that are given as input. However, it will only iterate through the ones with their “Valid” attribute set to “True”. This way, it will only iterate through the items that have information to return (invalid items are not returned here).
After targeting the correct nodes to iterate through, two variables are defined (ItemNumber and Quantity). These are the two pieces of information needed to differentiate between items in the input.
There is an existing RunQuery named “GetItem” which finds all the item info of the items requested by the input of the process.
With the two variables, a copy-of xslt variable is used to target the correct result from the “GetItem” service (the one from the iteration of the input we are on) and copy all the XML returned by the “GetItem” service to the EvaluateXml. The EvalauteXml service is wrapping all this information in an “Item” node, so each item will output an Item node just the same way it was passed in.
Notice that because we are in a For-Each loop, the reference to the GetItem service must exit the for each loop path to reference the correct node. It does this by placing a “/” before the WorkData.
The asterisk selects all child nodes. So this will select all the child nodes of the GetItem service. This will be attributes of the item like ItemNumber, Quantity, and Price.
The EvaluateXml created an output node named “Item”. See below as the Item node now has all the attributes from the GetItem service.