XSL for Xml to table transformation 2.0

时间:2012-10-30 04:53:54

标签: html xml xslt

跟进上一个问题XSL for Xml to table transformation 我一直致力于XSLT 2.0变体。我知道我在做一些根本错误的事情,但不确定是什么。提前感谢您的建议和指导。关心JJ。

输入:

<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 :(得分: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>