Как выделить определенную строку в XSLT

Я пытаюсь выделить строки, которые имеют элемент "год" со значением, превышающим "2000" в XML и XSLT.

У меня есть этот код:

XML

<!--?xml version="1.0" encoding="UTF-8"?-->
<!--?xml-stylesheet type="text/xsl" href="harrypotterbooks.xsl"?-->

<!--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 *******</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="/">
 
 
 <h2>My Book Collection</h2>
 <xsl:for-each select="catalog/book">
 </xsl:for-each><table border="1">
 <tbody><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>
 
 
 
 
 
 
 </tr>
 
 </tbody></table>
 
 
</xsl:template>

</xsl:stylesheet>

Каков наилучший способ выделить любую строку со значением "year" выше, чем "2000"?

заранее спасибо

1 ответ

Вы можете протестировать год и добавить атрибут стиля, чтобы изменить стиль CSS для строк с year больше 2000, например:

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

Результат будет выглядеть так, как показано в HTML:

Ниже приведен полный рабочий пример...

Входной XML:

<!--?xml version="1.0" encoding="UTF-8"?-->
<!--?xml-stylesheet type="text/xsl" href="harrypotterbooks.xsl"?-->

<!--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 *******</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="/">
 
 
 <h2>My Book Collection</h2>
 <xsl:for-each select="catalog/book">
 </xsl:for-each><xsl:if test="substring(year, string-length(year)-3) > 2000">
 <xsl:attribute name="style">background-color: yellow</xsl:attribute>
 </xsl:if><table border="1">
 <tbody><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>
 
 
 
 
 
 
 
 </tr>
 
 </tbody></table>
 
 
 </xsl:template>

</xsl:stylesheet>

Производит желаемый вывод HTML:

<h2>My Book Collection</h2>
 <table border="1">
 <tbody><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>
 <td>Harry Potter and the Order of the *******</td>
 <td>J.K Rowling</td>
 <td>United Kingdom</td>
 <td>Bloomsbury</td>
 <td>$17.99</td>
 <td>21/06/2003</td>
 </tr>
 <tr>
 <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>
 <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>
 </tbody></table>

licensed under cc by-sa 3.0 with attribution.