XSLT:取决于 td 数量的表类



我还有一个关于具有固定结构和可变输入内容的表的 XSLT 转换的问题。

我概述了两个不同的例子。

但是,所需的表类取决于输入 XML 中的节数。

我有一个由@Kirill Polishchuk提供的先前示例开始,@JLRishe

使用 xsl 从节属性中提取类

XSLT 转换为固定表结构

请注意,我正在使用 XSLT 1.0。任何建议或指导将不胜感激。

例 1

包含 6 个部分的输入 XML

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

期望的输出

<table class="col_6">
<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>

期望的输出

<table class="col_4">
<tr>
<td class="Stadium">Stadium</td>
<td class="Support">Support</td>
<td class="Cost">Cost</td>
<td class="Outlook">Outlook</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>
</tr>
</table>

这应该可以做到:

<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="/*">
    <xsl:variable name="groups"
       select="*[generate-id() = generate-id(key('kGroup', @section)[1])]" />
    <table class="col_{count($groups)}">
      <tr>
        <xsl:apply-templates select="$groups" mode="top" />
      </tr>
      <tr>
        <xsl:apply-templates select="$groups" mode="pageNums" />
      </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:stylesheet>

在第一个输入上运行时:

<table class="col_6">
  <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 class="col_4">
  <tr>
    <td class="Stadium">Stadium</td>
    <td class="Support">Support</td>
    <td class="Cost">Cost</td>
    <td class="Outlook">Outlook</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>
  </tr>
</table>

相关内容

  • 没有找到相关文章

最新更新