Tuesday, May 6, 2014

XPath: Query XML Exercises

It's important to have a solid understanding of XPath Expression. This will help us convert XML document to tabular format using both OPENXML function and XQuery. If you want to practice querying XML, use this online XPath Tester tool. Or download this XPath Visualizer and install on your computer. Note press Ctrl + N in the XPath Visualizer to create a new source widows then copy and paste the XML source.
For our example, I'll use this XML document. You can view a set of XPath exercises from MSDN.

<bookstore specialty="novel">
  <book style="autobiography">
    <author>
      <first-name>Joe</first-name>
      <last-name>Bob</last-name>
      <award>Trenton Literary Review Honorable Mention</award>
    </author>
    <price>12</price>
  </book>
  <book style="textbook">
    <author>
      <first-name>Mary</first-name>
      <last-name>Bob</last-name>
      <publication>Selected Short Stories of
        <first-name>Mary</first-name>
        <last-name>Bob</last-name>
      </publication>
    </author>
    <editor>
      <first-name>Britney</first-name>
      <last-name>Bob</last-name>
    </editor>
    <price>55</price>
  </book>
  <magazine style="glossy" frequency="monthly">
    <price>2.50</price>
    <subscription price="24" per="year"/>
  </magazine>
  <book style="novel" id="myfave">
    <author>
      <first-name>Toni</first-name>
      <last-name>Bob</last-name>
      <degree from="Trenton U">B.A.</degree>
      <degree from="Harvard">Ph.D.</degree>
      <award>Pulitzer</award>
      <publication>Still in Trenton</publication>
      <publication>Trenton Forever</publication>
    </author>
    <editor>
      <first-name>Preston</first-name>
      <last-name>Phuong</last-name>
    </editor>
    <price intl="Canada" exchange="0.7">6.50</price>
    <excerpt>
      <p>It was a dark and stormy night.</p>
      <p>But then all nights in Trenton seem dark and
      stormy to someone who has gone through what
      <emph>I</emph> have.</p>
      <definition-list>
        <term>Trenton</term>
        <definition>misery</definition>
      </definition-list>
    </excerpt>
  </book>
  <my:book xmlns:my="uri:mynamespace" style="leather" price="29.50">
    <my:title>Who's Who in Trenton</my:title>
    <my:author>Robert Bob</my:author>
  </my:book>
</bookstore>


Let's go over some XPath syntax
Expression                     Description
   /                           Root node(absolute path)
   Node                   The node (relative path)
   //                          All descendant nodes of the current node
   ./                          Current node
   ../                         Parent of current node
   ../../                      Grandparent of current node
  @                         Attribute nodes

Let's go over some examples using the XML document above. Note XML is case sensitive.
1. ALL elements in the XML document.
bookstore
./bookstore
//bookstore

2. All <book> elements. 
bookstore/book
./bookstore/book
//bookstore/book

3. All element with attribute.
//@*
bookstore//@*

4. All <book> element with author's <first-name> element value equals to Mary.
bookstore/book/author[first-name = "Mary"]
bookstore//author[first-name = "Mary"]
.//author[first-name = "Mary"]
*//author[first-name = "Mary"]

5. All <book>  element with author's [last-name] element value equal to Bob and editor's <first-name> element value equals to Preston.
bookstore/book/author[last-name = "Bob" and ../editor[first-name = "Preston"]]

6. All <book> element with author's 
[last-name] element value equals to Bob and <price> element >= 10. 
bookstore/book/author[last-name = "Bob" and ../price >= 10]

7. All <book> element with author's 
[last-name]  element value equals to Bob and <price>  element >=10 
bookstore/book[author[last-name = "Bob" and ../price >= 10]]

8. All <book> element with id attribute.
bookstore/book[@id]

9. All <book> element with attribute style = textbook.
bookstore/book[@style = "textbook"]

10. All <book> element with <price> element value >= 10
/bookstore/book/price[.>=10]
/bookstore/book/price[node()>=10]

Notice the examples above the two periods (..) syntax is equivalent to go up one level, and single period (.) is equivalent to current context. This just like in a file system. Try this, if you are using windows OS, launch command prompt and type this command and press enter: cd ../ (this command change the current directory to the parent folder of the current path) and this: dir . (this command lists all the file and folders in the current path).

Anyway, just like SQL, XPath only need a few days to learn but a lifetime to master.

No comments:

Post a Comment