News Ticker

Menu

Bài viết phổ biến

Java

Python

Django

XML

Recent Posts

XSLT_Bài tập.

Thursday, May 19, 2016 / No Comments

<XML_XSLT> Bài 10: Bài tập trên lớp về XSLT.

1. FIle XML dowload  student.xml student.html

2. FIle XSLT dowload student.xsl

3. Tổng hợp tất cả các bài tập: XML_Tổng hợp tất cả bài tập

XSLT

/ No Comments

<XML_XSLT> Bài 9: Tổng quan.

Cũng như nhưng ngôn ngữ lập trình khác trong XSLT cũng thực hiện theo kiểu có duy nhất một chương trình chính <main> để thực hiện các yêu cầu và có các function bên trong. Và có các chương trình con<Function>.

Cú pháp.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="text"/>
<xsl:template match="/">

</xsl:template>
</xsl:stylesheet>

1. Biến và tham số

1.1 Biến <Variable>

Khai báo:

<xsl:variable name="tên biến" select="Giá trị của biến"/>

Trong XSLT giá trị của biến là cố định không thay đổi.

Ví dụ: Tính tổng 3 + 2.

<xsl:template name="add">
<xsl:variable name="a" select="3"/>
<xsl:variable name="b" select="2"/>
<xsl:value-of select="$a + $b"/>
</xsl:template>

Gọi lại trong hàm main<main template>

<xsl:call-template name="add"/>

1.2 Tham số

Biến là giá trị cố định không thể thay đổi nên trong XSLT ta có thể sử dụng tham số để thao tác khi có sự thay đổi giá trị.

Khai báo: 

<xsl:param name="tên tham số" select="Giá trị"/>
Truyền giá trị:
<xsl:with-param name="tên tham số" select="giá trị"/>

Ví dụ: Tính tổng 3 + 2

<xsl:template name="add2">
<xsl:param name="a" />
<xsl:param name="b"/>
<xsl:value-of select="$a + $b"/>
</xsl:template>

main template.

<xsl:call-template name="add">
<xsl:with-param name="a" select="3"/>
<xsl:with-param name="b" select="2"/>
</xsl:call-template>

1.2 Lệnh if trong XSLT

Cú pháp:

<xsl:if test="Điều kiện">
  ...Câu lệnh...
</xsl:if>

Lệnh if kiểm tra điều kiện: Nếu điều kiện đúng thì thực hiện câu lệnh.

Ví dụ:

<xsl:template match="/">
<xsl:variable name="test" select="5"/>
<xsl:if test="$test &gt; 5">
Gia tri cua bien test lon hon 5
</xsl:if>
<xsl:if test="$test &lt; 5">
Gia tri cua bien test nho hon 5
</xsl:if>
<xsl:if test="$test = 5">
Gia tri cua bien test bang 5
</xsl:if>

</xsl:template>

Kết quả : Gia tri cua bien test bang 5

1.3 Choose trong XSLT

Cú pháp: 

<xsl:choose>
  <xsl:when test="Điều kiện">
    ... some output ...
  </xsl:when>
  <xsl:otherwise>
    ... some output ....
  </xsl:otherwise>
</xsl:choose>

Kiểm tra điều kiện nếu đúng thì thực hiện câu lệnh bên trong không thì sẽ thực hiện câu lệnh trong "otherwise"

VD:

<xsl:template match="/">
<xsl:variable name="test" select="5"/>
<xsl:choose>
<xsl:when test="$test &gt; 5">
Gia tri cua bien test lon hon 5
</xsl:when>
<xsl:when test="$test &lt; 5">
Gia tri cua bien test nho hon 5
</xsl:when>
<xsl:otherwise>
Gia tri cua bien test bang 5
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Kết quả : Gia tri cua bien test bang 5

1.4 Vòng lặp For

Trong XSLT không có câu lệnh lặp For vậy nên ta phải tự viết vòng lặp cho nó. Dựa trên những gì mà XSLT cho phép sử dụng : IF , Param<tham số> .

VD: Bài toán in ra dẫy số nguyên từ 1 - n .

<XSLT>Lặp lùi:

<xsl:template name="for_lui">
<xsl:param name="min" select="1"/>
<xsl:param name="n"/>
<xsl:if test="$min = $n">
<xsl:value-of select="$n"/>
</xsl:if>

<xsl:if test="$min &lt; $n">
<xsl:value-of select="$n"/>
<xsl:call-template name="for_lui">
<xsl:with-param name="min" select="$min"/>
<xsl:with-param name="n" select="$n - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

<XSLT>Lặp tiến:

<xsl:template name="for_tien">
<xsl:param name="min" select="1"/>
<xsl:param name="n"/>
<xsl:if test="$min = $n">
<xsl:value-of select="$n"/>
</xsl:if>

<xsl:if test="$min &lt; $n">
<xsl:value-of select="$min"/>
<xsl:call-template name="for_tien">
<xsl:with-param name="min" select="$min + 1"/>
<xsl:with-param name="n" select="$n"/>
</xsl:call-template>
</xsl:if>
</xsl:template>

main template : 

<xsl:call-template name="for_lui">
<xsl:with-param name="n" select="5"/>
</xsl:call-template>

Kết quả : 54321

<xsl:call-template name="for_tien">
<xsl:with-param name="n" select="5"/>
</xsl:call-template>

Kết quả : 12345

1.5 For-each

For-each thường dùng để lặp trong XSLT 

Cú pháp: 


<xsl:for-each select="student/courses/course">

....Câu lệnh...

</xsl:for-each>

student/courses/course là XPath để trỏ tới địa chỉ cần lặp.
Lặp tất cả các "course" trong "courses" của "student".

Ví dụ:

<xsl:for-each select="student/courses/course">
<xsl:sort select="@score" order="ascending"/>
<xsl:if test="@score &gt; 8">
<tr class="goodScore">
<td><p><xsl:value-of select="position()"/></p></td>
<td><p><xsl:value-of select="@courseID"/></p>
</td>
<td><p><xsl:value-of select="title"/></p>
</td>
<td><p><xsl:value-of select="@unit"/></p></td>
<td><p><xsl:value-of select="@score"/></p></td>
</tr>
</xsl:if>
<xsl:if test="@score &lt; 5">
<tr class="badScore">
<td><p><xsl:value-of select="position()"/></p></td>
<td><p><xsl:value-of select="@courseID"/></p>
</td>
<td><p><xsl:value-of select="title"/></p>
</td>
<td><p><xsl:value-of select="@unit"/></p></td>
<td><p><xsl:value-of select="@score"/></p></td>
</tr>
</xsl:if>
<xsl:if test="(@score &gt;= 5 ) and (@score &lt;= 8)">
<tr>
<td><p><xsl:value-of select="position()"/></p></td>
<td><p><xsl:value-of select="@courseID"/></p>
</td>
<td><p><xsl:value-of select="title"/></p>
</td>
<td><p><xsl:value-of select="@unit"/></p></td>
<td><p><xsl:value-of select="@score"/></p></td>
</tr>
</xsl:if>
</xsl:for-each>



Chúc các bạn thành công!