How do you use the XPath statement on the left side of an assignment statement (i.e.. the left side of an equation or the equal sign)?  I’m referring to a message assignment shape, in an orchestration, where you want

This is the MSDN page for the BizTalk XPath statement.  There are two problems with that page.

1) Xpath subscripts ared 1-based and not 0-based. See http://stackoverflow.com/questions/3319341/why-do-indexes-in-xpath-start-with-1-and-not-0.
Seems like that page might have a mistake, is says “select the fourth book element”, but shows xpath of “/catalog/book[3]”.
2) It doesn’t show a good example of how to set an XML element value.

The “String” function, is used only the right side of the assignment statement. It is used to convert a “node” to a string value.
You can copy/paste the XPATH from the properties box on a schema or map (after clicking on the element or attribute).
I like to put the xpath in a string variable. I then had to add the subscript, which is needed if you have an element that has “Max Occurs=UNBOUNDED”.
In this case, I only had one MESSAGE_HEADER child, but I still had to specify the subscript of [1].

//NOTE: Substring [0] after MessageHeader
strXPATH = "/*[local-name()='MyRoot' and namespace-uri()='']/*[local-name()='MESSAGE' and namespace-uri()='']/*[local-name()='MESSAGE_HEADER' and namespace-uri()=''][1]/*[local-name()='FILE_NAME' and namespace-uri()='']/text()";
xpath(msgCanonical, strXPATH) = strFileRcvdName;

Note the following;

1) If you leave off the /text() statement, you might get this error:
“selected a node which is not valid for property or distinguished field retrieval, or it selected no node at all. Only text-only elements or attributes may be selected”.

Other errors related to bad Xpath:

Inner exception: Expression must evaluate to a node-set” – I think this happens if you put the String statement on the left side of the assignment statement.

NOTE: A few months after writing this post, I copied the above into a different message assignment shape, and got this error:
Inner exception: Illegal attempt to update the value of part ‘part’ in XLANG/s message ‘msgCanonical’ after the message construction was complete.
The issue was that in that orchestration, the message name should have been msgCanonicalCombined, as that was the message being constructed. It turns out I also had a msgCanonical in the same orchestration. Thus, the errors was subtly telling me that msgCanonical cannot be changed; I need to build a new message. Well, that is what I was doing; I just made a copy/paste typo. Changing to “xpath(msgCanonicalCombined, strXPATH) = strFileRcvdName;” fixed the error.

Tagged with:

Filed under: Orchestrations