我正在尝试使用xsl(FOP)将我的XML数据放入表格格式,但由于单元格中没有文本时单元格重叠,因此无法完全完成。
这是我的xsl代码:
<?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 margin-left="50pt"
margin-top="50pt"
margin-bottom="50pt"
margin-right="50pt"></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 text-align="center">
<fo:table table-layout="fixed" width="100%" >
<fo:table-body>
<xsl:for-each select="./list">
<xsl:if test="position()<= 1">
<fo:table-row>
<xsl:for-each select="./item">
<fo:table-cell text-align="center" border="2px">
<fo:block color="green" font-family="monospace" font-size="10pt" padding="5pt" space-before="5pt" space-after="5pt">
<xsl:attribute name="color">
<xsl:choose>
<xsl:when test="position() = 1 ">
<xsl:text>black</xsl:text>
</xsl:when>
<xsl:when test="position() = 2 ">
<xsl:text>#333399</xsl:text>
</xsl:when>
<xsl:when test="position() = 3 ">
<xsl:text>#FF9900</xsl:text>
</xsl:when>
<xsl:when test="position() = 4 ">
<xsl:text>#96CCD8</xsl:text>
</xsl:when>
<xsl:when test="position() = 5 ">
<xsl:text>#19A347</xsl:text>
</xsl:when>
<xsl:when test="position() = 6 ">
<xsl:text>green</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>white</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:if>
</xsl:for-each>
<xsl:for-each select="./list">
<xsl:if test="position()!=1">
<fo:table-row>
<xsl:for-each select="./item">
<fo:table-cell border="4px" text-align="center">
<fo:block font-family="monospace" border-style="solid" 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="position() = 1 ">
<xsl:text>#C1BFC4</xsl:text>
</xsl:when>
<xsl:when test="position() = 2 ">
<xsl:text>#B1A1C8</xsl:text>
</xsl:when>
<xsl:when test="position() = 3 ">
<xsl:text>#F9CAA0</xsl:text>
</xsl:when>
<xsl:when test="position() = 4 ">
<xsl:text>#96CCD8</xsl:text>
</xsl:when>
<xsl:when test="position() = 5 ">
<xsl:text>#C2D89A</xsl:text>
</xsl:when>
<xsl:when test="position() = 6 ">
<xsl:text>green</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>red</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:if>
</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>Subject Number</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Monthly Dairy Contact</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>3-Month Safety Contact</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Annual visit</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>Suspected HZ follow-up Visit</val>
</item>
</list>
<list>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>49210</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>x</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>15-sep-2012</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val></val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val></val>
</item>
</list>
<list>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>49210</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>x</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>15-sep-2012</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>test</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val></val>
</item>
</list>
<list>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>49210</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>x</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>15-sep-2012</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>test</val>
</item>
<item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="jaXBValue">
<val>test</val>
</item>
</list>
</records>
我的XSL输出如下:
任何人都可以修改我的代码,以便正确显示表格,而不是像我在图片中显示的那样。
答案 0 :(得分:2)
如果您将border-style
和background-color
属性从fo:block
移至fo:table-cell
,则会有效。通过删除xsl:text
元素也可以简化代码的这一部分:
<fo:table-cell border="4px" border-style="solid" text-align="center">
<xsl:attribute name="background-color">
<xsl:choose>
<xsl:when test="position() = 1">#C1BFC4</xsl:when>
<xsl:when test="position() = 2">#B1A1C8</xsl:when>
<xsl:when test="position() = 3">#F9CAA0</xsl:when>
<xsl:when test="position() = 4">#96CCD8</xsl:when>
<xsl:when test="position() = 5">#C2D89A</xsl:when>
<xsl:when test="position() = 6">green</xsl:when>
<xsl:otherwise>red</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<fo:block font-family="monospace" font-size="12pt" wrap-option="no-wrap"
padding="5pt" space-before="5pt" space-after="5pt">
<xsl:value-of select="val"/>
</fo:block>
</fo:table-cell>