2015-04-01 23 views
0

我需要修改我的XSLT代碼以獲得不同的輸出結果。我需要輸出一個包含學號和平均數的表格。爲不同的輸出修改XSLT XPATH代碼

這裏是我的XML代碼:

<?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> 

這裏是我的XSLT代碼至今:

<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> 

這裏的輸出應該是什麼樣子:

Desired Table

非常感謝您的幫助!

+0

「INF9430」課程中只有一個人註冊 - 爲什麼平均「39.0」?另外,你應該照顧你之前的一些問題。要麼接受或以其他方式對給出的[這裏](http://stackoverflow.com/a/28753643/1987598)和[這裏](http://stackoverflow.com/a/28998433/1987598)給出的答案作出反應。 – 2015-04-01 14:17:06

回答

1

您輸入的文檔格式不正確,請在Stackoverflow上發佈問題時小心。

標識關於其內容或其屬性的唯一元素的標準方法是使用密鑰。對此的最佳解釋仍在Jeni Tennison's web page

如果平均列中的精度太高,您也可以考慮使用round()函數。

XSLT樣式表

<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輸出

<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> 

呈現的HTML輸出

包括圓功能,輸出看起來像

enter image description here


此外,如果你是負責這個XML文檔設計的,注意,有些在它的名字或者是不可理解的(「事務所」)或不適當給出的上下文( 「注意」)。適當的條款將是「簽名」和「等級」或「分數」。

+0

非常感謝!我會修復我的代碼! – Sebastian 2015-04-01 14:22:59