xslt通过测试2列来更改背景颜色

时间:2013-10-01 18:54:00

标签: xml xslt colors

我有一些xslt代码将xml文件转换为html。如果CallHandled不等于CallsRecorded,我想更改单元格背景颜色。这是我的xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
    <table border="1">
      <tr>
        <td>Date</td>
        <td>AccountName</td>
        <td>AccountKey</td>
        <td>LOB</td>
        <td>LOBKey</td>
        <td>CallHandled</td>
        <td>CallRecorded</td>
      </tr>
      <xsl:for-each select="FieldingCounts/Row">
        <tr>
          <xsl:for-each select="./*">
            <td bgcolor="#FFFF00">
              <xsl:value-of select="." />
            </td>
          </xsl:for-each>
        </tr>
      </xsl:for-each>
    </table>
 </xsl:template>
</xsl:stylesheet>

以下是示例输入xml

<FieldingCounts>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>[none]</account_name>
    <account_key>-1</account_key>
    <lob_name>[none]</lob_name>
    <lob_key>-1</lob_key>
    <CallHandled>678</CallHandled>
    <Calls_Recorded>336</Calls_Recorded>
  </Row>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>ABC</account_name>
    <account_key>342</account_key>
    <lob_name>Grass</lob_name>
    <lob_key>1382</lob_key>
    <CallHandled>1389</CallHandled>
    <Calls_Recorded>1226</Calls_Recorded>
  </Row>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>ABC</account_name>
    <account_key>347</account_key>
    <lob_name>Blocks</lob_name>
    <lob_key>1866</lob_key>
    <CallHandled>2544</CallHandled>
    <Calls_Recorded>2485</Calls_Recorded>
  </Row>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>HD</account_name>
    <account_key>347</account_key>
    <lob_name>Wood</lob_name>
    <lob_key>2417</lob_key>
    <CallHandled>567</CallHandled>
    <Calls_Recorded>567</Calls_Recorded>
  </Row>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>Lowes</account_name>
    <account_key>505</account_key>
    <lob_name>Nails</lob_name>
    <lob_key>2388</lob_key>
    <CallHandled>2622</CallHandled>
    <Calls_Recorded>0</Calls_Recorded>
  </Row>
  </Row>
</FieldingCounts>

我希望html看起来像什么

<FieldingCounts>
  <table border="1">
    <tr>
      <td>Date</td>
      <td>AccountName</td>
      <td>AccountKey</td>
      <td>LOB</td>
      <td>LOBKey</td>
      <td>CallHandled</td>
      <td>CallRecorded</td>
    </tr>
    <tr>
      <td>2013-09-23T00:00:00</td>
      <td>[none]</td>
      <td>-1</td>
      <td>[none]</td>
      <td>-1</td>
      <td>678</td>
      <td bgcolor="RED">336</td>
    </tr>
    <tr>
      <td>2013-09-23T00:00:00</td>
      <td>ABC</td>
      <td>342</td>
      <td>Grass</td>
      <td>1382</td>
      <td>1389</td>
      <td bgcolor="RED">1226</td>
    </tr>
    <tr>
      <td>2013-09-23T00:00:00</td>
      <td>ABC</td>
      <td>347</td>
      <td>Blocks</td>
      <td>1866</td>
      <td>2544</td>
      <td bgcolor="RED">2485</td>
    </tr>
    <tr>
      <td>2013-09-23T00:00:00</td>
      <td>HD</td>
      <td>347</td>
      <td>Wood</td>
      <td>2417</td>
      <td>567</td>
      <td>567</td>
    </tr>
    <tr>
      <td>2013-09-23T00:00:00</td>
      <td>Lowes</td>
      <td>505</td>
      <td>Nails</td>
      <td>2388</td>
      <td>2622</td>
      <td bgcolor="RED">0</td>
  </tr>
  </table>
</FieldingCounts>

这是我想要的新OUTPUT html

<FieldingCounts>
    <tr>
        <td>Date 09-23-2013</td>
    </tr>
  <table border="1">
    <tr bgcolor="lightgrey" cellspacing="right">
      <td>AccountName</td>
      <td>AccountKey</td>
      <td>LOB</td>
      <td>LOBKey</td>
      <td>CallHandled</td>
      <td>CallRecorded</td>
      <td>CallMissing</td>
      <td>Percentage</td>
    </tr>
    <tr  bgcolor="lightyellow">
      <td>[none]</td>
      <td>-1</td>
      <td>[none]</td>
      <td>-1</td>
      <td>678</td>
      <td bgcolor="RED">336</td>
      <td bgcolor="RED">342</td>
      <td bgcolor="RED">49.55%</td>
    </tr>
    <tr  bgcolor="lightyellow">
      <td>ABC</td>
      <td>342</td>
      <td>Grass</td>
      <td>1382</td>
      <td>1389</td>
      <td bgcolor="RED">1226</td>
      <td bgcolor="RED">163</td>
      <td bgcolor="RED">88.26%</td>
    </tr>
    <tr  bgcolor="lightyellow">
      <td>ABC</td>
      <td>347</td>
      <td>Blocks</td>
      <td>1866</td>
      <td>2544</td>
      <td bgcolor="RED">2485</td>
      <td bgcolor="RED">59</td>
      <td bgcolor="RED">97.68%</td>
    </tr>
    <tr  bgcolor="lightyellow">
      <td>HD</td>
      <td>347</td>
      <td>Wood</td>
      <td>2417</td>
      <td>567</td>
      <td bgcolor="lightGreen">567</td>
      <td bgcolor="lightGreen">0</td>
      <td bgcolor="lightGreen">100%</td>
    </tr>
    <tr  bgcolor="lightyellow">
      <td>Lowes</td>
      <td>505</td>
      <td>Nails</td>
      <td>2388</td>
      <td>2622</td>
      <td bgcolor="RED">0</td>
      <td bgcolor="RED">2622</td>
      <td bgcolor="RED">0.00%</td>
  </tr>
  </table>
