根据数据值向XSL文件添加CSS

根据数据值向XSL文件添加CSS,css,xml,xslt,Css,Xml,Xslt,我有一个XSLT文件,它将XML文件显示到HTML页面中。有三个表,我希望maxtemp列下大于-2的数据具有绿色文本颜色。我尝试了,但它不起作用。有人能帮我吗 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="weatherdata.xsl"?> <weatherdata> <stationdata weatherdate="2015-1

我有一个XSLT文件,它将XML文件显示到HTML页面中。有三个表,我希望
maxtemp
列下大于-2的数据具有绿色文本颜色。我尝试了
,但它不起作用。有人能帮我吗

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="weatherdata.xsl"?>
<weatherdata>
    <stationdata weatherdate="2015-1-1" location="Calgary">
        <maxtemp>1.1°C</maxtemp>
        <mintemp>-6.1°C</mintemp>
        <totalrain>0 mm</totalrain>
        <totalsnow>0cm</totalsnow>
    </stationdata>
    <stationdata weatherdate="2015-1-2" location="Calgary">
        <maxtemp>-3.4°C</maxtemp>
        <mintemp>-18.2°C</mintemp>
        <totalrain>0 mm</totalrain>
        <totalsnow>5cm</totalsnow>
    </stationdata>
    <stationdata weatherdate="2015-1-3" location="Calgary">
        <maxtemp>-18.1°C</maxtemp>
        <mintemp>-21.1°C</mintemp>
        <totalrain>0 mm</totalrain>
        <totalsnow>1.6cm</totalsnow>
    </stationdata>
    <stationdata weatherdate="2015-01-01" location="Charlottetown">
        <maxtemp>-3.5°C</maxtemp>
        <mintemp>-15°C</mintemp>
        <totalrain>0 mm</totalrain>
        <totalsnow>0.4cm</totalsnow>
    </stationdata>
    <stationdata weatherdate="2015-01-02" location="Charlottetown">
        <maxtemp>-1°C</maxtemp>
        <mintemp>-13.2°C</mintemp>
        <totalrain>0 mm</totalrain>
        <totalsnow>0.6cm</totalsnow>
    </stationdata>
    <stationdata weatherdate="2015-01-03" location="Charlottetown">
        <maxtemp>-11.8°C</maxtemp>
        <mintemp>-16.1°C</mintemp>
        <totalrain>0 mm</totalrain>
        <totalsnow>0cm</totalsnow>
    </stationdata>   
    <stationdata weatherdate="2015-01-01" location="Ottawa">
        <maxtemp>-3°C</maxtemp>
        <mintemp>-8.1°C</mintemp>
        <totalrain>0 mm</totalrain>
        <totalsnow>0.2cm</totalsnow>
    </stationdata>
    <stationdata weatherdate="2015-01-02" location="Ottawa">
        <maxtemp>-3.8°C</maxtemp>
        <mintemp>-15.8°C</mintemp>
        <totalrain>0 mm</totalrain>
        <totalsnow>0cm</totalsnow>
    </stationdata>
    <stationdata weatherdate="2015-01-03" location="Ottawa">
        <maxtemp>-9.6°C</maxtemp>
        <mintemp>-15.5°C</mintemp>
        <totalrain>0 mm</totalrain>
        <totalsnow>18cm</totalsnow>
    </stationdata>
</weatherdata>

1.1摄氏度
-6.1摄氏度
0毫米
0厘米
-3.4摄氏度
-18.2摄氏度
0毫米
5厘米
-18.1摄氏度
-21.1摄氏度
0毫米
1.6厘米
-3.5摄氏度
-15摄氏度
0毫米
0.4厘米
-1摄氏度
-13.2摄氏度
0毫米
0.6厘米
-11.8摄氏度
-16.1摄氏度
0毫米
0厘米
-3摄氏度
-8.1摄氏度
0毫米
0.2厘米
-3.8摄氏度
-15.8摄氏度
0毫米
0厘米
-9.6摄氏度
-15.5摄氏度
0毫米
18厘米
这是我的XSL:

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

