Geeks With Blogs
Tanveer Ibn Haresh
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:

Posted on Wednesday, February 18, 2009 12:39 PM XML,XSLT,XPath | Back to top

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: XPath Query

# re: XPath Query
Requesting Gravatar...
Thanks for this. It certainly clears a few things up for me.
Left by Garry on Feb 20, 2009 4:15 PM

# re: XPath Query
Requesting Gravatar...
I use Xpath Query very often. It's easy to use.
Left by Propane garage heaters on Jan 23, 2010 2:32 PM

# re: XPath Query
Requesting Gravatar...
Xpath is very helpful tool. I use it in web interfaces.
Left by goji berries on Feb 23, 2010 4:55 AM

# re: XPath Query
Requesting Gravatar...
The application's other features include dynamic querying, the ability to preview files while downloading, advanced techniques for locating rare files, and an extremely intuitive user interface.
Left by primes des jeux du casino on Mar 04, 2010 4:10 AM

# re: XPath Query
Requesting Gravatar...
Nice and thanks for given information to us.
Your site always has some good info to share..got you bookmarked..Thanks!
discount Longines watches
Left by discount Longines watches on May 25, 2010 2:48 AM

# re: XPath Query
Requesting Gravatar...
this was very useful for me for my university task , it helped me a lot ! thanks a bunch
Left by stock charts on Aug 01, 2010 9:21 PM

# re: XPath Query
Requesting Gravatar...
interesting article
Left by easy-prestito on Aug 30, 2010 10:57 AM

# re: XPath Query
Requesting Gravatar...
nice tutorial. I'll try it
Left by gabby pierzon on Sep 06, 2010 10:14 PM

# re: XPath Query
Requesting Gravatar...
Xpath is friendly-use for me. But with this post, it will become more easier for me
Left by almendes on Sep 06, 2010 10:16 PM

# re: XPath Query
Requesting Gravatar...
Nice write up thanks for sharing. I really appreciate your work keep it up.
Thanks for post. It is really informative stuff.
Left by Cash Payday Loan on Dec 09, 2010 4:20 AM

# re: XPath Query
Requesting Gravatar...
Great post.It would be great if you can provide more details about it.
Left by Payday Loans No Paper Work on Dec 09, 2010 4:27 AM

# re: XPath Query
Requesting Gravatar...
Very informative and helpful post. You have good command on the topic and have explained in a very nice way.
Left by Forex Trading on Dec 09, 2010 11:17 AM

# re: XPath Query
Requesting Gravatar...
This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. Youve got a design here thats not too flashy, but makes a statement as big as what youre saying. Great job, indeed. Cheap Cigarettes
Left by Cheap Cigarettes on Dec 22, 2010 4:18 AM

# re: XPath Query
Requesting Gravatar...
This really works. I tried it. This is very good article. this information really very helpful. Thanks for your efforts.
Left by payday loans fast on Dec 23, 2010 1:42 AM

# re: XPath Query
Requesting Gravatar...
Wow, this is very interesting blog. i would like to say thanks to you.
Left by quick loans on Dec 31, 2010 4:31 AM

# re: XPath Query
Requesting Gravatar...
Nice posting.
Left by bowflag on Jan 03, 2011 1:06 AM

# re: XPath Query
Requesting Gravatar...
Nice work! Great range of subjects ...enjoyed your site!
Left by business intranet on Jan 06, 2011 4:18 AM

# re: XPath Query
Requesting Gravatar...
hello .you got great website :) thanks for share.
Left by chiang mai travel info on Jan 07, 2011 1:31 AM

# re: XPath Query
Requesting Gravatar...
Finally, an issue that I am passionate about. I have looked for information of this caliber for the last several hours. This site is greatly appreciated.
Left by Financial Planning on Jan 11, 2011 9:54 PM

# re: XPath Query
Requesting Gravatar...
Fantastic post. Thank you very much for the whole information. It helped me a lot. I was little bit aware about it but your post gave me clear idea.
Left by Quick Loans Payday on Jan 14, 2011 11:32 PM

# re: XPath Query
Requesting Gravatar...
I am very enjoyed for this blog. Its an informative topic. It help me very much to solve some problems. Its opportunity are so fantastic and working style so speedy. I think it may be help all of you. Thanks a lot for enjoying this beauty blog with me. I am appreciating it very much! Looking forward to another great blog. Good luck to the author! all the best!
Left by free online racing games on Feb 04, 2011 7:37 AM

# re: XPath Query
Requesting Gravatar...
Interesting post and thanks for sharing. Some things in here I have not thought about before.Thanks for making such a cool post which is really very well written.will be referring a lot of friends about this.Keep blogging
Left by cheap college textbooks on Feb 04, 2011 11:45 AM

# re: XPath Query
Requesting Gravatar...
I’m happy to have found your very excellent article! I agree with some of your readers and will eagerly look forward to your coming updates. Just saying thanks will not just be adequate
Left by premarital counseling nyc on Feb 06, 2011 7:38 AM

# re: XPath Query
Requesting Gravatar...
Such clever work and reporting! Keep up the great works guys I've added you guys to my blog roll. This is a great article thanks for sharing this informative information. I will visit your blog regularly for some latest post.
Left by ossf design on Feb 26, 2011 7:27 AM

# re: XPath Query
Requesting Gravatar...
It is an interesting approach. I commonly see nothing exceptional views on the subject, but yours is written in a very unusual way. No doubt I will get back to your website for additional information.
Left by Property management MN on Mar 05, 2011 7:37 AM

# re: XPath Query
Requesting Gravatar...
What gets me confused sometimes is the understanding of the match and select statements of the templates.You had made it simple to me.Thanks with regards.
Left by Used Audi on Mar 11, 2011 6:27 AM

# re: XPath Query
Requesting Gravatar...
This is a great blog with excellent posts and links.
Thanks for sharing.
Left by mothers day flowers Spain on Mar 12, 2011 1:11 AM

# re: XPath Query
Requesting Gravatar...
The particular design can be interesting, I may take part in discussion.
Left by kendall howard on Mar 12, 2011 2:02 AM

# Dr
Requesting Gravatar...
Hey thanks for sharing this information...have been looking all over yahoo and google and could not locate a reasonable write-up for this.
Left by patent leather corset on Mar 12, 2011 5:41 AM

# re: XPath Query
Requesting Gravatar...
I usually do self-hosting for my WCF services, but on a project I am working on we wanted to host in IIS.
Left by Duck hunting on Mar 21, 2011 8:49 AM

# liajames
Requesting Gravatar...
Your work is very good and I appreciate you and hopping for some more informative posts. Thank you for sharing great information to us.
Left by sending flowers Canada on Mar 29, 2011 6:02 AM

# re: XPath Query
Requesting Gravatar...
query is an inquiry from a writer to an editor of a magazine, newspaper, etc., regarding the acceptability of or interest. It is very important to everyone of us so that we are updated to the current events. Don't be confused it will just be focus.
Left by cebu city on Jul 30, 2012 9:10 PM

# re: XPath Query
Requesting Gravatar...
Now this query is very helpful. I tried creating this several times but it didn't work out on the first place.
Left by pmp exam quick reference guide on Jul 31, 2012 5:45 AM

Your comment:
 (will show your gravatar)


Copyright © Tanveer-Ibn-Haresh | Powered by: GeeksWithBlogs.net