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']