<xsl:variable name="thead">
    <tr>
        <th>Data</th>
        <th>Maximum Temperature(°C)</th>
        <th>Minimum Temperature(°C)</th>
        <th>Total Rain(mm)</th>
        <th>Total Snow(cm)</th>
    </tr>
</xsl:variable>

<xsl:template match="weatherdata">
    <html align="center">
        <head>
            <title>weather data</title>
            <style type="text/css">

        table, th, td {
       border: 1px solid black;
            }

          th{
          background-color:green;
          }      

 </style>

        </head>
        <body>
       <ul align="left" id="top">
       <li><a href="#Calgary">Calgary Weather</a></li>
       <li><a href="#Charlottetown">Charlottetown Weather</a></li>
       <li><a href="#Ottawa">Ottawa Weather</a></li>
       </ul>

            <h1 id="Calgary">Calgary – Meteorological Data (2015)</h1>
            <table border="1" align="center">
                <xsl:copy-of select="$thead"/>
                <xsl:apply-templates select="stationdata[@location='Calgary']"/>
            </table>
            <a href="#top">Back To Top</a>

            <h1 id="Charlottetown">Charlottetown – Meteorological Data (2015)</h1>
            <table border="1" align="center">
                <xsl:copy-of select="$thead"/>
                <xsl:apply-templates select="stationdata[@location='Charlottetown']"/>
            </table>

             <a href="#top">Back To Top</a>


            <h1 id="Ottawa">Ottawa – Meteorological Data (2015)</h1>
            <table border="1" align="center">
                <xsl:copy-of select="$thead"/>
                <xsl:apply-templates select="stationdata[@location='Ottawa']"/>
            </table>
             <a href="#top">Back To Top</a>
        </body>
    </html>
</xsl:template>

<xsl:template match="stationdata">
    <tr>
        <td><xsl:value-of select="@weatherdate"/></td>
                <xsl:choose>
          <xsl:when test="maxtemp &gt;'10'">
            <td color="yellow">
            <xsl:value-of select="maxtemp"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td><xsl:value-of select="maxtemp"/></td>
          </xsl:otherwise>
        </xsl:choose>  
        <td> <xsl:value-of select="mintemp"/></td>
        <td><xsl:value-of select="totalrain"/></td>
        <td><xsl:value-of select="totalsnow"/></td>
    </tr>
</xsl:template>

</xsl:stylesheet>

资料
最高温度(°C)
最低温度(°C)
总降雨量(毫米)
总雪量(厘米)
天气数据
表,th,td{
边框:1px纯黑;
}
th{
背景颜色:绿色;
}      
卡尔加里——气象数据(2015年) 夏洛特镇–气象数据(2015年) 渥太华——气象数据(2015年)
我修复了上面的XML,因为它的格式不好。关于比较
maxtemp
的问题:

<xsl:when test="maxtemp &gt;'10'">
下面是完整的XSLT

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

<xsl:variable name="thead">
    <tr>
        <th>Data</th>
        <th>Maximum Temperature(°C)</th>
        <th>Minimum Temperature(°C)</th>
        <th>Total Rain(mm)</th>
        <th>Total Snow(cm)</th>
    </tr>
</xsl:variable>

<xsl:template match="weatherdata">
    <html align="center">
        <head>
            <title>weather data</title>
            <style type="text/css">
              table, th, td {
                border: 1px solid black;
              }
              th{
                background-color:green;
              }  
             .Warmer { background-color: GreenYellow;  }
 </style>

        </head>
        <body>
       <ul align="left" id="top">
       <li><a href="#Calgary">Calgary Weather</a></li>
       <li><a href="#Charlottetown">Charlottetown Weather</a></li>
       <li><a href="#Ottawa">Ottawa Weather</a></li>
       </ul>

            <h1 id="Calgary">Calgary – Meteorological Data (2015)</h1>
            <table border="1" align="center">
                <xsl:copy-of select="$thead"/>
                <xsl:apply-templates select="stationdata[@location='Calgary']"/>
            </table>
            <a href="#top">Back To Top</a>

            <h1 id="Charlottetown">Charlottetown – Meteorological Data (2015)</h1>
            <table border="1" align="center">
                <xsl:copy-of select="$thead"/>
                <xsl:apply-templates select="stationdata[@location='Charlottetown']"/>
            </table>

             <a href="#top">Back To Top</a>


            <h1 id="Ottawa">Ottawa – Meteorological Data (2015)</h1>
            <table border="1" align="center">
                <xsl:copy-of select="$thead"/>
                <xsl:apply-templates select="stationdata[@location='Ottawa']"/>
            </table>
             <a href="#top">Back To Top</a>
        </body>
    </html>
