如何突出显示XSLT中的特定行

时间:2014-09-23 11:12:26

标签: html css xml xslt

我正在尝试突出显示元素"年"价值高于" 2000"在XML和XSLT中。

我有这段代码:

XML

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="harrypotterbooks.xsl"?>
<!DOCTYPE catalog [
<!ELEMENT catalog (book)>
<!ELEMENT book          (title,author,country,publisher,price,year)>
<!ELEMENT title         (#PCDATA)>
<!ELEMENT author        (#PCDATA)>
<!ELEMENT country       (#PCDATA)>
<!ELEMENT publisher     (#PCDATA)>
<!ELEMENT price         (#PCDATA)>
<!ELEMENT year          (#PCDATA)>
]>

<catalog>
    <book>
        <title>Harry Potter and the Philosophers Stone</title>
        <author>J.K Rowling</author>
        <country>United Kingdom</country>
        <publisher>Bloomsbury</publisher>
        <price>$11.99</price>
        <year>26/06/1997</year>
    </book>
    <book>
        <title>Harry Potter and the Chamber of Secrets</title>
        <author>J.K Rowling</author>
        <country>United Kingdom</country>
        <publisher>Bloomsbury</publisher>
        <price>$13.99</price>
        <year>02/07/1998</year>
    </book>
    <book>
        <title>Harry Potter and the Prisoner of Azkaban</title>
        <author>J.K Rowling</author>
        <country>United Kingdom</country>
        <publisher>Bloomsbury</publisher>
        <price>$12.99</price>
        <year>08/07/1999</year>
    </book>
    <book>
        <title>Harry Potter and the Goblet of Fire</title>
        <author>J.K Rowling</author>
        <country>United Kingdom</country>
        <publisher>Bloomsbury</publisher>
        <price>$16.99</price>
        <year>08/07/2000</year>
    </book>
    <book>
        <title>Harry Potter and the Order of the Phoenix</title>
        <author>J.K Rowling</author>
        <country>United Kingdom</country>
        <publisher>Bloomsbury</publisher>
        <price>$17.99</price>
        <year>21/06/2003</year>
    </book>
    <book>
        <title>Harry Potter and the Half Blood Prince</title>
        <author>J.K Rowling</author>
        <country>United Kingdom</country>
        <publisher>Bloomsbury</publisher>
        <price>$19.99</price>
        <year>16/07/2005</year>
    </book>
    <book>
        <title>Harry Potter and the Deathly Hallows</title>
        <author>J.K Rowling</author>
        <country>United Kingdom</country>
        <publisher>Bloomsbury</publisher>
        <price>$25.99</price>
        <year>21/07/2007</year>
    </book>
</catalog>

和这个XSL

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <html>
    <body>
    <h2>My Book Collection</h2>
    <table border="1">
        <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Author</th>
            <th>Country</th>
            <th>Publisher</th>
            <th>Price</th>
            <th>Year of Release</th>
        </tr>
        <xsl:for-each select="catalog/book">
        <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="author"/></td>
            <td><xsl:value-of select="country"/></td>
            <td><xsl:value-of select="publisher"/></td>
            <td><xsl:value-of select="price"/></td>
            <td><xsl:value-of select="year"/></td>
        </tr>
        </xsl:for-each>
    </table>
    </body>
    </html>
</xsl:template>

</xsl:stylesheet>

什么是使用&#34;年&#34;突出显示任何行的最佳方式?价值高于&#34; 2000&#34;?

提前致谢

1 个答案:

答案 0 :(得分:2)

您可以测试年份并添加样式属性,以更改year大于2000的行的CSS样式,如下所示:

    <tr>
      <xsl:if test="substring(year, string-length(year)-3) > 2000">
        <xsl:attribute name="style">background-color: yellow</xsl:attribute>
      </xsl:if>

输出将看起来像在HTML中呈现:

enter image description here

完整的工作示例如下......

您的输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="harrypotterbooks.xsl"?>
<!DOCTYPE catalog [
<!ELEMENT catalog (book)>
<!ELEMENT book          (title,author,country,publisher,price,year)>
<!ELEMENT title         (#PCDATA)>
<!ELEMENT author        (#PCDATA)>
<!ELEMENT country       (#PCDATA)>
<!ELEMENT publisher     (#PCDATA)>
<!ELEMENT price         (#PCDATA)>
<!ELEMENT year          (#PCDATA)>
]>

<catalog>
  <book>
    <title>Harry Potter and the Philosophers Stone</title>
    <author>J.K Rowling</author>
    <country>United Kingdom</country>
    <publisher>Bloomsbury</publisher>
    <price>$11.99</price>
    <year>26/06/1997</year>
  </book>
  <book>
    <title>Harry Potter and the Chamber of Secrets</title>
    <author>J.K Rowling</author>
    <country>United Kingdom</country>
    <publisher>Bloomsbury</publisher>
    <price>$13.99</price>
    <year>02/07/1998</year>
  </book>
  <book>
    <title>Harry Potter and the Prisoner of Azkaban</title>
    <author>J.K Rowling</author>
    <country>United Kingdom</country>
    <publisher>Bloomsbury</publisher>
    <price>$12.99</price>
    <year>08/07/1999</year>
  </book>
  <book>
    <title>Harry Potter and the Goblet of Fire</title>
    <author>J.K Rowling</author>
    <country>United Kingdom</country>
    <publisher>Bloomsbury</publisher>
    <price>$16.99</price>
    <year>08/07/2000</year>
  </book>
  <book>
    <title>Harry Potter and the Order of the Phoenix</title>
    <author>J.K Rowling</author>
    <country>United Kingdom</country>
    <publisher>Bloomsbury</publisher>
    <price>$17.99</price>
    <year>21/06/2003</year>
  </book>
  <book>
    <title>Harry Potter and the Half Blood Prince</title>
    <author>J.K Rowling</author>
    <country>United Kingdom</country>
    <publisher>Bloomsbury</publisher>
    <price>$19.99</price>
    <year>16/07/2005</year>
  </book>
  <book>
    <title>Harry Potter and the Deathly Hallows</title>
    <author>J.K Rowling</author>
    <country>United Kingdom</country>
    <publisher>Bloomsbury</publisher>
    <price>$25.99</price>
    <year>21/07/2007</year>
  </book>
</catalog>

鉴于此XSLT:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <html>
      <body>
        <h2>My Book Collection</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Author</th>
            <th>Country</th>
            <th>Publisher</th>
            <th>Price</th>
            <th>Year of Release</th>
          </tr>
          <xsl:for-each select="catalog/book">
            <tr>
              <xsl:if test="substring(year, string-length(year)-3) > 2000">
                <xsl:attribute name="style">background-color: yellow</xsl:attribute>
              </xsl:if>
              <td><xsl:value-of select="title"/></td>
              <td><xsl:value-of select="author"/></td>
              <td><xsl:value-of select="country"/></td>
              <td><xsl:value-of select="publisher"/></td>
              <td><xsl:value-of select="price"/></td>
              <td><xsl:value-of select="year"/></td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>

</xsl:stylesheet>

生成所需的输出HTML:

<html>
   <body>
      <h2>My Book Collection</h2>
      <table border="1">
         <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Author</th>
            <th>Country</th>
            <th>Publisher</th>
            <th>Price</th>
            <th>Year of Release</th>
         </tr>
         <tr>
            <td>Harry Potter and the Philosophers Stone</td>
            <td>J.K Rowling</td>
            <td>United Kingdom</td>
            <td>Bloomsbury</td>
            <td>$11.99</td>
            <td>26/06/1997</td>
         </tr>
         <tr>
            <td>Harry Potter and the Chamber of Secrets</td>
            <td>J.K Rowling</td>
            <td>United Kingdom</td>
            <td>Bloomsbury</td>
            <td>$13.99</td>
            <td>02/07/1998</td>
         </tr>
         <tr>
            <td>Harry Potter and the Prisoner of Azkaban</td>
            <td>J.K Rowling</td>
            <td>United Kingdom</td>
            <td>Bloomsbury</td>
            <td>$12.99</td>
            <td>08/07/1999</td>
         </tr>
         <tr>
            <td>Harry Potter and the Goblet of Fire</td>
            <td>J.K Rowling</td>
            <td>United Kingdom</td>
            <td>Bloomsbury</td>
            <td>$16.99</td>
            <td>08/07/2000</td>
         </tr>
         <tr style="background-color: yellow">
            <td>Harry Potter and the Order of the Phoenix</td>
            <td>J.K Rowling</td>
            <td>United Kingdom</td>
            <td>Bloomsbury</td>
            <td>$17.99</td>
            <td>21/06/2003</td>
         </tr>
         <tr style="background-color: yellow">
            <td>Harry Potter and the Half Blood Prince</td>
            <td>J.K Rowling</td>
            <td>United Kingdom</td>
            <td>Bloomsbury</td>
            <td>$19.99</td>
            <td>16/07/2005</td>
         </tr>
         <tr style="background-color: yellow">
            <td>Harry Potter and the Deathly Hallows</td>
            <td>J.K Rowling</td>
            <td>United Kingdom</td>
            <td>Bloomsbury</td>
            <td>$25.99</td>
            <td>21/07/2007</td>
         </tr>
      </table>
   </body>
</html>