Using XPath expression with filters in orchestration

In one of our projects, we hadto do an xpath expression in an orchestration to find a value from a nodes subnode, where another subnode has a specific value.
So our input is like this:

<Root>
	<Delivery>
		<DeliveryType>Home</DeliveryType>
		<BoxInfo>
		<BoxID>22</BoxID>
		</BoxInfo>
	</Delivery>
	<Delivery>
		<DeliveryType>Work</DeliveryType>
		<BoxInfo>
		<BoxID>35</BoxID>
		</BoxInfo>
	</Delivery>
	<Delivery>
		<DeliveryType>Home</DeliveryType>
		<BoxInfo>
		<BoxID>12</BoxID>
		<BoxID>87</BoxID>
		</BoxInfo>
	</Delivery>
	<Boxes>
		<Box>
			<ID>12</ID>
			<Contents>Envelopes</Contents>
		</Box>
		<Box>
			<ID>22</ID>
			<Contents>Surface Pro 2</Contents>
		</Box>
		<Box>
			<ID>35</ID>
			<Contents>Stickers</Contents>
		</Box>
		<Box>
			<ID>87</ID>
			<Contents>Stamps</Contents>
		</Box>
	</Boxes>
</Root>

Now what we wanted to do, is to have the ID’s of the boxes, where the delivery type was Home.

In my search for the correct xpath, a colleague told me about DanSharp Xml Viewer, which is a great tool for testing xpaths for BizTalk. The tool let’s you load a schema, and then you can create xpath queries by selecting the nodes. You can also test your own queries, so you know right away what your queries are doing, and make changes quickly. Really a tool that really helps when writing xpath.

Now as for the xpath query itself, once you know the syntax, it’s not that difficult, but since BizTalk (especially in orchestrations) seems to have a slightly different syntax then standard xpath, it can be a challenge finding the correct query. So, the xpath you will want to use is this:

/*[local-name()=’Root’]/*[local-name()=’Delivery’][*[local-name()=’DeliveryType’]/text()=’Home’]/*[local-name()=’BoxInfo’]/*[local-name()=’BoxID’]

Here you navigate through your nodes, and do a filter with this part:

[*[local-name()=’DeliveryType’]/text()=’Home’]

This will retrieve the BoxID’s of home deliveries, which we can then iterate through in our orchestration.

DanSharpXmlViewer

Leave a Reply

Your email address will not be published. Required fields are marked *