Jay Kimble
You may be wondering what’s so cool about XSLT. Well, I have used it as an AJAX technology that do some speedy reporting with filtering and sorting. I have used it to create multiple reports from the same XML. It really can be used to do all kinds of things, and it is in my opinion one of the key things that makes XML cool (XML isn’t really all that compelling to me by itself)...."> XSLT for the Uninitiated Part 1: Beginning Xpath - Jay Kimble -- The Dev Theologian - CodeBetter.Com - Stuff you need to Code Better!
 
Jay Kimble -- The Dev Theologian

Sponsors

The Lounge

Syndication

News

  • CodeBetter.Com Home
    Current Threat level
    Terror Alert Level

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
XSLT for the Uninitiated Part 1: Beginning Xpath

Why an XSLT Tutorial?
I have had a couple conversations with fellow programmers recently.  I was surprised to learn about their lack of knowedge in the area XSLT.  As a result I have decided to put together a few posts (I don’t know how many I’ll need to cover the material).  I will primarily be focusing on the XSLT 1.0 standard (but maybe we’ll do some exploring after this series).

You may be wondering what’s so cool about XSLT.  Well, I have used it as an AJAX technology that do some speedy reporting with filtering and sorting.  I have used it to create multiple reports from the same XML.  It really can be used to do all kinds of things, and it is in my opinion one of the key things that makes XML cool (XML isn’t really all that compelling to me by itself).

Before we can really talk about XSLT we need to talk about XPath.

What’s XPath?
Glad you asked! XPath is an XML selection language.  If you understand directory trees then you understand XPath… No really it’s true!.  Let’s start with a simple XML document that looks like this (pulling this out of the air, BTW)

<?xml version="1.0"?>
<root>
   <countries>
      <country region=”North America” type=”Preliminary” name=”Canada” sales=”34300” />
      <country region=”Europe” type=”Final” name=”Italy” sales=”35000” />
      <country region=”Europe” type=”Preliminary” name=”Sweden” sales=”36700” />
      <country region=”North America” type=”Final” name=”US” sales=”33200” />
   </countries>
</root>

So if I wanted to select (get) all the country nodes, the XPath would look like this:

/root/countries/country

So we are selecting country nodes because that’s what’s at the end of the XPath statement.  We want country nodes whose parent node is a countries node; the parent countries node must have a parent that is a root node.  BTW, when we select the country node, we are getting the country node and all it’s attributes and all it’s children (contained tags).

Now this is not the only XPath statement that will accomplish the desired affect for this XML.  It could also be written like this:

//country

Of course we’re selecting country nodes, but in the ‘//’ means that we are selecting any country node in the XML file (sometimes called the tree or document tree).  This works for our XML, but it might not have the desired affect in all XML files.

Ok, let’s try something a little more complicated.  Let’s select all the country nodes where the region is Europe.  We’ll use the shortened syntax (mainly because I don’t feel like typing that much):

//country[@region eq ‘Europe’]

The stuff between the square brackets are selection criteria, so we’re still selecting the complete country node, but we’re only getting country nodes with a region attribute (the ‘@’ symbol designates an attribute) that equals the string ‘Europe’ (BTW you can represent the “equals” other ways, but I have had more luck with using eq, lt, etc in XSLT… it all depends on your XSLT processor)

So let’s repeat the last one except let’s add two things.  First of all let’s select only Final numbers and let’s return just the sales attribute. 

//country[@region eq ‘Europe’ && @type eq ‘Final’]/@sales

So we added 2 things here.  First we added a second condition  and combined the 2 using the ‘&&.’  The other new item occurs after the square brackets: the ‘/@sales.’  This means that we are now selecting just the sales attribute. 

That’s it for now.


Posted 02-12-2006 9:49 PM by Jay Kimble
Filed under:

[Advertisement]