Reputation: 365
I am trying to apply different colors for each column in my XSLT stylesheet but I could not find a satisfying solution.
Here is my xsl code:
<xsl:output encoding="iso-8859-1" />
<xsl:template match ="records">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="list">
<fo:region-body></fo:region-body>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="list">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:table>
<fo:table-body>
<xsl:for-each select="./list">
<fo:table-row background-color="rgb(192,192,192)">
<xsl:for-each select="./item">
<fo:table-cell text-align="center">
<fo:block font-family="monospace"
font-size="12pt" color="green"
wrap-option="no-wrap" padding="5pt"
space-before="5pt" space-after="5pt">
<xsl:value-of select="val" /></fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
and my data that comes from xml is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<records>
<list>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Column 1</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Column 2</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Column 3</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Column 4</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Column 5</val>
</item>
</list>
<list>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Value 1</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Value 2</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Value 3</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Value 4</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Value 5</val>
</item>
</list>
</records>
Now, in the output the table cells of value1 and column1 should be of the same color, value2 and column 2 should be of the same color and so on. But every column should be in a different color.
Can any one help with the changes I need to make in my xsl code? Thank you in advance.
Upvotes: 1
Views: 7546
Reputation: 22617
Do not hard-code your color
attribute. Instead, check what element you are looking at within an xsl:choose
and then assign a value to the color
attribute respectively.
Of course, "Column 1" might not be the actual name in your XML data and you'd have to replace this:
<xsl:when test="contains(./val,'1')">
with another condition that can tell your item
elements apart. If the coloring depends on position only, use position()
as suggested by @Tomalak.
Full stylesheet:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="iso-8859-1" />
<xsl:template match ="records">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="list">
<fo:region-body></fo:region-body>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="list">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:table>
<fo:table-body>
<xsl:for-each select="./list">
<fo:table-row background-color="rgb(192,192,192)">
<xsl:for-each select="./item">
<fo:table-cell text-align="center">
<fo:block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt">
<xsl:attribute name="background-color">
<xsl:choose>
<xsl:when test="contains(./val,'1')">
<xsl:text>green</xsl:text>
</xsl:when>
<xsl:when test="contains(./val,'2')">
<xsl:text>red</xsl:text>
</xsl:when>
<xsl:when test="contains(./val,'3')">
<xsl:text>orange</xsl:text>
</xsl:when>
<xsl:when test="contains(./val,'4')">
<xsl:text>blue</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>black</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:value-of select="val"/>
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
With FOP 1.0, the output looks like:
EDIT: Now the background color changes instead of the font color.
Upvotes: 1
Reputation: 338238
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
>
<xsl:output encoding="iso-8859-1" />
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="list">
<fo:region-body></fo:region-body>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="list">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates select="records" />
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="records">
<fo:block>
<fo:table>
<fo:table-body>
<xsl:apply-templates select="list" />
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
<xsl:template match="records/list">
<fo:table-row background-color="rgb(192,192,192)">
<xsl:apply-templates select="item" />
</fo:table-row>
</xsl:template>
<xsl:template match="records/list/item">
<fo:table-cell text-align="center">
<fo:block
font-family="monospace"
font-size="12pt"
wrap-option="no-wrap"
padding="5pt"
space-before="5pt"
space-after="5pt"
>
<xsl:call-template name="cell-color" />
<xsl:value-of select="val" />
</fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template name="cell-color">
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="position() = 1">green</xsl:when>
<xsl:when test="position() = 2">blue</xsl:when>
<xsl:when test="position() = 3">red</xsl:when>
<xsl:when test="position() = 4">yellow</xsl:when>
<xsl:when test="position() = 5">brown</xsl:when>
<xsl:otherwise>black</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
gives you
<root xmlns="http://www.w3.org/1999/XSL/Format">
<layout-master-set>
<simple-page-master master-name="list">
<region-body/>
</simple-page-master>
</layout-master-set>
<page-sequence master-reference="list">
<flow flow-name="xsl-region-body">
<block>
<table>
<table-body>
<table-row background-color="rgb(192,192,192)">
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="green">Column 1</block>
</table-cell>
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="blue">Column 2</block>
</table-cell>
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="red">Column 3</block>
</table-cell>
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="yellow">Column 4</block>
</table-cell>
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="brown">Column 5</block>
</table-cell>
</table-row>
<table-row background-color="rgb(192,192,192)">
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="green">Value 1</block>
</table-cell>
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="blue">Value 2</block>
</table-cell>
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="red">Value 3</block>
</table-cell>
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="yellow">Value 4</block>
</table-cell>
<table-cell text-align="center">
<block font-family="monospace" font-size="12pt" wrap-option="no-wrap" padding="5pt" space-before="5pt" space-after="5pt" color="brown">Value 5</block>
</table-cell>
</table-row>
</table-body>
</table>
</block>
</flow>
</page-sequence>
</root>
Notes
<xsl:for-each>
. You should generally try to avoid it.<xsl:apply-templates>
.color
) in a separate template.<xsl:call-template>
does not affect the value of position()
.xmlns:fo
at the stylesheet level.There is a lot of pointless duplication in your cell definitions. I'm sure XSL:FO has a way to avoid that. Try to produce output that is less duplicated.
Upvotes: 1