Thursday, February 27, 2014

My own words Xpath and Navigation

As I wrote in my last post I was going to study further XML so that I'm comfortable explaining a few of the concepts in my own words.

First XPath.  Xpath refers to a position in an XML documents, I saw a good example somewhere online as I was working on this that helped me visualize it more.  Suppose you want to get to some directions to a geographical location, for example you would like to go to the Bagdad Theater in Portland, Oregon (my home town), well you might write down the location something like this:

usa/or/portland/southeast/hawthorn/37ave/BagdadTheater

In XML this might look something like this:
<usa>
  <or>
    <portland>
      <southeast>
        <hawthorn>
          <thirtyseventh>
            </BagdadTheater>
          </thirtyseventh>
        </hawthorn>
      </southeast>
    </portland>
  </or>
</usa>


The Xpath for the Bagdad Theater could be presented as we saw it above: usa/or/portland/southeast/hawthorn/37ave/BagdadTheater

That's all Xpath is, it explains where something is, then you can reference what ever is there.  Simple right?

Next this leads us into Navigation.

DOM node tree
OR:
Node tree
Credit to W3schools.org for these pics!

They say a picture is worth of thousand words.  This picture helped me to understand what exactly an XML tree looks like, and what the relationship is within it.  It's not surprising that when it comes to navigation most books will talk about "child nodes", "siblings", "parents", and "descendants"... they do this because again XML is formatted like a tree (think family tree).  So there are children, parents etc.  To make navigation a little easier we have some abbreviations for the nodes:

This:    //     "Retrieves the context node and all its descendants"
This:    @    "Retrieves the specified ATTRIBUTE (remember attributes are always in quotes) of the context                     node"
This:    ..     "Retrieves the parent of the context node" I tend to think about Linux on this one, and how to                        navigate folders; ".." always goes up one folder

Hopefully this clears up some navigation within XML.

I'll write more on Xquery tomorrow... cheers until then!

Credits: 
wikipedia.org
w3schools.org
Querying Microsoft SQL 2012; Itzik Ben-Gan, Dejan Sarka, Ron Talmage

No comments:

Post a Comment