2012-10-30 68 views
0

A跟隨上一個問題XSL for Xml to table transformation 我一直致力於XSLT 2.0的變體。我知道我正在做一些根本性錯誤,但不知道是什麼。在此先感謝您的建議和指導。關心JJ。XSL for Xml to table transformation 2.0

輸入:

<root> 
    <page number="1" section="Arsenal">Arsenal</page> 
    <page number="2" section="Arsenal">Arsenal</page> 
    <page number="3" section="Arsenal">Arsenal</page> 
    <page number="4" section="Arsenal">Arsenal</page> 
    <page number="5" section="Arsenal">Arsenal</page> 
    <page number="6" section="Arsenal">Arsenal</page> 
    <page number="7" section="Chelsea">Chelsea</page> 
    <page number="8" section="Chelsea">Chelsea</page> 
    <page number="9" section="Chelsea">Chelsea</page> 
    <page number="10" section="Chelsea">Chelsea</page> 
    <page number="11" section="Chelsea">Chelsea</page> 
    <page number="12" section="Chelsea">Chelsea</page> 
    <page number="13" section="ManUnited">ManUnited</page> 
    <page number="14" section="ManUnited">ManUnited</page> 
    <page number="15" section="ManUnited">ManUnited</page> 
    <page number="16" section="ManUnited">ManUnited</page> 
    <page number="17" section="ManUnited">ManUnited</page> 
    <page number="18" section="ManUnited">ManUnited</page> 
    <page number="19" section="ManCity">ManCity</page> 
    <page number="20" section="ManCity">ManCity</page> 
    <page number="21" section="ManCity">ManCity</page> 
    <page number="22" section="ManCity">ManCity</page> 
    <page number="23" section="ManCity">ManCity</page> 
    <page number="24" section="ManCity">ManCity</page> 
</root> 

所需的輸出

<tr><td>1</td><td>Arsenal</td><td>6</td></tr> 
<tr><td>7</td><td>Chelsea</td><td>12</td></tr> 
<tr><td>13</td><td>ManU</td><td>18</td></tr> 
<tr><td>19</td><td>Mancity</td><td>24</td></tr> 

電流XSL

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

    <xsl:output omit-xml-declaration="no" indent="yes" /> 
    <xsl:strip-space elements="*" /> 

    <xsl:template match="/root"> 
    <table> 
     <xsl:for-each-group select="page" group-by="@section"> 
      <tr> 
     <td> 
     <xsl:copy-of select="@number"/> 
     </td> 
     <td> 
     <xsl:copy-of select="@section"/> 
     </td> 
     <td> 
     <xsl:copy-of select=".|current-group()[last()]"/> 
     </td> 
    </tr> 
     </xsl:for-each-group> 
    </table> 
    </xsl:template> 

</xsl:stylesheet> 

電流輸出

<table> 
    <tr> 
     <td number="1"/> 
     <td section="Arsenal"/> 
     <td> 
      <page number="1" section="Arsenal">Arsenal</page> 
      <page number="6" section="Arsenal">Arsenal</page> 
     </td> 
    </tr> 
    <tr> 
     <td number="7"/> 
     <td section="Chelsea"/> 
     <td> 
      <page number="7" section="Chelsea">Chelsea</page> 
      <page number="12" section="Chelsea">Chelsea</page> 
     </td> 
    </tr> 
    <tr> 
     <td number="13"/> 
     <td section="ManUnited"/> 
     <td> 
      <page number="13" section="ManUnited">ManUnited</page> 
      <page number="18" section="ManUnited">ManUnited</page> 
     </td> 
    </tr> 
    <tr> 
     <td number="19"/> 
     <td section="ManCity"/> 
     <td> 
      <page number="19" section="ManCity">ManCity</page> 
      <page number="24" section="ManCity">ManCity</page> 
     </td> 
    </tr> 
</table> 
+1

您目前的輸出似乎與您的輸入沒有任何關係。很難看到發生了什麼問題。 –

+0

謝謝@Cheran Shunmugavel輸出已被修復。 –

回答

1
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output omit-xml-declaration="no" indent="yes"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="/root"> 
     <table> 
      <xsl:for-each-group select="page" group-by="@section"> 
       <tr> 
        <td> 
         <xsl:value-of select="@number"/> 
        </td> 
        <td> 
         <xsl:value-of select="@section"/> 
        </td> 
        <td> 
         <xsl:value-of select="current-group()[last()]/@number"/> 
        </td> 
       </tr> 
      </xsl:for-each-group> 
     </table> 
    </xsl:template> 
</xsl:stylesheet>