2012-11-12 54 views
1

我還有另一個XSLT問題,它跟隨我上週提出的問題。 XSL for Xml to table transformation for two rows and many columns用於Xml的XSL:使用XSL插入特定的類

挑戰是根據section屬性爲每個類插入不同的類。我需要兩個不同的輸出,如下面的輸出中所示。我有一個來自我上一個問題的示例XSLT,我想對其進行相應編輯。

請注意我正在使用XSLT 1.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> 

輸出1:

<table> 
<tr> 
<td class="AFC">Arsenal</td> 
<td></td> 
<td class="CFC">Chelsea</td> 
<td></td> 
<td class="MUFC">ManUnited</td> 
<td></td> 
<td class="MCFC">ManCity</td> 
<td></td> 
</tr> 
<tr> 
<td>1</td> 
<td>6</td> 
<td>7</td> 
<td>12</td> 
<td>13</td> 
<td>18</td> 
<td>19</td> 
<td>24</td> 
</tr> 
</table> 

輸出2:

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

電流XSLT通過@Kirill舒克

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/> 
<xsl:key name="k" match="page" use="@section"/> 

<xsl:template match="/root"> 
<table> 
<tr> 
<xsl:apply-templates select="page[generate-id() = generate-id(key('k', @section))]"/> 
</tr> 
<tr> 
<xsl:apply-templates select="page[generate-id() = generate-id(key('k', @section))]" mode="page"/> 
</tr> 
</table> 
</xsl:template> 

<xsl:template match="page"> 
<td> 
<xsl:value-of select="."/> 
</td> 
<td></td> 
</xsl:template> 

<xsl:template match="page" mode="page"> 
<td> 
<xsl:value-of select="@number"/> 
</td> 
<td> 
<xsl:value-of select="key('k', @section)[last()]/@number"/> 
</td> 
</xsl:template> 

</xsl:stylesheet> 
提供

回答

1

使用此XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my"> 
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/> 

    <xsl:key name="k" match="page" use="@section"/> 

    <xsl:param name="short-name" select="true()"/> 
    <!-- Change param to false()--> 

    <my:data> 
    <club name="Arsenal">AFC</club> 
    <club name="Chelsea">CFC</club> 
    <club name="ManUnited">MUFC</club> 
    <club name="ManCity">MCFC</club> 
    </my:data> 

    <xsl:template match="/root"> 
    <table> 
     <tr> 
     <xsl:apply-templates select="page[generate-id() = generate-id(key('k', @section))]"/> 
     </tr> 
     <tr> 
     <xsl:apply-templates select="page[generate-id() = generate-id(key('k', @section))]" mode="page"/> 
     </tr> 
    </table> 
    </xsl:template> 

    <xsl:template match="page"> 
    <xsl:variable name="class"> 
     <xsl:choose> 
     <xsl:when test="$short-name"> 
      <xsl:value-of select="document('')/*/*/club[@name = current()/@section]"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="@section"/> 
     </xsl:otherwise> 
     </xsl:choose> 

    </xsl:variable> 

    <td class="{$class}"> 
     <xsl:value-of select="."/> 
    </td> 
    <td></td> 
    </xsl:template> 

    <xsl:template match="page" mode="page"> 
    <td> 
     <xsl:value-of select="@number"/> 
    </td> 
    <td> 
     <xsl:value-of select="key('k', @section)[last()]/@number"/> 
    </td> 
    </xsl:template> 

</xsl:stylesheet> 

根據參數$short-name它使你的1 ST所需的XML或2 第二

+0

再次感謝@Kirill Polishchuk。看起來很複雜,會逐行去理解它。 –

+0

@ jay.james.666,不客氣! –