2013-02-11 76 views
0

我有一個問題的變體,我之前問過關於根據類和內容的給定輸入提取特定的問題。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。

+0

'我有一個問題的變化,我早前問'它是什麼? – newday 2013-02-11 03:54:51

+0

@pottuamman http://stackoverflow.com/questions/13466641/extracting-a-class-from-the-section-attribute-using-xsl – 2013-02-11 04:00:27

回答

1

這裏有一個稍微更具擴展性的方法:

<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:variable name="rename"> 
    <item from="Arsenal_Finances" to="Finance’s Today" /> 
    </xsl:variable> 

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


    <xsl:template match="page"> 
    <xsl:variable name="sectionTrimmed" 
        select="substring-after(@section, '_')" /> 
    <td class="{$sectionTrimmed}"> 
     <xsl:variable name="renameItem" 
        select="document('')//xsl:variable[@name = 'rename'] 
          /item[@from = current()/@section]" /> 
     <xsl:choose> 
     <xsl:when test="$renameItem"> 
      <xsl:value-of select="$renameItem/@to"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="$sectionTrimmed"/> 
     </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> 

在這裏我有一個xsl:variable,你可以列出應該被重新命名爲其他名稱1個或多個項目名稱。如果找到匹配項,則使用@to值。如果不是,則使用substring-after(@section, '_')。我還使用變量來捕獲在單個模板中多次使用的兩個公式的值。

+0

感謝您的幫助@JLRishe,非常感謝。 – 2013-02-12 22:16:45