我有一个我之前问过的关于根据给定的类和内容的输入提取特定的问题的变体。
我有@Kirill Polishchuk 提供的示例解决方案。我如何为一个特定的部分实现细微的变化。
我正在使用 XSLT1.0,我已经概述了一个可能的解决方案,但如果这是最佳实践,我不是。我完全不知道如何解决这个问题,任何建议和帮助都将不胜感激。
输入:
<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_Stadium">Arsenal_Stadium</page>
<page number="6" section="Arsenal_Stadium">Arsenal_Stadium</page>
<page number="7" section="Arsenal_Stadium">Arsenal_Stadium</page>
<page number="8" section="Arsenal_Crowds">Arsenal_Crowds</page>
<page number="9" section="Arsenal_Crowds">Arsenal_Crowds</page>
<page number="10" section="Arsenal_Crowds">Arsenal_Crowds</page>
<page number="11" section="Arsenal_Crowds">Arsenal_Crowds</page>
<page number="12" section="Arsenal_Crowds">Arsenal_Crowds</page>
<page number="13" section="Arsenal_Finances">Arsenal_Finances</page>
<page number="14" section="Arsenal_Finances">Arsenal_Finances</page>
<page number="15" section="Arsenal_Finances">Arsenal_Finances</page>
<page number="16" section="Arsenal_Finances">Arsenal_Finances</page>
<page number="17" section="Arsenal_Finances">Arsenal_Finances</page>
<page number="18" section="Arsenal_Finances">Arsenal_Finances</page>
<page number="19" section="Arsenal_Finances">Arsenal_Finances</page>
<page number="20" section="Arsenal_Outlook">Arsenal_Outlook</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>
<tr>
<td class="Stadium">Stadium</td>
<td></td>
<td class="Crowds">Crowds</td>
<td></td>
<td class="Finances">Finance’s Today</td>
<td></td>
<td class="Outlook">Outlook</td>
<td></td>
</tr>
<tr>
<td>1</td>
<td>7</td>
<td>8</td>
<td>12</td>
<td>13</td>
<td>19</td>
<td>20</td>
<td>24</td>
</tr>
</table>
可能的解决方案
<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 class="{substring-after(@section, '_')}">
<xsl:choose>
<xsl:when test="contains(@section, '_Finances')">Finance’s Today
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-after(@section, '_')"/>:
</xsl:otherwise>
</xsl:choose>
</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>
问候JJ。