codeRed
codeRed

Reputation: 53

XSLT 2.0 transformation not generating proper html document

I am trying to use Saxon XSLT 2.0 simple transform (using Exchanger XML Editor) to generate an HTML file from an XML and an XSL document, but the data from the XML file is not getting picked up. I am assuming I have errors in my XSL stylesheet, but I am not sure where.

I have uploaded all 4 files (1 XSL, 1 XML, 1 CSS, and 1 PNG) that are needed to create the complete HTML document here... https://drive.google.com/open?id=0B9o30hEqwvyDSjRsbWlZVUpyNzA

Here is my XSL stylesheet (which is the same as the one included in the link above)...

<?xml version="1.0" encoding="UTF-8" ?>


<xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="html"
      doctype-system="about:legacy-compat"
      encoding="UTF-8"
      indent="yes" />

   <xsl:template match="/">
      <html>
         <head>
            <title>Employment Report</title>
            <link href="horizons.css" rel="stylesheet" type="text/css" />
         </head>

         <body>
            <div id="wrap">
               <header>
                  <img src="horizons.png" alt="Horizons TechNet" />
               </header>

               <h1>Employment Report</h1>
               <xsl:for-each-group select="employee" group-by="gender">

                   <table id="summary">
                       <tr>
                           <th>Gender</th>
                           <td>
                               <xsl:value-of select="current-grouping-key()" />
                           </td>
                       </tr>
                       <tr>
                           <th>Employees</th>
                           <td>
                               <xsl:value-of select="count(current-group())" /> 
                           </td>
                       </tr>
                       <tr>
                           <th>Average Compensation</th>
                           <td> 
                              <xsl:value-of select="avg(current-group()//salary+bonus+commission)"/>
                           </td>
                       </tr>
                   </table>


                   <table id="emptable">
                        <tr>
                            <th>ID</th>
                            <th>Department</th>
                            <th>Education Level</th>
                            <th>Total Compensation</th>
                        </tr>

                    <xsl:apply-templates select="employee">
                        <xsl:sort select="sum(salary+bonus+commission)" order="descending" data-type="number" />
                    </xsl:apply-templates>

                    </table>

               </xsl:for-each-group>

             </div>
         </body>

      </html>
   </xsl:template>

<xsl:template name="employee">
    <tr>
        <td><xsl:value-of select="@empID" /></td>
        <td><xsl:value-of select="department" /></td>
        <td><xsl:value-of select="title" /></td>
        <td><xsl:value-of select="edLevel" /></td>
        <td><xsl:value-of select="sum(salary+bonus+commission)" /></td>
    </tr>
</xsl:template>   


</xsl:stylesheet>

Upvotes: 0

Views: 480

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167726

Use <xsl:for-each-group select="//employee" group-by="gender">, for a start. And I guess you want <xsl:apply-templates select="current-group ()"> instead of <xsl:apply-templates select="employee">

Additionally you have a name instead of a match attribute on the last template and in some places you use + and then sum, I guess you either want to sum items using sum or + but not both:

<?xml version="1.0" encoding="UTF-8" ?>



<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html"
        doctype-system="about:legacy-compat"
        encoding="UTF-8"
        indent="yes" />

    <xsl:template match="/">
        <html>
            <head>
                <title>Employment Report</title>
                <link href="horizons.css" rel="stylesheet" type="text/css" />
            </head>

            <body>
                <div id="wrap">
                    <header>
                        <img src="horizons.png" alt="Horizons TechNet" />
                    </header>

                    <h1>Employment Report</h1>
                    <xsl:for-each-group select="//employee" group-by="gender">

                        <table id="summary">
                            <tr>
                                <th>Gender</th>
                                <td>
                                    <xsl:value-of select="current-grouping-key()" />
                                </td>
                            </tr>
                            <tr>
                                <th>Employees</th>
                                <td>
                                    <xsl:value-of select="count(current-group())" /> 
                                </td>
                            </tr>
                            <tr>
                                <th>Average Compensation</th>
                                <td> 
                                    <xsl:value-of select="avg(current-group()/sum(salary | bonus |commission))"/>
                                </td>
                            </tr>
                        </table>


                        <table id="emptable">
                            <tr>
                                <th>ID</th>
                                <th>Department</th>
                                <th>Education Level</th>
                                <th>Total Compensation</th>
                            </tr>

                            <xsl:apply-templates select="current-group()">
                                <xsl:sort select="sum(salary | bonus | commission)" order="descending"/>
                            </xsl:apply-templates>

                        </table>

                    </xsl:for-each-group>

                </div>
            </body>

        </html>
    </xsl:template>

    <xsl:template match="employee">
        <tr>
            <td><xsl:value-of select="@empID" /></td>
            <td><xsl:value-of select="department" /></td>
            <td><xsl:value-of select="title" /></td>
            <td><xsl:value-of select="edLevel" /></td>
            <td><xsl:value-of select="sum(salary | bonus | commission)" /></td>
        </tr>
    </xsl:template>   


</xsl:stylesheet>

Upvotes: 1

Related Questions