xml - xslt 1.0 groupping by node attribute(s) and output as html -
i've searched stackoverflow this, somehow did not find example fit me. not particularly skilled when comes xslt. want transform given xml:
<root> <list id="1" name="list1"> <item rownumber="1" data="list1row1" name="firstrow"> </item> <item rownumber="2" data="list1row2" name="secondrow"> </item> <item rownumber="3" data="list1row3" name="thirdrow"> </item> </list> <list id="2" name="list2"> <item rownumber="1" data="list2row1" name="firstrow"> </item> <item rownumber="2" data="list2row2" name="secondrow"> </item> <item rownumber="3" data="list2row3" name="thirdrow"> </item> </list> <list id="3" name="list3"> <item rownumber="1" data="list3row1" name="firstrow"> </item> <item rownumber="2" data="list3row2" name="secondrow"> </item> <item rownumber="3" data="list3row3" name="thirdrow"> </item> </list> </root>
into html:
<table> <tr> <td>1</td> <td>firstrow</td> <td id="list1_row1">list1row1</td> <td id="list2_row1">list2row1</td> <td id="list3_row1">list3row1</td> </tr> <tr> <td>2</td> <td>secondrow</td> <td id="list1_row2">list1row2</td> <td id="list2_row2">list2row2</td> <td id="list3_row2">list3row2</td> </tr> <tr> <td>3</td> <td>thirdrow</td> <td id="list1_row3">list1row3</td> <td id="list2_row3">list2row3</td> <td id="list3_row3">list3row3</td> </tr> </table>
so want take rows , group them in table row, cells containing row number, name , data. ok if rownumber , name hidden cells.
i thinking of using <xsl:if test="position() = 1">
check whether display metadata (row number , name). cells have id list name (or id) , row number (so can searched javascript).
thank in advance!
assuming each list has same number of items (i.e. rows), try way:
xslt 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/> <xsl:key name="item-by-row" match="item" use="@rownumber"/> <xsl:template match="/"> <table> <xsl:for-each select="root/list[1]/item"> <tr> <td><xsl:value-of select="@rownumber"/></td> <td><xsl:value-of select="@name"/></td> <xsl:for-each select="key('item-by-row', @rownumber)"> <td id="{../@name}_row{@rownumber}"><xsl:value-of select="@data"/></td> </xsl:for-each> </tr> </xsl:for-each> </table> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment