I have a single data set with varying list of the attributes(about 15), like displayed below.
Sample Data 1:
<cd>
<name>Tom</name>
<address>Madison Square</address>
<phone>000-000-0000</phone>
</cd>
Sample Data 2:
<cd>
<name>Tom</name>
<city>New York</city>
<phone>000-000-0000</phone>
</cd>
Complete list of attributes(subset):
<cd>
<name>xxx</name>
<address>xxxx</address>
<city>xxxx</city>
<phone>000-000-0000</phone>
</cd>
I want to build a fo:table with alternate row colors for this dynamic table. I have tried using xsl:for-each and position(), but it doesn't work. Is there a way to get previous row properties like, color? Any suggestions.
Added code for reference:
<fo:table-row>
<fo:table-cell padding-left="3pt" padding-top="8pt">
<fo:block> <xsl:value-of select="cd/name"/></fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:if test="normalize-space(cd/address) !=''">
<fo:table-row background-color="#EEF0F2" >
<fo:table-cell padding-left="3pt" padding-top="8pt">
<fo:block> <xsl:value-of select="cd/address"/></fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:if>
<xsl:if test="normalize-space(cd/city) !=''">
<fo:table-row background-color="#EEF0F2" >
<fo:table-cell padding-left="3pt" padding-top="8pt">
<fo:block> <xsl:value-of select="cd/city"/></fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:if>
Your posted code example is not quite enough. We have no idea of context. Something reproducible (something we could run) would be ideal.
Here's a generic answer that should help though.
What you can do is use
xsl:apply-templates
to process the children ofcd
that aren't empty. This changes the node set that's being processed and will allowposition()
to work properly.Note: You could also do this with
xsl:for-each
. However, if you're transforming anything but the most simple XML to a very simple XSL-FO you're better off using a push approach rather than a pull approach. This will make your XSLT much easier to maintain and scale.You can then use
mod
to see if the position is divisible by 2. If it is, add the background-color.Example:
XML Input
XSLT 1.0
XSL-FO Output
Rendered PDF (used FOP 1.1)