The BizTalk Xpath statements usually required for my projects never seem to be as simple as the examples I find online. Usually, it’s because I’m stuck using namespaces, unlike virtually every sample Xpath ever written.
Last week I was working with an XML document where I wanted to capture the unique values in a repeating set. There are a few ways to do this, but I ended up going with the way Stephen Kaufman showed here:
select=”//cities/city[not(@country=preceding-sibling::city/@country)]/@country” />
In the above example, you end up with a variable containing all the unique countries that correspond to the many cities in the list. My challenge was that I was going to use this variable within a map, using Inline XSLT as part of the Scripting functoid. And, my inbound document has elements from multiple namespaces. Using a namespace prefix was not going to work, so I had to write my elements in the
“//*[localname()=’node’ namespaceuri()=’namespace’]” way instead of the easier “//node” or (“//ns1:node”) way. Thank goodness for the Visual XPath tool which made testing my Xpath much easier.
You can’t just swap the node names in the above Xpath with the “[localname()=” namespaceuri()=”]” equivalent, as there are subtle differences. So given that my XML looked like this:
<Results>
<QueryRecord>
<DomainID></DomainID>
<PageName></PageName>
</QueryRecord>
<QueryRecord>
<DomainID></DomainID>
<PageName></PageName>
</QueryRecord>
</Results>
…
Let’s say I have two nodes I need to use in the Xpath statement:
- [local-name()=’QueryRecord’ and namespace-uri()=’http://namespace1′%5D
- [local-name()=’DOMAINID’ and namespace-uri()=’http://namespace1′%5D
My “converted” Xpath now looks like this:
Got that? Yowza. So I’m getting all the unique “domain ids” by grabbing each “domain id” where it doesn’t match a previous instance in the node tree. The main syntax difference between this Xpath and the one at the top is the “*” peppered around. If not included, you’ll get all sorts of does not evaluate to node set errors.
Any other “Xpath with namespaces in BizTalk” war stories or tips to share?
Technorati Tags: BizTalk
Leave a reply to hung Cancel reply