Quackit Logo
FREE Hosting!
With every domain you register with ZappyHost you get FREE hosting.
Create free Flash websites

XPath Location Path - Relative

Print Version

Now that we've looked at absolute location paths, let's look at relative location paths.

A relative location path is one where the path starts from the node of your choosing - it doesn't need to start from the root node. This can reduce the amount of code you need to write - especially if you need to select many nodes that share the same name.

Example of a Relative Location Path

Consider the following XML document:

<albums>
  <rock>
    <title>Machine Head</title>
    <artist>Deep Purple</artist>
  </rock>
  <blues>
    <title>Greens From The Garden</title>
    <artist>Cory Harris</artist>
  </blues>
  <country>
    <title>The Ranch</title>
    <artist>The Ranch</artist>
  </country>
</albums>

If we wanted to select the "title" node of all albums, we could use the following (relative) location path:

title

The Result

This single line of code has exactly the same result as the example in the previous lesson. The only difference is that, in the previous lesson, we needed 3 lines of code to provide the same result.

So to just to make sure you understand what this line of code is doing, it is selecting all title nodes within our XML document. We don't need to provide the full path - just the name of the node we need to work with. This makes our like easier and keeps our code nice and clean.

<albums>
  <rock>
    <title>Machine Head</title>
    <artist>Deep Purple</artist>
  </rock>
  <blues>
    <title>Greens From The Garden</title>
    <artist>Cory Harris</artist>
  </blues>
  <country>
    <title>The Ranch</title>
    <artist>The Ranch</artist>
  </country>
</albums>

Children

We can also select a node's children using relative location paths.

Example 1 - Selecting the two children of the "rock" node ("title" and "artist"). The context node is "rock", because that's where our relative path starts:

rock/title
rock/artist

Example 2 - Using a wildcard to select all children of the "rock" node. This (single line of code) has the same result as the above two lines of code. Further, if another node was added to the XML document under the "rock" node, it would be automatically included using the wildcard:

rock/*

About the Wildcard

If you don't know what I mean by "wildcard", it is represented by the asterisk (*). The wildcard represents any node that would be located where the wildcard is positioned. Therefore, using our example, it is representing any node that comes under the "rock" node.

Wildcards don't have to appear at the end of a location path - they can also appear in the middle of a location path. We aren't limited to just one either - we could use as many as we like within a location path.

Enjoy this website?

  1. Add this page to your Favorites
  2. Link to this page (copy/paste into your own website or blog):
  3. Help support Quackit by making a donation

Oh, and thank you for supporting Quackit!

Need Website Content?
Get unique, quality digital content for your website.
  • 270+ Website Templates
  • 800+ Flash Templates
  • 25,000+ Images, Logos
  • 30,000+ e-Books
  • 15,000+ Scripts
  • 27,000+ Animated GIFs
  • 21,000+ Ringtones
  • ...and much more!
Get your content now!
$1.99 .info domain names!
© Copyright 2000 - 2010 Quackit.com