html - Modifying a XSLT XPATH code for a different output -


i need modify xslt code different output result. need output table sigle, number of students , average.

here's xml code :

<?xml version="1.0" encoding="iso-8859-1" ?> <?xml-stylesheet href="class.xsl" type="text/xsl" ?> <university> <student><sname>charlie parker</name> <course sigle="inf8430" note="69" /> <course sigle="inf1030" note="65" /> <course sigle="inf1230" note="73" /></student> <student><name>miles davis</name> <course sigle="inf8430" note="65" /> <course sigle="inf1030" note="77" /> <course sigle="inf1230" note="83" /></student> <student><name>john coltrane</name> <course sigle="inf9430" note="24" /> <course sigle="inf1030" note="64" /> <course sigle="inf1230" note="56" /></student> <student><name>charles mingus</name> <course sigle="inf8430" note="34" /> <course sigle="inf1230" note="89" /></student> </university> 

here's xslt code far :

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <xsl:template match="/university"> <html> <body> <table border="1"> <tr> <th>name</th> <th>average</th> </tr> <xsl:for-each select="student"> <xsl:sort select="substring-after(name, ' ')"/> <tr> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="sum(course/@note) div count(course)"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> 

here's output should :

desired table

thanks lot help!

your input document not well-formed, please careful when posting questions on stackoverflow.

the standard approach identify elements unique respect content or 1 of attributes use key. best explanation of still on jeni tennison's web page.

you consider using round() function if there's precision in average column.

xslt stylesheet

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>      <xsl:key name="course-sigle" match="course" use="@sigle"/>      <xsl:template match="/university">         <html>             <body>             <table border="1">                 <tr>                     <th>sigle</th>                     <th>number of students</th>                     <th>average</th>                 </tr>                 <xsl:for-each select="student/course[count(. | key('course-sigle', @sigle)[1]) = 1]">                      <xsl:variable name="count" select="count(key('course-sigle', @sigle))"/>                     <tr>                         <td>                             <xsl:value-of select="@sigle"/>                         </td>                         <td>                             <xsl:value-of select="$count"/>                         </td>                         <td>                             <xsl:value-of select="sum(key('course-sigle', @sigle)/@note) div $count"/>                         </td>                     </tr>                 </xsl:for-each>             </table>             </body>         </html>     </xsl:template>  </xsl:stylesheet> 

html output

<html>    <body>       <table border="1">          <tr>             <th>sigle</th>             <th>number of students</th>             <th>average</th>          </tr>          <tr>             <td>inf8430</td>             <td>3</td>             <td>56</td>          </tr>          <tr>             <td>inf1030</td>             <td>3</td>             <td>68.66666666666667</td>          </tr>          <tr>             <td>inf1230</td>             <td>4</td>             <td>75.25</td>          </tr>          <tr>             <td>inf9430</td>             <td>1</td>             <td>24</td>          </tr>       </table>    </body> </html> 

rendered html output

including round function, output looks like

enter image description here


besides, if in charge of designing xml document, note of names in either incomprehensible ("sigle") or inappropriate given context ("note"). proper terms "signature" , "grade", or "score".


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -