Subscribe to

<2017 July>

On this page ...


Send me an email Send mail to the author(s)

© Copyright 2017

Powered by: newtelligence dasBlog 2.0.7180.0
 Wednesday, 28 November 2007
XPath and Namespaces

When you work with BizTalk, you work with XML, and when you work with XML, you are bound to run in to the need to do simple queries against an XML file. The answer of course is XPath queries; essentially just a matter of indicating a path through an XML file leading to a specific element – or set of elements – you want to extract.

As is the case – again when working with BizTalk – these files will typically have one or several namespaces. I created two very similar XML files for the sample (full links to all files are found at the end of the post). The files are invoices and have a standard items loop with some references at the item level that I will be accessing. In overview the files look like the following.

What I want is to find the value of the reference to the order number for the first Item. That is the value of the ReferenceValue element when the sibling ReferenceType element has a value of ‘OrderNumber’.

XML with one namespace

 1 <?xml version="1.0" encoding="utf-8" ?>

 2 <ns0:Invoice xmlns:ns0="http://XPathTest/Schemas/Invoice">

 3   <Items>

 4     <Item>

 5       <Reference>

 6         <ReferenceType>OrderNumber</ReferenceType>

 7         <ReferenceValue>1</ReferenceValue>

 8       </Reference>

 9       <Reference>

10         <ReferenceType>DeliveryNoteNumber</ReferenceType>

11         <ReferenceValue>1</ReferenceValue>

12       </Reference>

13     </Item>

14     .

15     .

For our file with one namespace this is quite simple as XPath assumes all elements in our query are from the default namespace – because nothing else is expressed. Therefore we only have to include the name of the elements in our query which ends up looking like the following.


I am applying two different filters; the index filter to get the first Item element out of a number of repeating elements and a value filter to get the Reference element where the child ReferenceType element has a specific value.

With two namespaces

 1 <?xml version="1.0" encoding="utf-8" ?>

 2 <ns0:Invoice xmlns:ns0="http://XPathTest/Schemas/Invoice">

 3   <Items>

 4     <ns2:Item xmlns:ns2="http://XPathTest/Schemas/Item">

 5       <ns2:Reference>

 6         <ns2:ReferenceType>OrderNumber</ns2:ReferenceType>

 7         <ns2:ReferenceValue>1</ns2:ReferenceValue>

 8       </ns2:Reference>

 9       <ns2:Reference>

10         <ns2:ReferenceType>DeliveryNoteNumber</ns2:ReferenceType>

11         <ns2:ReferenceValue>1</ns2:ReferenceValue>

12       </ns2:Reference>

13     </ns2:Item>

14     .

15     .

For our file with two namespaces we have to indicate the namespaces for all the elements not found in the default namespace of the file which strongly affects the readability as can be seen from the resulting query. (Where I have added line brakes for readability only)

 1 //Items

 2 /*[local-name()='Item' and namespace-uri()

 3 /*[local-name()='Reference' and namespace-uri()

 4 /*[local-name()='ReferenceType' and namespace-uri()
     ='http://XPathTest/Schemas/Item' and .= 'OrderNumber']

 5 /..

 6 /*[local-name()='ReferenceValue' and namespace-uri()

2. The index filter is applied in the same way as with the file with one namespace.

4. For the file with one namespace we applied the value filter at the level of the Reference element where we could indicate we only wanted that element when the child called ReferenceType had a specific value.

For the file with two namespaces we have to move down to the actual ReferenceType element and indicate we only want that element when it has a specific value.

5. Then we move back up to the parent of the – the Reference element.

6. Because we really want to move back down to the ReferenceValue element – which is want we wanted to find.

If you want to grab the files and test for yourself:

File with one namespace

File with two namespaces

Text file with the two XPath queries

Wednesday, 28 November 2007 20:50:19 (Romance Standard Time, UTC+01:00)#Comments [0]
Comments are closed.