xml - Grouping rows and merge output HTML cells using XSL -
i'm trying group span of rows can set background color of each set alternatively. merge first column 1 cell. here have done far.
xml
<action> <step> <obj>uc_a</obj> </step> <step> <obj>abc</obj> </step> <step> <obj>bcd</obj> </step> <step> <obj>cde</obj> </step> <step> <obj>uc_b</obj> </step> <step> <obj>def</obj> </step> <step> <obj>efg</obj> </step> <step> <obj>uc_c</obj> </step> <step> <obj>abc</obj> </step> <step> <obj>bcd</obj> </step> <step> <obj>cde</obj> </step> </action>
xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/action"> <html> <head> <style type="text/css"> table { table-layout:auto; border-collapse:collapse; font-family:arial; } td { vertical-align:top; border:1px solid silver; padding:0pt; font-size:9pt; padding-right:3px; padding-left:3px; } tr.odd { background-color:#ffffdb; } tr.even { background-color:#e9ffff; } </style> </head> <body> <table> <xsl:apply-templates/> </table> </body> </html> </xsl:template> <xsl:template match="step"> <xsl:variable name="ucbgcolor"> <xsl:if test="starts-with(obj, 'uc_')"> <xsl:variable name="isoddeven"> <xsl:number count="step[starts-with(obj, 'uc_')]"/> </xsl:variable> <xsl:choose> <xsl:when test="$isoddeven mod 2 = 0">even</xsl:when> <xsl:otherwise>odd</xsl:otherwise> </xsl:choose> </xsl:if> </xsl:variable> <tr class="{$ucbgcolor}"> <td> <xsl:if test="starts-with(obj, 'uc_')"> <xsl:number count="step[starts-with(obj, 'uc_')]"/> </xsl:if> </td> <td> <xsl:value-of select="obj" /> </td> </tr> </xsl:template> </xsl:stylesheet>
current output
expected output
to add rowspan
attribute use approach like
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="html" omit-xml-declaration="yes" version="5.0" doctype-system="about:legacy-compat" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="group" match="step[obj[not(starts-with(., 'uc_'))]]" use="generate-id(preceding-sibling::step[obj[starts-with(., 'uc_')]][1])"/> <xsl:template match="/action"> <html> <head> <style type="text/css"> table { table-layout:auto; border-collapse:collapse; font-family:arial; } td { vertical-align:top; border:1px solid silver; padding:0pt; font-size:9pt; padding-right:3px; padding-left:3px; } tr.odd { background-color:#ffffdb; } tr.even { background-color:#e9ffff; } </style> </head> <body> <table> <xsl:apply-templates select="step[starts-with(obj, 'uc_')]"/> </table> </body> </html> </xsl:template> <xsl:template match="step[starts-with(obj, 'uc_')]"> <xsl:variable name="ucbgcolor"> <xsl:if test="starts-with(obj, 'uc_')"> <xsl:variable name="isoddeven"> <xsl:number count="step[starts-with(obj, 'uc_')]"/> </xsl:variable> <xsl:choose> <xsl:when test="$isoddeven mod 2 = 0">even</xsl:when> <xsl:otherwise>odd</xsl:otherwise> </xsl:choose> </xsl:if> </xsl:variable> <xsl:variable name="group" select="key('group', generate-id())"/> <tr class="{$ucbgcolor}"> <td rowspan="{1 + count($group)}"> <xsl:number count="step[starts-with(obj, 'uc_')]"/> </td> <td> <xsl:value-of select="obj"/> </td> </tr> <xsl:apply-templates select="$group"> <xsl:with-param name="ucbgcolor" select="$ucbgcolor"/> </xsl:apply-templates> </xsl:template> <xsl:template match="step[not(starts-with(obj, 'uc_'))]"> <xsl:param name="ucbgcolor"/> <tr class="{$ucbgcolor}"> <td> <xsl:value-of select="obj"/> </td> </tr> </xsl:template> </xsl:stylesheet>
online @ http://xsltransform.net/gwmuik9/2.
Comments
Post a Comment