我正在尝试为我的XSLT样式表中的每一列应用不同的颜色,但我找不到令人满意的解决方案。
这是我的xsl代码:
<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>
我的数据来自xml如下:
<?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>
现在,在输出中,value1和column1的表格单元格应该是相同的颜色,value2和第2列应该是相同的颜色,依此类推。但每列应该有不同的颜色。
任何人都可以帮助我在xsl代码中进行更改吗?提前谢谢。
答案 0 :(得分:1)
不要对您的color
属性进行硬编码。相反,请检查您在xsl:choose
中查看的元素,然后分别为color
属性分配值。
当然,“第1列”可能不是XML数据中的实际名称,您必须替换它:
<xsl:when test="contains(./val,'1')">
另一个条件可以告诉你的item
元素。如果着色仅取决于位置,请使用@Tomalak建议的position()
。
完整样式表:
<?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>
使用FOP 1.0,输出如下:
编辑:现在背景颜色改变而不是字体颜色。
答案 1 :(得分:1)
<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>
给你
<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>
注释
<xsl:for-each>
。你通常应该尽量避免它。<xsl:apply-templates>
。color
)。<xsl:call-template>
不会影响position()
。xmlns:fo
。您的单元格定义中存在许多无意义的重复。我确信XSL:FO有办法避免这种情况。尝试生成不那么重复的输出。