Bài 5
XSL Style Sheets (phần II)
Các lệnh về điều kiện
Giống như trong ngôn ngữ lập trình thông thường ta có các instructions về điều kiện như
IF, SELECT CASE, ELSE .v.v.. để lựa chọn, trong XSL ta có các lệnh về điều kiện như
xsl:if, xsl:choose, xsl:when, và xsl:otherwise. Khi expression của Element xsl:if,
xsl:when, hay xsl:otherwise có trị số true, thì cái Template nằm bên trong nó sẽ được tạo
ra (instantiated).
Thường thường, nếu công việc thử tính đơ
n giản ta dùng xsl:if. Nếu nó hơi rắc rối vì tùy
theo trường hợp ta phải làm những công tác khác nhau thì ta dùng
choose/when/otherwise.
Trị số của Attribute test của xsl:if và xsl:when là một expression để tính. Expression nầy
có thể là một so sánh hay một expression loại XPath. Kết quả việc tính nầy sẽ là true nếu
nó trả về một trong các trị số sau đây:
•
Một bộ node có ít nhất một node
•
Một con số khác zero
•
Một mảnh (fragment) Tree
•
Một text string không phải là trống rỗng (non-empty)
Để minh họa cách dùng các lệnh XSL về điều kiện ta sẽ dùng hồ sơ nguồn tên
catalog.xml sau đây:
<?xml version="1.0"?>
<catalog>
<book id="bk102">
<author>Ralls, Kim</author>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>After an inadvertant trip through a Heisenberg Uncertainty Device,
James Salway
discovers the problems of being quantum.</description>
</book>
<book id="bk110">
<author>O'Brien, Tim</author>
<title>Microsoft .NET: The Programming Bible</title>
<genre>Computer</genre>
<price>36.95</price>
<publish_date>2000-12-09</publish_date>
<description>Microsoft's .NET initiative is explored in detail in this deep
programmer's
reference.</description>
</book>
</catalog>
Dưới đây là một thí dụ dùng xsl:if:
<xsl:for-each select="//book">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:if test="price > 6">
<xsl:attribute name="bgcolor">cyan</xsl:attribute>
</xsl:if>
<xsl:value-of select="price"/>
</td>
xsl:if và xsl:when nói trên:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=" version="1.0">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE>Book Lovers' Catalog</TITLE>
</HEAD>
<BODY>
<Center>
<H1>Book Lovers' Catalog</H1>
</Center>
<TABLE Border="1" Cellpadding="5">
<TR>
<TD align="center" bgcolor="silver">
<b>ID</b>
</TD>
<TD align="center" bgcolor="silver">
<b>Author</b>
</TD>
<TD align="center" bgcolor="silver">
<b>Title</b>
</TD>
<TD align="center" bgcolor="silver">
<b>Genre</b>
</TD>
<TD align="center" bgcolor="silver">
<b>Price</b>
</TD>
<TD align="center" bgcolor="silver">
<TD>
<xsl:value-of select="genre"/>
</TD>
<TD>
<xsl:if test="price > 6">
<xsl:attribute name="bgcolor">cyan</xsl:attribute>
</xsl:if>
<xsl:value-of select="price"/>
</TD>
<TD>
<xsl:value-of select="publish_date"/>
</TD>
<TD>
<xsl:value-of select="description"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
Sau khi thêm câu:
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>
vào đầu hồ sơ catalog.xml, double click lên tên file catalog.xml, Internet Explorer sẽ
hiển thị kết quả sau:
Book Lovers' Catalog