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

标签 html css xml xslt

我试图在 XML 和 XSLT 中突出显示元素“年”的值高于“2000”的行。

我有这个代码:

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>

突出显示“年份”值高于“2000”的任何行的最佳方法是什么?

提前致谢

最佳答案

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

    <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>

关于html - 如何突出显示 XSLT 中的特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25993610/

相关文章:

html - 输入边框 CSS

javascript - 如何使悬停字幕正常工作?

css - HTML 元素之间的空白是否重要?

javascript - 如何使用 jQuery 为文本链接设置动画?

android - 在 gradle android 闭包中定义时,是否必须在 AndroidManifest.xml 中设置 SDKVersion?

css - 列表项链接的奇怪继承

Javascript 监听器捕获当前加载资源的 URL

python - 我如何阻止漂亮的汤在解析时跳过行?

c# - XMLWriterTraceListener 生成文本墙而不是格式化的 XML

html - 无法在既没有 CSS 也没有 cellspacing 属性的 <td> 标签上放置边距