</xsl:template>

<xsl:template match="stationdata">
    <tr>
          <td><xsl:value-of select="@weatherdate"/></td>
          <xsl:choose>
            <xsl:when test="substring-before(maxtemp, '°C') &gt; -2.0">
              <td class="Warmer">
              <xsl:value-of select="maxtemp"/></td>
            </xsl:when>
            <xsl:otherwise>
              <td><xsl:value-of select="maxtemp"/></td>
            </xsl:otherwise>
          </xsl:choose>  
          <td> <xsl:value-of select="mintemp"/></td>
          <td><xsl:value-of select="totalrain"/></td>
          <td><xsl:value-of select="totalsnow"/></td>
    </tr>
</xsl:template>
</xsl:stylesheet>

资料
最高温度(°C)
最低温度(°C)
总降雨量(毫米)
总雪量(厘米)
天气数据
表,th,td{
边框:1px纯黑;
}
th{
背景颜色:绿色;
}  
.warer{背景色:绿黄色;}
卡尔加里——气象数据(2015年) 夏洛特镇–气象数据(2015年) 渥太华——气象数据(2015年)
Thank you@zx485,您指出我正在将字符串与数字进行比较,我在(maxtemp,°C')之前找到了子字符串,我使用number()将其转换为munger,现在一切正常
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:variable name="thead">
    <tr>
        <th>Data</th>
        <th>Maximum Temperature(°C)</th>
        <th>Minimum Temperature(°C)</th>
        <th>Total Rain(mm)</th>
        <th>Total Snow(cm)</th>
    </tr>
</xsl:variable>

<xsl:template match="weatherdata">
    <html align="center">
        <head>
            <title>weather data</title>
            <style type="text/css">
              table, th, td {
                border: 1px solid black;
              }
              th{
                background-color:green;
              }  
             .Warmer { background-color: GreenYellow;  }
 </style>

        </head>
        <body>
       <ul align="left" id="top">
       <li><a href="#Calgary">Calgary Weather</a></li>
       <li><a href="#Charlottetown">Charlottetown Weather</a></li>
       <li><a href="#Ottawa">Ottawa Weather</a></li>
       </ul>

            <h1 id="Calgary">Calgary – Meteorological Data (2015)</h1>
            <table border="1" align="center">
                <xsl:copy-of select="$thead"/>
                <xsl:apply-templates select="stationdata[@location='Calgary']"/>
            </table>
            <a href="#top">Back To Top</a>

            <h1 id="Charlottetown">Charlottetown – Meteorological Data (2015)</h1>
            <table border="1" align="center">
                <xsl:copy-of select="$thead"/>
                <xsl:apply-templates select="stationdata[@location='Charlottetown']"/>
            </table>

             <a href="#top">Back To Top</a>


            <h1 id="Ottawa">Ottawa – Meteorological Data (2015)</h1>
            <table border="1" align="center">
                <xsl:copy-of select="$thead"/>
                <xsl:apply-templates select="stationdata[@location='Ottawa']"/>
            </table>
             <a href="#top">Back To Top</a>
        </body>
    </html>
</xsl:template>

<xsl:template match="stationdata">
    <tr>
          <td><xsl:value-of select="@weatherdate"/></td>
          <xsl:choose>
            <xsl:when test="substring-before(maxtemp, '°C') &gt; -2.0">
              <td class="Warmer">
              <xsl:value-of select="maxtemp"/></td>
            </xsl:when>
            <xsl:otherwise>
              <td><xsl:value-of select="maxtemp"/></td>
            </xsl:otherwise>
          </xsl:choose>  
          <td> <xsl:value-of select="mintemp"/></td>
          <td><xsl:value-of select="totalrain"/></td>
          <td><xsl:value-of select="totalsnow"/></td>
    </tr>
</xsl:template>
</xsl:stylesheet>