2013-02-11 67 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