Complex XPath with BizTalk Schema/Data

http://www.w3schools.com/Xpath/xpath_examples.asp does a pretty good job of showing elementary XPath.
But in the “Real World” with BizTalk, Xpath can be more challenging.
This is a follow-up to my January 3, 2005 blog entitled “Common XPATH Examples, Questions, Issues with Biztalk”
http://nealwalters.blogspot.com/2005/01/common-xpath-examples-questions-issues.html

When BizTalk creates a sample instance, it creates it in this structure (except below, more realistic data has been entered).

<books>
   <book>
     <title>A Tale of Two Cities</title>
     <authorLastname>Dickens</authorLastname>
     <authorFirstname>Charles</authorFirstname>
     <category>fiction</category>
   </book>
   <book>
     <title>Operating Manual for Spaceship Earth</title>
     <authorLastname>Fuller</authorLastname>
     <authorFirstname>Buckminster</authorFirstname>
     <category>science</category>
   </book>
   <book>
     <title>The Singularity Is Near: When Humans Transcend Biology</title>
     <authorLastname>Kurzweil </authorLastname>
     <authorFirstname>Ray</authorFirstname>
     <category>science</category>
   </book>
   <book>
     <title>The Age of Spiritual Machines: When Computers Exceed Human Intelligence</title>
     <authorLastname>Kurzweil </authorLastname>
     <authorFirstname>Ray</authorFirstname>
     <category>science</category>
   </book>
</books>

/books/book/title – returns all titles

/books/book/category[.=’science’] – returns all categories, where the text of that category = ‘science’

/books/book[category=’science’]/title – returns all titles, where the text of that category = ‘science’

/books/book[category=’science’ and authorFirstname=’Ray’]/title – return all titles where Ray is the author and category = ‘science’

When BizTalk creates a sample instance, it creates it in this structure (except below, more realistic data has been entered).
Each “book” record has it’ s own name space. This data was created with a ‘books’ schema, which in turn imported a “book” schema.
The book record was defined by adding a new “record”, and settings its property “Data Structure (Type):

<ns0:books xmlns:ns0=”http://biztalk-training.com/books”>
   <ns1:book xmlns:ns1=”http://biztalk-training.com/book”>
     <title>A Tale of Two Cities</title>
     <authorLastname>Dickens</authorLastname>
     <authorFirstname>Charles</authorFirstname>
     <category>fiction</category>
   </ns1:book>
   <ns1:book xmlns:ns1=”http://biztalk-training.com/book”>
     <title>Operating Manual for Spaceship Earth</title>
     <authorLastname>Fuller</authorLastname>
     <authorFirstname>Buckminster</authorFirstname>
     <category>science</category>
   </ns1:book>
   <ns1:book xmlns:ns1=”http://biztalk-training.com/book”>
     <title>The Singularity Is Near: When Humans Transcend Biology</title>
     <authorLastname>Kurzweil</authorLastname>
     <authorFirstname>Ray</authorFirstname>
     <category>science</category>
   </ns1:book>
   <ns1:book xmlns:ns1=”http://biztalk-training.com/book”>
     <title>The Age of Spiritual Machines: When Computers Exceed Human Intelligence</title>
     <authorLastname>Kurzweil</authorLastname>
     <authorFirstname>Ray</authorFirstname>
     <category>science</category>
   </ns1:book>
</ns0:books>

When BizTalk creates a file from the mapping utility, it combines the namespaces onto the root element, and uses a different prefix, as shown below:

<ns0:books xmlns:ns0=”http://biztalk-training.com/books” xmlns:bk=”http://biztalk-training.com/book”>
   <bk:book>
     <title>A Tale of Two Cities</title>
     <authorLastname>Dickens</authorLastname>
     <authorFirstname>Charles</authorFirstname>
     <category>fiction</category>
   </bk:book>
   <bk:book>
     <title>Operating Manual for Spaceship Earth</title>
     <authorLastname>Fuller</authorLastname>
     <authorFirstname>Buckminster</authorFirstname>
     <category>science</category>
   </bk:book>
   <bk:book>
     <title>The Singularity Is Near: When Humans Transcend Biology</title>
     <authorLastname>Kurzweil</authorLastname>
     <authorFirstname>Ray</authorFirstname>
     <category>science</category>
   </bk:book>
   <bk:book>
     <title>The Age of Spiritual Machines: When Computers Exceed Human Intelligence</title>
     <authorLastname>Kurzweil</authorLastname>
     <authorFirstname>Ray</authorFirstname>
     <category>science</category>
   </bk:book>
</ns0:books>

/*[local-name()=’books’ and namespace-uri()=’http://biztalk-training.com/books’]/*[local-name()=’book’ and namespace-uri()=’http://biztalk-training.com/book’]/title

/*[local-name()=’books’]/*[local-name()=’book’]/title

/*[local-name()=’books’]/*[local-name()=’book’][category[.=’science’]]/title

If for some reason, you had a namespace on category, the same query could be done like this:
/*[local-name()=’books’]/*[local-name()=’book’][*[local-name()=’category’][.=’science’]]/title

/*[local-name()=’books’]/*[local-name()=’book’][category=’science’ and authorFirstname=’Ray’]/title

Thanks to the support team at “StylusStudio.com” for helping with these concepts in their forums. Click here for –>
My discussion thread on these issues
The trick they revealed is that the [] expressions can be nested. Here’s another post, where we talked about selecting a higher node:
link.

That’s the end of the XPath lesson, if you want to see how the “Books” schema was created, see here… BizTalk Schema Imports.

Tagged with:

Filed under: XPATH