Reputation: 153
I have a variation of a question I asked earlier in regards to extracting specific according to a given input for classes and content.
I have an example solution provided by @Kirill Polishchuk. How do I implement a slight variation for one particular section.
Extracting a class from the section attribute using xsl
I’m using XSLT1.0, I have outlined a possible solution but I'm not if this the best practice. I’m totally confused on how about solving this problem, any advise and help to will appreciated.
INPUT:
<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>
OUTPUT
<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>
POSSIBLE SOLUTION
<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>
Regards JJ.
Upvotes: 0
Views: 141
Reputation: 101680
Here's a somewhat more extensible approach:
<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>
Here I have an xsl:variable
where you can list 1 or more item names that should be renamed to other names. If a match is found, then the @to
value is used. If not, then substring-after(@section, '_')
is used. I've also used variables to capture the values of two formulas that were being used more than once in a single template.
Upvotes: 1