xslt - problem displaying variable (node) values in a table

Hi Experts,

I have the following xml file:
<?xml version="1.0" encoding="UTF-8"?>
<?xsl:stylesheet type="text/xsl" href="Employees.xsl"?>

 <Employees>
     <Employee id="1">
         <age>42</age>
         <name>Martina</name>
         <gender>Female</gender>
         <role>Cashier</role>
       <distance unit="m">1300.0</distance>
     </Employee>
       <Employee id="2">
         <age>30</age>
         <name>Soletho</name>
         <gender>Male</gender>
         <role>Tester</role>
       <distance unit="m">7203.5</distance>
     </Employee>
       <Employee id="3">
         <age>19</age>
         <name>Tom</name>
         <gender>Male</gender>
         <role>Manager</role>
       <distance unit="m">9042.0</distance>
     </Employee>
     <Employee id="4">
         <age>25</age>
         <name>Meghna</name>
         <gender>Female</gender>
         <role>Clerk</role>
       <distance unit="m">4100.0</distance>
     </Employee>
       <Employee id="5">
         <age>42</age>
         <name>Martha</name>
         <gender>Female</gender>
         <role>Cashier</role>
       <distance unit="m">3000.0</distance>
     </Employee>
       <Employee id="6">
         <age>31</age>
         <name>Sindrathi</name>
         <gender>Male</gender>
         <role>Tester</role>
       <distance unit="m">2300.5</distance>
     </Employee>
       <Employee id="7">
         <age>19</age>
         <name>Timothy</name>
         <gender>Male</gender>
         <role>Manager</role>
       <distance unit="m">13950.0</distance>
     </Employee>
     <Employee id="8">
         <age>25</age>
         <name>Solange</name>
         <gender>Female</gender>
         <role>Clerk</role>
       <distance unit="m">9975.0</distance>
     </Employee>
 </Employees>

I want to display the information about the high performing employee at the corporate athletics/running event.

Here is what my xslt file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <h1>Top Runner</h1>
        <table border="1">
          <tr bgcolor="#4000FF">
            <th>Age</th>
            <th>Name</th>
            <th>Gender</th>
            <th>Role</th>
            <th>Distance</th>
           </tr>
         
          <xsl:variable name="top_runner" select="/Employees/Employee[not(number(distance) &lt; ../Employee/distance)]/*" />
          <xsl:for-each select="$top_runner" >
          <tr>
                <td>
                  <xsl:value-of select="Age" />
                </td>
                <td>
                  <xsl:value-of select="Name" />
                </td>
               <td>
                  <xsl:value-of select="Gender" />
                </td>
                <td>
                  <xsl:value-of select="Role" />
                </td>
                <td>
                  <xsl:value-of select="Distance" />
                </td>
           </tr>
          </xsl:for-each>          
         </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Using the xpath expression, I do the sorting and attribute the high performing employee to the variable top_runner.
I want to be able to display the information in the variable in a table.
So far, I only get the header ...
I am surely missing something but I'm not sure what !
I'm quite new to xpath and xslt, altogether so would much appreciate your help.

Thanks.
Smanyx
SmanyxAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gertone (Geert Bormans)Information ArchitectCommented:
two comments

- element names in XML are case sensitive
- in the variable you had the level wrong

you are trying to find "Age" as a child of a child of Employee
but you should b elooking for "age" as a direct child of Employee

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h1>Top Runner</h1>
                <table border="1">
                    <tr bgcolor="#4000FF">
                        <th>Age</th>
                        <th>Name</th>
                        <th>Gender</th>
                        <th>Role</th>
                        <th>Distance</th>
                    </tr>
                    
                    <xsl:variable name="top_runner" select="/Employees/Employee[not(number(distance) &lt; ../Employee/distance)]" />
                    <xsl:for-each select="$top_runner" >
                        <tr>
                            <td>
                                <xsl:value-of select="age" />
                            </td>
                            <td>
                                <xsl:value-of select="name" />
                            </td>
                            <td>
                                <xsl:value-of select="gender" />
                            </td>
                            <td>
                                <xsl:value-of select="role" />
                            </td>
                            <td>
                                <xsl:value-of select="distance" />
                            </td>
                        </tr>
                    </xsl:for-each>          
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Open in new window

Gertone (Geert Bormans)Information ArchitectCommented:
BUT

using a variable is never a good idea if you don't need to,
before you know you have a Result Tree Fragment instead of a node-set
and then you can't use the XPaths inside the Result Tree Fragment

Here is what I would do (cutting the for-each as well)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h1>Top Runner</h1>
                <table border="1">
                    <tr bgcolor="#4000FF">
                        <th>Age</th>
                        <th>Name</th>
                        <th>Gender</th>
                        <th>Role</th>
                        <th>Distance</th>
                    </tr>
                    <xsl:apply-templates select="/Employees/Employee[not(number(distance) &lt; ../Employee/distance)]"></xsl:apply-templates>
                 </table>
            </body>
        </html>
    </xsl:template>
    
    
    <xsl:template match="Employee" >
        <tr>
            <td>
                <xsl:value-of select="age" />
            </td>
            <td>
                <xsl:value-of select="name" />
            </td>
            <td>
                <xsl:value-of select="gender" />
            </td>
            <td>
                <xsl:value-of select="role" />
            </td>
            <td>
                <xsl:value-of select="distance" />
            </td>
        </tr>
    </xsl:template>          
    
</xsl:stylesheet>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SmanyxAuthor Commented:
Thank you. Much appreciated.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.