Stephen W. Thomas BizTalk Blog

100% Pure BizTalk
posts - 130, comments - 141, trackbacks - 438

My Links

News

Subscribe to my blog via:


Add to Google

Visit my website at:
www.BizTalkGurus.com

Archives

BizTalk 2004 Samples

BizTalk 2006 Samples

BizTalk Videos

BizTalk White Papers

BizTalkBlogs.com

Great BizTalk Blogs

Other Links

XPath Inside Orchestrations in BizTalk 2004

Working with XPath inside Orchestrations is a powerful and simple feature of BizTalk 2004.  The help guide does a good job describing the process (under Using XPath in Message Assignment).

 

I have found that the XPath queries can only be done against a Message and the results can be set to a Message, XML Document or other orchestration variables.  XPath queries can also be executed against untyped messages.  That is, a Message that is of type System.Xml.XmlDocument.

 

CRITICAL: BizTalk xpath can be used to both read values and set values inside your Message.  To set values in your message, you need to be inside a Message Construct shape.

 

Here are some of the things you can do with xpath and how to do them:

- Set a single values inside a Message using xpath

à xpath(SingleXML, "//LineTotal") = nLineTotal;

 

- Extract a single piece of data out of a Message

à sCustomer = xpath(InXML,"string(//Customer)");

 

- Extract a single node out of a large XML Document and assign it to a message or variable

à sXPath = System.String.Format("//Item[{0}]",nCount);

à xDoc = xpath(InXML, sXPath);

 

- Count the number of nodes or occurrences of something inside your message

à nNumberItems = System.Convert.ToInt32(xpath(InXML, "count(//Item)"));

 

A great resource for xpath functions and expressions is the W3Schools.

 

I have put together a sample that shows several different xpath uses inside the Orchestration.  This sample takes in an Order, calculates the total per line, sends out each line item as a single message, and sends out the whole order with an order total.  Note that the Item nodes are not updated on the Output document, only on the single documents.

 

DOWNLOAD: XPath Sample

 

The de-batching approach is based on Darren Jefford’s Blog about Manual Message Splitting.  Make sure you check it out and download his sample as well.

Print | posted on Monday, October 25, 2004 10:30 PM |


Feedback

# re: XPath Inside Orchestrations in BizTalk 2004

Great article.

One question. I am very new to xpath. I would to know if in using xpath I can assign data to elements in my xml for a specific instance of a node.

Example: if my xml is like this

Record1 (n)
- element1

Can I use xpath to assign value "A" to element1 for Record1[1] and "B" to element1 for Record1[2]?
Will xpath a new node Record1[x] if it doesn't exist?

Thanks
12/11/2004 11:07 AM | Dean Cyril Wood

# re: XPath Inside Orchestrations in BizTalk 2004

Please forget about my question.
Your sample explains all. 3/20/2005 5:04 PM | Chris Han

# re: XPath Inside Orchestrations in BizTalk 2004

Have you done this part:

- Extract a single node out of a large XML Document and assign it to a message or variable

sXPath = System.String.Format("//Item[{0}]/@SomeAttributeValue",nCount);

xVar = xpath(InXML, sXPath);

Where xVar is a string variable? I want to load the value of an attribute of a particular instance of a loop based on an iterator that I created from a count of this same loop.

I can see that my string variables that I use to create the full xpath statement look good and correct - but when I use them in the

xVar = xpath(InXML, sXPath); statement the orchestration just dies with no error code or reporting.

What am I doing wrong???

~Desperate



3/23/2006 2:56 PM | Lisa Klein

# re: XPath Inside Orchestrations in BizTalk 2004

Hi Stephen,
I came across your blog while looking for an issue.. The problem is,

I am not able to do XPath() on a web service response message. My web serrvice response is stronly typed. When i do XPath for getting or setting a message element in the WS Response, it is not executing. If I try to get a value, it returns a "" for a string field. But If I give a wrong xpath expression, it fails in the orchestration itself..

I know there is a workaround by using a map and transform the WS Response.. but i was curious to find out why I XPth is not working with WS Response message...

Ur response is greatly appreciated..
Regards,
Shan. 7/18/2006 11:21 AM | Shan

# re: XPath Inside Orchestrations in BizTalk 2004

I have found that Xml that is not formatted does not xpath correctly. So if you have <xml><something> it might not work but
<xml>
<something>
Might work. I’m guessing the message doesn’t get returned formatted.

I’d probably try using a map.
7/18/2006 12:36 PM | Stephen W. Thomas

# re: XPath Inside Orchestrations in BizTalk 2004

Thanks for your Response.

I tried with a map, and my problem solved.. But back to the problem, Why I cannot xpath on a web service message.. I did some more experiments.. and it looks like when I have a target namespace for the XML document I am doing Xpath, XPath fuctions does not work at all.. when I remove the target namespace from the schema, everything works fine.. Any idea why this behavior..? do I need to do something extra if there is aatarget namespace for the xml document..?


7/18/2006 1:23 PM | Shan

# re: XPath Inside Orchestrations in BizTalk 2004

Are you using XPath like /*[local-name()='Root_ABC' and namespace-uri()='http://TestNamespace.Schema1']/*[local-name()='Field' and namespace-uri()=''] with the local-name? Maybe it’s just a namespace issue.

Best way to get the XPath is to open your schema and copy the Instance XPath expression.

Other than that, I don’t have any ideas.

Stephen W. Thomas 7/18/2006 2:17 PM | Stephen W. Thomas

# re: XPath Inside Orchestrations in BizTalk 2004

Question about using the xpath function (from message assignment shape or expression shape).

Example:

xpath(MyMsg, "XPATH1") = "DummyValue";

If the XPATH1 expression would evaluate to more than one value, what would happen?
(for instance //orders/@description)

Would all values be set to "DummyValue"? Or would an error occur?

Any help much appreciated. 7/26/2006 3:45 AM | Bram

# re: XPath Inside Orchestrations in BizTalk 2004

doh!

//order/@description would be a better example. 7/26/2006 3:49 AM | Bram

# re: XPath Inside Orchestrations in BizTalk 2004

It would probably error. I always make sure the Xpath matches the data I'm putting in it.

Stephen W. Thomas 7/26/2006 7:36 AM | Stephen W. Thomas

# re: XPath Inside Orchestrations in BizTalk 2004

How can we write an xpath to change the namespace of a message? 3/5/2008 5:32 PM | OI

# re: XPath Inside Orchestrations in BizTalk 2004

how do i get the root node element name of an untyped message? 7/16/2008 11:12 AM | john smith

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 6 and type the answer here:

Powered by: