« Posts under Technobable

All things XPath

What’s XPath?

So XPath is really just a means of accessing data on xml nodes. XML has structure, with node and branches and attributes; XPath is the notation we use to express which nodes, branches or attributes we want to access. XSL makes use of this notation in order to change the structure and layout of an XML document. The process of changing one form of xml into another is called an XSL Transformation, or xslt for short.

The Basics!

If you’ve ever laid out a web page, YOU ALREADY KNOW XPATH! More specifically, if you have ever used an image in a webpage, you already some understanding of how xpath works. Take the following example:

<img src=”/images/my_image.gif“/>

The value of the “src” attribute is an xpath expression. It means the following: select the image that resides in the folder named “images” at the root of the web server’s domain, and choose the image named “my_image.gif”. In this example, the server’s directory structure is the XML we are navigating, and the image location is the data we want to transform to reuse in a new location.

If you’ve ever used a command prompt or terminal, you’ll notice that xsl is a little more intuitive because of expressions like this:

../siblingElement = go up one directory and pick the “siblingElement”
./currentElement = pick node “currentElement” in the current location
/rootElement = pick the node named “rootElement” at the base of the xml structure
node/childElement = pick the node named “childElement” inside the node named “node” which is in the current location

In addition, there are ways of accessing node attributes with the “@” sign:

< blockquote >../siblingElement/@id = go up one directory and pick the attribute “id” belonging to the node named the “siblingElement”
./currentElement/@name = pick attribute named “name” belonging to the node “currentElement” in the current location
/rootElement/@rootId = pick the attribute named “rootId” belonging to the node named “rootElement” at the base of the xml structure
node/childElement /@count= pick the attribute named “count” belonging to the node named “childElement” inside the node named “node” which is in the current location

Conditional Selectivity

XPath allows you to use conditional logic to navigate your xml document. Consider the following xml:

	<node name="first"/>
	<node name="second"/>

What kind of expression would you use to say “pick the node with the attribute named ‘first'”? This kind of logic is not only possible, but aslo can allow for extremely complicated xpath expressions. This is how to do it:

/xml/node[@name = 'first']

This literally means – get all the nodes in the xml where the /xml/node ‘s attribute named “name” has the value “first”. It will return ALL xml nodes that satisfy that requirement. The reason xpath expressions can become so complicated by this ability is that poorly constructed xml will force people to cross link attribute references between this branch and that so a really wordy xpath expression needs to be used to used as the selector.


This is my terrible segue way into some comments on xml! when constructing xml, please structure it as simply as possible with the forethought that xml should be extensible, as concise as possible, and xpath friendly as you can build it. Structure is the most important thing your xml schema should express and some genuine forethought and planning there will really pay off in the long run.

Advanced XPath

XPath has a lot of other commands that can be used for selecting nodes. I’ll list some interesting constructs below:

Select the node name “poll” with the namespace “ns”

Select all attributes named “id” for all the elements named “example”

Select the first poll node

Select all poll elements whose id is “100” and whose count is > 0:
example[@id = “100” and @count &gt; 0]
Notice it uses the html entity &amp;, this is because angle brackets break xml, specifically “>” breaks the current xml as the parser would not know it was text and not a “close node definition” instruction

Select the nearest poll ancestor of the current element:
if you drop the [1] from the expression, it seems to choose the farthest node

An example of XPath unions:
pollVote | pollVote/vote
this means pick all nodes that satisfy either selector expression “pollvote” or “pollVote/vote”

N-Tier architecture

History Lesson

Long, long ago.. in a galaxy far, far away… People once known as computer nerds started writing code… They walked 20 miles a day to get to the keyboard, and walked 20 miles more to copy a 3.5 inch floppy disk, remember those? 512k ram was huge! Well, back in those days people used to program applications with a procedural approach. Procedural programming was a logical, step by step approach to solving problems. Applications back then were much simpler, required much less complexity and were quite happy working within these boundaries.

Eventually, applications started to become interdependent, and grew degrees of magnitude in complexity. Data manipulation in various formats became central to portability. As new technologies, techniques and different information systems emerged, the importance of a pluggable, non monolithic approach to system architecture and design became more and more obvious, not just in hardware but also in the software itself.

Let me illustrate with this example – I’m going to say I have a server machine and it runs a single program that does everything. It serves web pages, keeps track of data, load balances, implements all my business logic, keeps track of customers that log on, EVERYTHING, on one machine. This is what I’d call “putting all your eggs in one basket.” If we learned anything from the 90s tech bubble, the housing bubble, the economic recession, it’s to diversify and do the exact opposite and not put all the eggs in one basket.


Because if anything were to happen – if there was a power surge, if the hard drive failed, if there was a bug in the software, if any one aspect of the application were to fail or go down for whatever reason, basically if ANYTHING went wrong, I’d be pretty much out of luck in trying to restore the system. There is no resilience because the system is far too tightly knit between all of its components for it to survive any kind stress outside a near perfect situation. To make things worse, if i wanted to upgrade system performance by updating one aspect, I’d have to be extra careful to not break the paradigm and compensate for all the interdependency. This becomes much more obvious the more complex the system becomes.

Think about it as if it were a studio apartment. If anything breaks in the studio, like a ceiling pipe that starts spewing water, then the whole room ends up flooding. This one pipe ends up affecting your living room, kitchen, bedroom, bathroom, everything because all of it is in one room. If you wanted to get new windows, or upgrade your electricity, or even paint the walls you’d have to figure out how you’re going to live with all the mess everywhere because everything is in one room. It’s not like a multi room house where you can partition off a room by closing the door and upgrade that one room without affecting the rest of the house Wait a minute, that sounds like a GREAT idea!

This concept is basically what n-tier architecture evolved into. The approach is to break out an application into an n number of tiers – essentially turning your one room studio apartment into a 6 room house, with each room in charge of one specialized task. N-tier architecture is usually broken up into 3 major sections: the presentation/front end tier, the business/application tier, and the data/backed tier. The image below is one example of a possible n-tier architecture layout:

N-Tier Architecture Overview

A simple overview of n-tier architecture

Presentation Tier

In the presentation tier, the software is written to present the user with the application. Front end frameworks like Spring MVC, Seam, Struts, and JSF, are only some of the frameworks that aim to provide an extensible, modern day front end architecture for web based applications written in java. The end result is usually some kind of html page with javascript that validates data or handles some kind of user interaction. Presentation tier frameworks usually provide some kind of validation framework, some kind of implementation of the Model-View-Controller design pattern that’s become so famous in software, and usually some sort of layer of code that integrates with the application tier, allowing for easy communication with deeper levels of the application. It’s important to note that although the presentation tier is off by itself its still able to be “torn out” and replaced with another front end framework. A well written application will allow for this kind of upgrade without having to rewrite the entire application back to front. Only upgrade the parts you want, not the whole thing. If the presentation tier were a house, it might be the paint, furniture, appliances, big screen TV, everything that makes a house easy to use and live in.

Business Tier

The business tier is essentially the glue that holds the entire application together. All the logic is abstracted and broken down into layers of logic or composable services (as an SOA architecture would promote) that hook into the front end via simple function calls. Application servers like jboss, websphere, weblogic, jetty and others deploy and run all the logic used by web based applications. If the business tier were a house, it would be the house’s frame, plumbing, electrical system – the infrastructure that makes the house work.

Data Tier

Lastly, the data tier is the tier where all the data used by the application lives. A relational database, xml datastore, data grid, a file on a computer, anything that is designed to store data for use by the application could be considered part of the data tier. Postgres, Oracle, MySQL, and Microsoft SQL Server are only some of the well known relational database system. There are plenty others out there that have some kind of use in most applications. If we follow the house metaphor, I think the data tier might be the house’s natural resources, the land, trees, roads, fences, grass.. These are things that persist after the house has been torn down. Its not a perfect metaphor but hopefully it makes some kind of sense.

Further reading: