2013-02-01 117 views
-1

對於具有固定結構和可變輸入內容的輸出表,我有一個關於XSLT轉換的問題。將XSLT轉換爲固定表結構

我已經概述了2個不同的例子。

所需的輸出表是6列和兩行。

但所需的TD類和內容依賴於輸入XML

我前面的例子中,開始從@Kirill舒克提供

Extracting a class from the section attribute using xsl

請注意我的工作XSLT 1.0。任何建議或指導將非常感激。

實施例1

輸入XML用6個部分

<root> 
    <page number="1" section="Arsenal_Stadium">Arsenal_Stadium</page> 
    <page number="2" section="Arsenal_Stadium">Arsenal_Stadium</page> 
    <page number="3" section="Arsenal_Stadium">Arsenal_Stadium</page> 
    <page number="4" section="Arsenal_Stadium">Arsenal_Stadium</page> 
    <page number="5" section="Arsenal_Crowds">Arsenal_Crowds</page> 
    <page number="6" section="Arsenal_Crowds">Arsenal_Crowds</page> 
    <page number="7" section="Arsenal_Crowds">Arsenal_Crowds</page> 
    <page number="8" section="Arsenal_Crowds">Arsenal_Crowds</page> 
    <page number="9" section="Arsenal_Support">Arsenal_Support</page> 
    <page number="10" section="Arsenal_Support">Arsenal_Support</page> 
    <page number="11" section="Arsenal_Support">Arsenal_Support</page> 
    <page number="12" section="Arsenal_Support">Arsenal_Support</page> 
    <page number="13" section="Arsenal_Revenue">Arsenal_Revenue</page> 
    <page number="14" section="Arsenal_Revenue">Arsenal_Revenue</page> 
    <page number="15" section="Arsenal_Revenue">Arsenal_Revenue</page> 
    <page number="16" section="Arsenal_Revenue">Arsenal_Revenue</page> 
    <page number="17" section="Arsenal_Cost">Arsenal_Cost</page> 
    <page number="18" section="Arsenal_Cost">Arsenal_Cost</page> 
    <page number="19" section="Arsenal_Cost">Arsenal_Cost</page> 
    <page number="20" section="Arsenal_Cost">Arsenal_Cost</page> 
    <page number="21" section="Arsenal_Outlook">Arsenal_Outlook</page> 
    <page number="22" section="Arsenal_Outlook">Arsenal_Outlook</page> 
    <page number="23" section="Arsenal_Outlook">Arsenal_Outlook</page> 
    <page number="24" section="Arsenal_Outlook">Arsenal_Outlook</page> 
</root> 

所需的輸出6行& 2列

<table> 
<tr> 
<td class="Stadium">Stadium</td> 
<td class="Crowds">Crowds</td> 
<td class="Support">Support</td> 
<td class="Revenue">Revenue</td> 
<td class="Cost">Cost</td> 
<td class="Outlook">Outlook</td> 
</tr> 
<tr> 
<td class="Stadium_R2">1-4</td> 
<td class="Crowds_R2">5-6</td> 
<td class="Support_R2">7-10</td> 
<td class="Revenue_R2">11-14</td> 
<td class="Cost_R2">15-18</td> 
<td class="Outlook_R2">19-22</td> 
</tr> 
</table> 

實施例2

輸入XML 4段

<root> 
    <page number="1" section="Arsenal_Stadium">Arsenal_Stadium</page> 
    <page number="2" section="Arsenal_Stadium">Arsenal_Stadium</page> 
    <page number="3" section="Arsenal_Stadium">Arsenal_Stadium</page> 
    <page number="4" section="Arsenal_Stadium">Arsenal_Stadium</page> 
    <page number="5" section="Arsenal_Support">Arsenal_Support</page> 
    <page number="6" section="Arsenal_Support">Arsenal_Support</page> 
    <page number="7" section="Arsenal_Support">Arsenal_Support</page> 
    <page number="8" section="Arsenal_Support">Arsenal_Support</page> 
    <page number="9" section="Arsenal_Cost">Arsenal_Cost</page> 
    <page number="10" section="Arsenal_Cost">Arsenal_Cost</page> 
    <page number="11" section="Arsenal_Cost">Arsenal_Cost</page> 
    <page number="12" section="Arsenal_Cost">Arsenal_Cost</page> 
    <page number="13" section="Arsenal_Outlook">Arsenal_Outlook</page> 
    <page number="14" section="Arsenal_Outlook">Arsenal_Outlook</page> 
    <page number="15" section="Arsenal_Outlook">Arsenal_Outlook</page> 
    <page number="16" section="Arsenal_Outlook">Arsenal_Outlook</page> 
</root> 

所需的輸出6行& 2列

<table> 
<tr> 
<td class="Stadium">Stadium</td> 
<td class="Support">Support</td> 
<td class="Cost">Cost</td> 
<td class="Outlook">Outlook</td> 
<td class="None"></td> 
<td class="None"></td> 
</tr> 
<tr> 
<td class="Stadium_R2">1-4</td> 
<td class="Support_R2">5-8</td> 
<td class="Cost_R2">9-12</td> 
<td class="Outlook_R2">13-16</td> 
<td class="None"></td> 
<td class="None"></td> 
</tr> 
</table> 

問候JJ。

回答

1

我相信這應該可以做到。注意使用NoneRows模板填寫額外的細胞,使6:

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

    <xsl:template match="/*"> 
    <table> 
     <xsl:variable name="groups" 
     select="*[generate-id() = generate-id(key('kGroup', @section)[1])]" /> 

     <tr> 
     <xsl:apply-templates select="$groups" mode="top" /> 
     <xsl:call-template name="NoneCells"> 
      <xsl:with-param name="count" select="6 - count($groups)" /> 
     </xsl:call-template> 
     </tr> 
     <tr> 
     <xsl:apply-templates select="$groups" mode="pageNums" /> 
     <xsl:call-template name="NoneCells"> 
      <xsl:with-param name="count" select="6 - count($groups)" /> 
     </xsl:call-template> 
     </tr> 
    </table> 
    </xsl:template> 

    <xsl:template match="page" mode="top"> 
    <xsl:variable name="sectName" select="substring-after(@section, 'Arsenal_')" /> 
    <td class="{$sectName}"> 
     <xsl:value-of select="$sectName" /> 
    </td> 
    </xsl:template> 

    <xsl:template match="page" mode="pageNums"> 
    <xsl:variable name="groupMembers" select="key('kGroup', @section)" /> 
    <td class="{substring-after(@section, 'Arsenal_')}_R2"> 
     <xsl:value-of select="concat($groupMembers[1]/@number, '-', 
            $groupMembers[last()]/@number)"/> 
    </td> 
    </xsl:template> 

    <xsl:template name="NoneCells"> 
    <xsl:param name="count" /> 
    <xsl:if test="$count > 0"> 
     <td class="None"></td> 
     <xsl:call-template name="NoneCells"> 
     <xsl:with-param name="count" select="$count - 1" /> 
     </xsl:call-template> 
    </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

當第一個樣品輸入運行:

<table> 
    <tr> 
    <td class="Stadium">Stadium</td> 
    <td class="Crowds">Crowds</td> 
    <td class="Support">Support</td> 
    <td class="Revenue">Revenue</td> 
    <td class="Cost">Cost</td> 
    <td class="Outlook">Outlook</td> 
    </tr> 
    <tr> 
    <td class="Stadium_R2">1-4</td> 
    <td class="Crowds_R2">5-8</td> 
    <td class="Support_R2">9-12</td> 
    <td class="Revenue_R2">13-16</td> 
    <td class="Cost_R2">17-20</td> 
    <td class="Outlook_R2">21-24</td> 
    </tr> 
</table> 

當你的第二個樣本輸入運行:

<table> 
    <tr> 
    <td class="Stadium">Stadium</td> 
    <td class="Support">Support</td> 
    <td class="Cost">Cost</td> 
    <td class="Outlook">Outlook</td> 
    <td class="None" /> 
    <td class="None" /> 
    </tr> 
    <tr> 
    <td class="Stadium_R2">1-4</td> 
    <td class="Support_R2">5-8</td> 
    <td class="Cost_R2">9-12</td> 
    <td class="Outlook_R2">13-16</td> 
    <td class="None" /> 
    <td class="None" /> 
    </tr> 
</table>