xml - XSLT & XSL-FO: Creating a table with multiple rows? -
xml - XSLT & XSL-FO: Creating a table with multiple rows? -
i'm new xslt , stuck problem have element unknown amount of children, , need display these children in table such there 5-6 columns available display information.
if i'm given xml file looks this:
<books> <book> <author>ralls, kim</author> <title>midnight rain</title> </book> <book> <author>corets, eva</author> <title>maeve ascendant</title> </book> <book> <author>corets, eva</author> <title>oberon's legacy</title> </book> <book> <author>randall, cynthia</author> <title>lover birds</title> </book> <book> <author>thurman, paula</author> <title>splish splash</title> </book> <book> <author>knorr, stefan</author> <title>creepy crawlies</title> </book> <book> <author>kress, peter</author> <title>paradox lost</title> </book> <book> <author>crichton, michael</author> <title>jurassic park</title> </book> <book> <author>orwell, george</author> <title>1984</title> </book> <book> <author>martin, george</author> <title>a song of water ice , fire</title> </book> </books> i display these 10 books in table consisting of 2 rows , 5 columns.
i've gotten far:
<xsl:template match="books" mode="table"> <fo:table margin-left="auto" margin-right="auto"> <fo:table-body> <fo:table-row table-layout="fixed"> <xsl:for-each select="skill"> <fo:table-cell border="1"> <fo:block font-weight="bold"> <xsl:value-of select="name"/> </fo:block> </fo:table-cell> </xsl:for-each> </fo:table-row> </fo:table-body> </fo:table> </xsl:template> but set every cell on same row. i'm looking way check if loop has run amount of times (5 or 6), , inserting new row when happens, don't know if that's can in xsl.
can point me in right direction?
the previous answers way complicated should (and is) easy. recursion not required such simple thing.
in xsl fo not need construction tables rows. can utilize attribute "ends-row" specify ending row , starting new one. can adapt simple illustration , pass in "number of columns" (see mod 5 ... means after every 5th 1 start new row ... alter 4 or 8 or whatever wish) ... create construction table (fo:table , fo:table-body) outside of this. within table body set cells children template does:
<xsl:template match="book"> <xsl:variable name="pos" select="position()"/> <fo:table-cell> <xsl:if test="not($pos mod 5)"> <xsl:attribute name="ends-row">true</xsl:attribute> </xsl:if> <fo:block> <xsl:value-of select="author"/> </fo:block> </fo:table-cell> </xsl:template> so putting simple illustration info ... see below. formats xml 5 columns per row.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:fo="http://www.w3.org/1999/xsl/format" xmlns:xs="http://www.w3.org/2001/xmlschema" version="1.0"> <xsl:template match="/"> <fo:root xmlns:fo="http://www.w3.org/1999/xsl/format"> <fo:layout-master-set> <fo:simple-page-master margin-top="1in" margin-left="1in" margin-bottom="1in" margin-right="1in" page-width="8in" page-height="11in" master-name="first"> <fo:region-body margin-top="0pt"/> <fo:region-before extent="0pt"/> <fo:region-after extent="0pt"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="first"> <fo:flow flow-name="xsl-region-body" font-size="12pt" font-family="helvetica"> <xsl:apply-templates/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="books"> <fo:table> <fo:table-body> <xsl:apply-templates/> </fo:table-body> </fo:table> </xsl:template> <xsl:template match="book"> <xsl:variable name="pos" select="position()"/> <fo:table-cell border="1pt solid black"> <xsl:if test="not($pos mod 5)"> <xsl:attribute name="ends-row">true</xsl:attribute> </xsl:if> <fo:block> <xsl:value-of select="author"/> </fo:block> </fo:table-cell> </xsl:template> </xsl:stylesheet> xml xslt xsl-fo
Comments
Post a Comment