【问题标题】:how to apply different backgraound colors for particular selected columns in xsl如何为 xsl 中的特定选定列应用不同的背景颜色
【发布时间】:2014-01-09 09:36:53
【问题描述】:

我正在尝试为我的 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 和 column 2 应该是相同的颜色等等。但是每一列都应该有不同的颜色。

任何人都可以帮助我对我的 xsl 代码进行更改吗?提前谢谢你。

【问题讨论】:

    标签: xml xslt xsl-fo


    【解决方案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,输出如下所示:

    编辑:现在背景颜色改变而不是字体颜色。

    【讨论】:

    • 这里每列的背景颜色应该不同,并且所有列中的文本都应该是黑色的。我添加了我需要的输出的图形表示。并且第一行不应该驻留在表格中,它应该像一些标题。你能根据要求改变吗?谢谢
    • 谢谢,但输出中仍然缺少单元格边框颜色=黑色??
    • 不,XSLT 输出中没有缺少 background-color="black"。你也看过@Tomalak 的解决方案吗?我认为这是一个更灵活的解决方案。
    【解决方案2】:
    <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>
    

    注意事项

    • 此解决方案不使用&lt;xsl:for-each&gt;。您通常应该尽量避免它。
    • 将您的代码分成几个模板并使用&lt;xsl:apply-templates&gt;
    • 您可以在单独的模板中创建属性(如color)。
    • 使用&lt;xsl:call-template&gt; 不会影响position() 的值。
    • 在样式表级别声明xmlns:fo

    您的单元格定义中有很多毫无意义的重复。我确信 XSL:FO 有办法避免这种情况。尝试产生较少重复的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      相关资源
      最近更新 更多