XML,XSLT,XPath
XML,XSLT,XPath
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
How to refer a XSLT file in a XML Dcoment
Suppose You have a XML File Jungle.xml.

<?xml version="1.0"?>

<project>
  <title>The Xpath project</title>
<problems>
    <problem>
      <title>Initial problem</title>
      <description>We have to learn something about Location Path</description>
      <difficulty level="5">This problem should not be too hard</difficulty>
    </problem>
    <solutions>
      <item val="low">Buy a XSLT book</item>
      <item val="low">Find an XSLT website</item>
      <item val="high">Register for a XSLT course and do exercices</item>
    </solutions>
    <problem>
      <title>Next problem</title>
      <description>We have to learn something about predicates</description>
      <difficulty level="6">This problem is a bit more difficult</difficulty>
    </problem>
    <solutions>
      <item val="low">Buy a XSLT book</item>
      <item val="medium">Read the specification and do some exercises</item>
      <item val="high">Register for a XPath course and do exercices</item>
    </solutions>
  </problems>
</project>

You have an XSLT file called Jungle.xslt file.

<?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="/project">
    <html>
      <body bgcolor="#FFFFFF">
        <h1><xsl:value-of select="title" /></h1>
        Here are the titles of our problems: <ul>
        <xsl:apply-templates select="problems/problem" />
      </ul>
      </body>
    </html>
  </xsl:template>

<xsl:template match="problems/problem">
  <li><xsl:value-of select="title" /></li>
</xsl:template>

</xsl:stylesheet>

So how do you refer this xslt file in the xml file? Simple just add this one line under the <?xml version Tag just like this..

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

Now Open the xml file in IE without this stylesheet reference added and with added.You will see the difference.
53 Comments Filed Under [ XML,XSLT,XPath ]
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati