XPath Query
There are many tutorials explaining the syntax's of  XPathQuery. What gets me confused sometimes is the understanding of the match and select statements of the templates. Here is a simple example. The XML file that we are going to query is:

School.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Apply.xslt"?>

<School>
  <title>The Xpath Exercise</title>
  <people>
    <teacher>
      <FirstName>Henry</FirstName>
      <age>45</age>
      <description>Henry will be the tutor</description>
     </teacher>
     <student>
      <FirstName>Jack</FirstName>
      <age>15</age>
      <description> is an American</description>
      <FoodPref picture="dolores_001.jpg">Sea Food</FoodPref>
    </student>
    <student>
      <FirstName>Sorensen</FirstName>
      <age>16</age>
      <description> is a swedish</description>
      </student>
  </people>
  <furniture>
    <chair>
      <name>revolving chair</name>
    </chair>
  </furniture>
</School>




So we have a direct child of  root element <School>. School has two  child elements called <people> and <furniture> People has child elements called teacher and student. Now we only want to show the students.Their first name and description.Here is the XSLT file that we are going to use.

Apply.xslt

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:output method="html"/>

  <xsl:template match="/School">
    <html>
      <body bgcolor="#FFFFFF">
        <h1>What do we know about our students ?</h1>
        Here are some information <ul>
          <xsl:apply-templates select="people" />
        </ul>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="people">
          <xsl:apply-templates select="student"/>
   </xsl:template>
  <xsl:template match="student">
    <li>
      <xsl:value-of select="FirstName"/>
      <xsl:apply-templates select="description"/>
    </li>
  </xsl:template>
 
</xsl:stylesheet>

The syntax for locating the direct child t of root node is /XML_ElementName. Lets look at the beginning of  our first template.
<xsl:template match="/School">
 This defines the execution context of the template.It means the scope is the School element and the selection defined in this template will take effect on any school element in the output.

Now comes the important part.
<xsl:apply-templates select="people" />
This tells us that inside the school element, only the contents of the people element are going to be shown,so we are filtering out <furniture> here.

In the next template, we are increasing the level of filtering. Here the context is defined as <xsl:template match="people">.So this template is going to be applied on any people element in the output.So this is obviously going to be invoked by the first template call, as the first template is returning people element.The selection criteria here is
   <xsl:apply-templates select="student"/>
So any people element will be showing ony the student elements inside it, leaving out the teacher element that we have.

In the third step, we define a template for the student element itself. Till now all our endeavour was targeted at outputting a student element. So now we define how much information of the student we are going to show and how we want to show it. We want to show it as list elements.
<xsl:apply-templates select="description"/>
So we want to show the description element of the student. We also want to show the value of the FirstName element.This can be done with .
 <xsl:value-of select="FirstName"/>

So if you open the School.xml file the output you get should be:

4 Comments Filed Under [ XML,XSLT,XPath ]

Comments

# re: XPath Query
Gravatar Thanks for this. It certainly clears a few things up for me.
Left by Garry on 2/20/2009 4:15 PM
# re: XPath Query
Gravatar It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again. `1
Left by links of london Sweetie Bracelet on 10/19/2009 9:49 PM
# re: XPath Query
Gravatar Thanks for such a good post! i leanded more!
Left by gucci handbags on 10/22/2009 10:10 PM
# re: XPath Query
Gravatar I use Xpath Query very often. It's easy to use.
Left by Propane garage heaters on 1/23/2010 2:32 PM

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

 

Preview Your Comment.