Using xpath expressions in BizTalk

Xpath is a very nice way to retrieve values from BizTalk messages, especially when you can not use distinguished fields, for example in looping records. It can however be quite a complicated task as well, to find out how to retrieve a certain value. To that end, I have created a list of xpath filter expressions I commonly use. In these examples I will be using the following XML.

<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 material="cardboard">
			<id>12</id>
			<contents>Envelopes</contents>
		</box>
		<box material="cardboard">
			<id>22</id>
			<contents>Surface Pro 2</contents>
		</box>
		<box material="plastic">
			<id>35</id>
			<contents>Stickers</contents>
		</box>
		<box material="cardboard">
			<id>87</id>
			<contents>Stamps</contents>
		</box>
	</boxes>
</root>
  • Filter on index
    Get the third delivery node.

    /*[local-name()='Root']/*[local-name()='Delivery'][2]

    Get the deliverytype node of the second delivery.

    /*[local-name()='Root']/*[local-name()='Delivery'][2]/*[local-name()='DeliveryType']
  • Filter on subnode text
    Get all delivery nodes, which have a deliverytype of Home.

    /*[local-name()='Root']/*[local-name()='Delivery'][*[local-name()='DeliveryType'][text()='Home']]

    Get the deliverytype node of the delivery for BoxID 87.

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

    Get the deliverytype node, of the box which contains the stickers.

    /*[local-name()='Root']/*[local-name()='Delivery'][*[local-name()='BoxInfo']/*[local-name()='BoxID'][text()=/*[local-name()='Root']/*[local-name()='Boxes']/*[local-name()='Box'][*[local-name()='Contents'][text()='Stickers']]/*[local-name()='ID']]]/*[local-name()='DeliveryType']
  • Continue reading

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.
Continue reading