</FieldingCounts>

以下是用于获取新输出的xml

<FieldingCounts>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>[none]</account_name>
    <account_key>-1</account_key>
    <lob_name>[none]</lob_name>
    <lob_key>-1</lob_key>
    <CallHandled>678</CallHandled>
    <Calls_Recorded>336</Calls_Recorded>
    <CallMissing>342</CallMissing>
    <Percentage>49.55</Percentage>
  </Row>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>ABC</account_name>
    <account_key>342</account_key>
    <lob_name>Grass</lob_name>
    <lob_key>1382</lob_key>
    <CallHandled>1389</CallHandled>
    <Calls_Recorded>1226</Calls_Recorded>
    <CallMissing>163</CallMissing>
    <Percentage>88.26</Percentage>
  </Row>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>ABC</account_name>
    <account_key>347</account_key>
    <lob_name>Blocks</lob_name>
    <lob_key>1866</lob_key>
    <CallHandled>2544</CallHandled>
    <Calls_Recorded>2485</Calls_Recorded>
    <CallMissing>59</CallMissing>
    <Percentage>97.28</Percentage>
  </Row>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>HD</account_name>
    <account_key>347</account_key>
    <lob_name>Wood</lob_name>
    <lob_key>2417</lob_key>
    <CallHandled>567</CallHandled>
    <Calls_Recorded>567</Calls_Recorded>
    <CallMissing>0</CallMissing>
    <Percentage>100</Percentage>
  </Row>
  <Row>
    <day_date>2013-09-23T00:00:00</day_date>
    <account_name>Lowes</account_name>
    <account_key>505</account_key>
    <lob_name>Nails</lob_name>
    <lob_key>2388</lob_key>
    <CallHandled>2622</CallHandled>
    <Calls_Recorded>0</Calls_Recorded>
    <CallMissing>2622</CallMissing>
    <Percentage>0.00</Percentage>
  </Row>
</FieldingCounts>

到目前为止,我的xslt不正确,但我正试图到达那里.....

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <tr>Date</tr>
        <table border="1">
            <tr bgcolor="lightgrey" cellspacing="right">
                <td></td>
                <td>AccountName</td>
                <td>AccountKey</td>
                <td>LOB</td>
                <td>LOBKey</td>
                <td>CallHandled</td>
                <td>CallRecorded</td>
                <td>Missing</td>
                <td>Percentage</td>
            </tr>
            <xsl:for-each select="FieldingCounts/Row">
                <tr bgcolor="lightyellow">
                    <xsl:for-each select="./*">
                        <td>
                            <xsl:attribute name="bgcolor">
                                <xsl:choose>
                                    <xsl:when test="local-name() = 'Calls_Recorded' and ../CallHandled != text()">
                                       <xsl:text>F08080</xsl:text>
                                    </xsl:when>
                                    <xsl:when test="local-name() = 'Calls_Recorded' and ../CallHandled = text()">
                       <xsl:text>LightGreen</xsl:text>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:attribute>
                            <xsl:value-of select="."/>
                        </td>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

2 个答案:

答案 0 :(得分:1)

<xsl:for-each xmlns:xsl="sdfa" select="FieldingCounts/Row">
   <tr>
      <xsl:for-each select="./*">
         <td bgcolor="#FFFF00">
            <xsl:if test="(position() = 6 and normalize-space(../*[position()=7]) != normalize-space(.)) or (position() = 7 and normalize-space(../*[position()=6]) != normalize-space(.))">
               <xsl:attribute name="bgcolor">
                  <xsl:text>red</xsl:text>
               </xsl:attribute>
            </xsl:if>
            <xsl:value-of select="." />
         </td>
      </xsl:for-each>
   </tr>
</xsl:for-each>

答案 1 :(得分:0)

以下XSLT将更改Calls_Recorded单元格的背景颜色,如果它不等于CallHandled

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>

    <xsl:template match="/">
        <table border="1">
            <tr>
                <td>Date</td>
                <td>AccountName</td>
                <td>AccountKey</td>
                <td>LOB</td>
                <td>LOBKey</td>
                <td>CallHandled</td>
                <td>CallRecorded</td>
            </tr>
            <xsl:for-each select="FieldingCounts/Row">
                <tr>
                    <xsl:for-each select="./*">
                        <td>
                            <xsl:attribute name="bgcolor">
                                <xsl:choose>
                                    <xsl:when test="local-name() = 'Calls_Recorded' and ../CallHandled != text()">
                                        <xsl:text>RED</xsl:text>
                                    </xsl:when>
                                </xsl:choose>
                            </xsl:attribute>
                            <xsl:value-of select="."/>
                        </td>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>