我有一個問題的變體,我之前問過關於根據類和內容的給定輸入提取特定的問題。XSLT:使用xsl提取section屬性的輸出內容
我有@Kirill Polishchuk提供的示例解決方案。如何爲特定的部分實施細微的變化。
Extracting a class from the section attribute using xsl
我使用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。
'我有一個問題的變化,我早前問'它是什麼? – newday 2013-02-11 03:54:51
@pottuamman http://stackoverflow.com/questions/13466641/extracting-a-class-from-the-section-attribute-using-xsl – 2013-02-11 04:00:27