html - XSLT: Extracting output content from the section attribute using xsl -
i have variation of question asked before in regards extracting specific according given input classes , content.
i have illustration solution provided @kirill polishchuk. how implement slight variation 1 particular section.
extracting class section attribute using xsl
i’m using xslt1.0, have outlined possible solution i'm not if best practice. i’m totally confused on how solving problem, advise , help 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.
here's 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 have xsl:variable
can list 1 or more item names should renamed other names. if match found, @to
value used. if not, substring-after(@section, '_')
used. i've used variables capture values of 2 formulas beingness used more 1 time in single template.
html xml xslt xslt-1.0
No comments:
Post a Comment