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:

33 Comments Filed Under [ XML,XSLT,XPath ]
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

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 I use Xpath Query very often. It's easy to use.
Left by Propane garage heaters on 1/23/2010 2:32 PM
# re: XPath Query
Gravatar Xpath is very helpful tool. I use it in web interfaces.
Left by goji berries on 2/23/2010 4:55 AM
# re: XPath Query
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 3/4/2010 4:10 AM
# re: XPath Query
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 5/25/2010 2:48 AM
# re: XPath Query
Gravatar this was very useful for me for my university task , it helped me a lot ! thanks a bunch
Left by stock charts on 8/1/2010 9:21 PM
# re: XPath Query
Gravatar interesting article
Left by easy-prestito on 8/30/2010 10:57 AM
# re: XPath Query
Gravatar nice tutorial. I'll try it
Left by gabby pierzon on 9/6/2010 10:14 PM
# re: XPath Query
Gravatar Xpath is friendly-use for me. But with this post, it will become more easier for me
Left by almendes on 9/6/2010 10:16 PM
# re: XPath Query
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 12/9/2010 4:20 AM
# re: XPath Query
Gravatar Great post.It would be great if you can provide more details about it.
Left by Payday Loans No Paper Work on 12/9/2010 4:27 AM
# re: XPath Query
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 12/9/2010 11:17 AM
# re: XPath Query
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 12/22/2010 4:18 AM
# re: XPath Query
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 12/23/2010 1:42 AM
# re: XPath Query
Gravatar Wow, this is very interesting blog. i would like to say thanks to you.
Left by quick loans on 12/31/2010 4:31 AM
# re: XPath Query
Gravatar Nice posting.
Left by bowflag on 1/3/2011 1:06 AM
# re: XPath Query
Gravatar Nice work! Great range of subjects ...enjoyed your site!
Left by business intranet on 1/6/2011 4:18 AM
# re: XPath Query
Gravatar hello .you got great website :) thanks for share.
Left by chiang mai travel info on 1/7/2011 1:31 AM
# re: XPath Query
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 1/11/2011 9:54 PM
# re: XPath Query
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 1/14/2011 11:32 PM
# re: XPath Query
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 2/4/2011 7:37 AM
# re: XPath Query
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 2/4/2011 11:45 AM
# re: XPath Query
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 2/6/2011 7:38 AM
# re: XPath Query
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 2/26/2011 7:27 AM
# re: XPath Query
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 3/5/2011 7:37 AM
# re: XPath Query
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 3/11/2011 6:27 AM
# re: XPath Query
Gravatar This is a great blog with excellent posts and links.
Thanks for sharing.
Left by mothers day flowers Spain on 3/12/2011 1:11 AM
# re: XPath Query
Gravatar The particular design can be interesting, I may take part in discussion.
Left by kendall howard on 3/12/2011 2:02 AM
# Dr
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 3/12/2011 5:41 AM
# re: XPath Query
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 3/21/2011 8:49 AM
# liajames
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 3/29/2011 6:02 AM

Leave Your Comment

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

 

Preview Your Comment.