document type definition xsl va xslt
TRANSCRIPT
![Page 1: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/1.jpg)
XSL Transformation 1
XSL và XSLTCBGD: Nguyễn Trần Thi
Văn
NGÔN NGỮ ĐÁNH DẤU MỞ RỘNG
CHƯƠNG 06
![Page 2: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/2.jpg)
Nội dung Mục tiêu của bài học Không gian tên (namespaces) XSLT là gì? Cơ chế so khớp Lựa chọn nút Lựa chọn nội dung dữ liệu Sao chép nội dung của nút Đếm số nút Khai báo hằng (constants) Lựa chọn (rẽ nhánh)
2XSL Transformation
![Page 3: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/3.jpg)
3
Mục tiêu Trình bày được ý nghĩa và cách khai
báo, sử dụng không gian tên trong tài liệu XML.
Tạo được tập tin XSL và sử dụng tập tin này để định dạng cho tài liệu XML.
Biết cách so khớp để lựa chọn được những nội dung cần định dạng.
Sử dụng được một số hàm có sẵn của XSL để thực hiện lựa chọn phần tử theo điều kiện.
XSL Transformation
![Page 4: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/4.jpg)
Nội dung Mục tiêu của bài học Không gian tên (namespaces) XSLT là gì? Cơ chế so khớp Lựa chọn nút Lựa chọn nội dung dữ liệu Sao chép nội dung của nút Đếm số nút Khai báo hằng (constants) Lựa chọn (rẽ nhánh)
4XSL Transformation
![Page 5: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/5.jpg)
XML cho phép chúng ta tự do định nghĩa các thẻ.
Các thẻ cùng tên có thể chứa dữ liệu khác nhau và mang ý nghĩa khác nhau.
Ví dụ:
5
Không gian tên (namespaces)
<author> <title>Mr.</title> <name>John Smith</name></author><!-- … --><book> <title>Gone With The Wind</title></book>
XSL Transformation
![Page 6: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/6.jpg)
Để giải quyết vấn đề này, ta sử dụng không gian tên (namespaces).
Trong mỗi không gian tên, các thẻ phải có tên duy nhất (không được trùng tên nhau).
Hai thẻ trong 2 không gian tên khác nhau có thể trùng tên, nhưng khi ấy sẽ mang 2 ý nghĩa khác nhau.
6
Không gian tên (namespaces)
XSL Transformation
![Page 7: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/7.jpg)
Khai báo không gian tên:Ta sử dụng cú pháp xmlns:prefix Ví dụ:
<?xml version="1.0"?> <BookOrder
xmlns:hs="http://www.northwindtraders.com/customer">
<hs:Customer > <hs:Title>Mr.</Title> <hs:FirstName>Graeme</FirstName> <hs:LastName>Malcolm</LastName> </hs:Customer> </BookOrder>
7
Không gian tên (namespaces)
XSL Transformation
![Page 8: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/8.jpg)
Không gian tên mặc định: khi tài liệu chỉ có 1 không gian tên duy nhất, ta sử dụng cú pháp xmlns, không có prefix
Ví dụ:<?xml version="1.0"?> <BookOrder
xmlns="http://www.northwindtraders.com/customer">
<Customer > <Title>Mr.</Title> <FirstName>Graeme</FirstName> <LastName>Malcolm</LastName> </Customer> </BookOrder>
8
Không gian tên (namespaces)
XSL Transformation
![Page 9: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/9.jpg)
Khi có nhiều không gian tên mặc định?<?xml version="1.0"?> <BookOrder > <OrderDate>2001-01-01</OrderDate> <Customer
xmlns="http://www.northwindtraders.com/customer">
<Title>Mr.</Title> <FirstName>Graeme</FirstName> <LastName>Malcolm</LastName> </Customer> <Book
xmlns="http://www.northwindtraders.com/book"> <Title>Treasure Island</Title> <Author>Robert Louis Stevenson</Author> </Book> </BookOrder>
9
Không gian tên (namespaces)
XSL Transformation
![Page 10: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/10.jpg)
<?xml version="1.0"?> <BookOrder
xmlns="http://www.northwindtraders.com/order"
xmlns:cus="http://www.northwindtraders.com/customer"
xmlns:bok ="http://www.northwindtraders.com/book"
> <OrderDate>2001-01-01</OrderDate> <cus:Customer > <cus:Title>Mr.</cus:Title> <cus:FirstName>Graeme</cus:FirstName> <cus:LastName>Malcolm</cus:LastName> </cus:Customer> <bok:Book > <bok:Title>Treasure Island</bok:Title> <bok:Author>Robert Louis Stevenson</bok:Author> </bok:Book> </BookOrder
10
Không gian tên (namespaces).
XSL Transformation
![Page 11: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/11.jpg)
Nội dung Mục tiêu của bài học Không gian tên (namespaces) XSLT là gì? Cơ chế so khớp Lựa chọn nút Lựa chọn nội dung dữ liệu Sao chép nội dung của nút Đếm số nút Khai báo hằng (constants) Lựa chọn (rẽ nhánh)
11XSL Transformation
![Page 12: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/12.jpg)
XSLT được viết tắt từ cụm từ: eXtensible Style Language Transformation.
Là một trong 2 nhánh của ngôn ngữ XSL: XSLT XSL-FO (Formatting Objects)
Là ngôn ngữ con của XML, dùng để rút trích dữ liệu, chuyển dạng, định dạng tài liệu XML.
12
XSLT là gì?
XSL Transformation
![Page 13: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/13.jpg)
XSLprocessor
XMLdocumen
t
XSL stylesheet
texthtmlxml…
13
Định dạng tài liệu bằng XSL
XSL Transformation
![Page 14: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/14.jpg)
14
Các loại nút trong tài liệu
Nodes
Các loại nút mà XSLT có thể xử lý:
XSL Transformation
![Page 15: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/15.jpg)
Tài liệu XSL phải được đặt trong cặp thẻ hay phần tử <xsl:stylesheet>
<xsl:stylesheet>...</xsl:stylesheet>
15
Tài liệu chuyển dạng XSL
XSL Transformation
![Page 16: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/16.jpg)
Khai báo:
<xsl:stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”xmlns:fo="http://www.w3.org/1999/XSL/Format" >
16
Hiển thị tài liệu XML dùng XSL
<?xml-stylesheet type=”text/xsl” href=”vidu1.xsl”?>
XML file
XSL file
XSL Transformation
![Page 17: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/17.jpg)
Thẻ <xsl:template> được sử dụng để định nghĩa 1 khuôn mẫu. Đây chính là cách để ta rút trích thông tin trong tài liệu XML gốc và trình bày nó trong tài liệu mới (XML hoặc HTML) <xsl:template
match=”/”>...</xsl:template>
17
Thẻ xsl:template (1)
XSL Transformation
![Page 18: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/18.jpg)
Thuộc tính match dùng để chỉ định xem template này sẽ được so khớp với nút nào trong tài liệu XML ban đầu.
Ví dụ: template sau dùng để so khớp với nút gốc của tài liệu. <xsl:template
match=“/”>...</xsl:template>
18
Thẻ xsl:template (2.)
XSL Transformation
![Page 19: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/19.jpg)
Thẻ <xsl:apply-templates> được dùng để thế chỗ cho nơi dữ liệu XML sẽ được đặt vào tài liệu mới.
<xsl:template match="/"><html><xsl:apply-templates/></html>
</xsl:template>
19
Thẻ xsl:apply-templates
XSL Transformation
![Page 20: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/20.jpg)
Dùng để chỉ định template nào sẽ được thế vào vị trí của thẻ <xsl:apply-templates> tương ứng.
<xsl:template match="/"><html><xsl:apply-templates select="PLANETS"/></html>
</xsl:template><xsl:template match="PLANETS">
Planets found...</xsl:template>
20
Thuộc tính select
XSL Transformation
![Page 21: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/21.jpg)
Thẻ <xsl:value-of> được sử dụng để lấy ra nội dung (giá trị) của một nút nào đó và đưa vào tài liệu đích
Ví dụ: để lấy ra nội dung của thẻ NAME (bên trong thẻ PLANET), ta tạo template như sau:<xsl:template match="PLANET">
<p><xsl:value-of select="NAME"/></p>
</xsl:template>
21
Thẻ xsl:value-of (1)
XSL Transformation
![Page 22: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/22.jpg)
Nếu muốn lấy nội dung của nút đang xét, ta gán giá trị “.” cho thuộc tính select.
Ví dụ:<xsl:template match=“NAME">
<p><xsl:value-of select=“."/>
</p></xsl:template>
22
Thẻ xsl:value-of (2)
XSL Transformation
![Page 23: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/23.jpg)
<xsl:template match="PLANET"><xsl:for-each select="NAME">
<p><xsl:value-of select="."/>
</p></xsl:for-each>
</xsl:template>
Thẻ xsl:for-each
<xsl:template match="PLANET"><xsl:for-each>
<p><xsl:value-of
select=“NAME"/></p>
</xsl:for-each></xsl:template>
23
Thẻ <xsl:for-each> dùng để duyệt qua nhiều nút trùng tên nhau.
Ví dụ:
XSL Transformation
![Page 24: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/24.jpg)
Nội dung Mục tiêu của bài học XSLT là gì? Không gian tên (namespaces) Cơ chế so khớp Lựa chọn nút Lựa chọn nội dung dữ liệu Sao chép nội dung của nút Đếm số nút Khai báo hằng (constants) Lựa chọn (rẽ nhánh)
24XSL Transformation
![Page 25: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/25.jpg)
So khớp nút gốc So khớp theo tên phần tử So khớp các nút con bằng / So khớp các nút con cháu // So khớp theo ID So khớp thuộc tính bằng @ So khớp nội dung ghi chú bằng
comment() So khớp chỉ thị xử lý bằng processing-
instruction() So khớp nội dung văn bản bằng text()
25
Cơ chế so khớp các nút
XSL Transformation
![Page 26: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/26.jpg)
Khi ta dùng ký tự / cho thuộc tính match thì có nghĩa là trình duyệt bắt đầu phân tích từ nút gốc của cây tài liệu đầu vào
<xsl:template match="/"> <html> <xsl:apply-templates/> </html></xsl:template>
26
So khớp nút gốc
XSL Transformation
![Page 27: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/27.jpg)
Ta có thể tìm hoặc so khớp 1 phần tử đơn giản bằng cách chỉ định tên của phần tử đó
Ví dụ: so khớp các thẻ có tên là PLANET
<xsl:template match="PLANET"><p><xsl:value-of select="NAME"/></p>
</xsl:template>
27
So khớp theo tên thẻ
XSL Transformation
![Page 28: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/28.jpg)
Để so khớp và chọn thẻ NAME là con của thẻ PLANET, ta viết như sau:
<xsl:template match = “PLANET/NAME”><H3><xsl:value-of select = “.”/></H3>
</xsl:template>
28
So khớp các nút con
XSL Transformation
![Page 29: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/29.jpg)
Biểu thức PLANETS//NAME sẽ chọn mọi thẻ NAME nằm bên trong thẻ PLANETS, không phân biệt mức độ lồng nhau
<xsl:template match=”PLANETS//NAME”><xsl:value-of select=”.”/>
</xsl:template>
29
So khớp các nút con cháu
XSL Transformation
![Page 30: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/30.jpg)
Ta có thể dựa vào ID (định danh duy nhất) để so khớp và chọn ra một thẻ nào đó.
Ví dụ:<xsl:template match=”id(‘P12’)”>
<xsl:value-of select=”.”/></xsl:template>
30
So khớp bằng ID
XSL Transformation
![Page 31: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/31.jpg)
Ngoài việc lấy ra nội dung dữ liệu text của một thẻ, ta có thể lấy ra giá trị thuộc tính của thẻ bằng cách sử dụng ký hiệu @
Ví dụ:<xsl:template match="RADIUS">
<xsl:value-of select="."/><xsl:value-of
select="@UNITS"/></xsl:template>
31
So khớp thuộc tính bằng @
XSL Transformation
![Page 32: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/32.jpg)
Ta cũng có thể tìm và rút trích nội dung của các dòng chú thích trong tài liệu bằng cách sử dụng biểu thức comment()
Ví dụ:<xsl:template match=”comment()”>
<p><xsl:value-of select=”.”/></p>
</xsl:template>
32
So khớp nội dung ghi chú
XSL Transformation
![Page 33: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/33.jpg)
Chỉ thị xử lý đặt ở đầu tài liệu cũng có thể được XSL rút trích bằng biểu thức processing-instruction()
Ví dụ:<xsl:template match="/processing-instruction('xml-stylesheet')"><p><i><xsl:value-of select="."/></i></p>
</xsl:template>
33
So khớp chỉ thị xử lý
XSL Transformation
![Page 34: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/34.jpg)
Ta cũng có thể tìm và rút trích phần nội dung của tất cả các thẻ bằng cách sử dụng biểu thức text()
Ví dụ:<xsl:template match=”text()”><xsl:value-of select=”.”/>
</xsl:template>
34
So khớp nội dung văn bản
XSL Transformation
![Page 35: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/35.jpg)
Ta dùng toán tử OR để chỉ điều kiện hợp
Ví dụ: để so khớp những thẻ mang tên NAME hoặc MASS, ta viết như sau:<xsl:template match=”NAME | MASS”>
<p><xsl:apply-templates/>
</p></xsl:template>
35
Sử dụng toán tử OR |
XSL Transformation
![Page 36: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/36.jpg)
Ta dùng toán tử [ ] để kiểm tra một điều kiện nào đó
Ví dụ:<xsl:template match="PLANET[AB='5']">
<p><h1><xsl:apply-templates/></h1></p>
</xsl:template>
36
Kiểm tra điều kiện bằng [ ]
XSL Transformation
![Page 37: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/37.jpg)
Nội dung Mục tiêu của bài học XSLT là gì? Không gian tên (namespaces) Cơ chế so khớp Lựa chọn nút Lựa chọn nội dung dữ liệu Sao chép nội dung của nút Đếm số nút Khai báo hằng (constants) Lựa chọn (rẽ nhánh)
37XSL Transformation
![Page 38: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/38.jpg)
Ta có thể sử dụng các hàm và biểu thức do XSL cung cấp để thực hiện việc so khớp và lựa chọn phức tạp hơn
Các hàm này được chia thành 2 nhóm chính như sau:– Các hàm khoanh vùng vị trí tìm kiếm
trong cây– Các hàm liên quan đến biểu thức điều kiện
38
Sử dụng biểu thức để lựa chọn
XSL Transformation
![Page 39: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/39.jpg)
Ta có các hàm sau: from-ancestors() from-ancestors-or-self() from-attributes() from-children() from-descendants() from-descendants-or-self() from-following() from-following-siblings() from-parent() from-preceding() from-preceding-siblings() from-self()
39
Khoanh vùng tìm kiếm
XSL Transformation
![Page 40: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/40.jpg)
Ví dụ: tìm kiếm nút SYMBOL trên các nút cha của nút PERIODIC_TABLE
<xsl:template match=”SYMBOL”><xsl:value-of select=“from-ancestors(PERIODIC_TABLE)”/></xsl:template>
40
Khoanh vùng tìm kiếm
XSL Transformation
![Page 41: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/41.jpg)
Có 4 loại biểu thức như sau:
41
Các dạng biểu thức xử lý
XSL Transformation
![Page 42: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/42.jpg)
42
Tập hợp nút
Vị trí của nút hiện tại
Nút cuối cùng
Đếm số lượng nút
XSL Transformation
![Page 43: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/43.jpg)
Các hàm có thể sử dụng đối với kiểu Boolean:
43
Kiểu đúng/sai
< <
<= <=
XSL Transformation
![Page 44: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/44.jpg)
Ví dụ: in ra nút ở vị trí đầu tiên và nút ở vị trí cuối cùng
<xsl:template match=”ATOMIC_NUMBER[position()=1 or position()=last()]”>
<xsl:value-of select=”.”/></xsl:template>
44
Kiểu đúng/sai
XSL Transformation
![Page 45: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/45.jpg)
Các phép toán và hàm xử lý trên kiểu số:
45
Kiểu số
XSL Transformation
![Page 46: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/46.jpg)
Ví dụ: chọn nút ATOM có giá trị thẻ con AUTOMIC_NUMBER > 92:<xsl:template match=”/PERIODIC_TABLE”>
<HTML><xsl:apply-templates
select=”ATOM[ATOMIC_NUMBER>92]”/></HTML>
</xsl:template>
46
Kiểu số
XSL Transformation
![Page 47: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/47.jpg)
Một số hàm xử lý trên kiểu chuỗi:
47
Kiểu chuỗi
XSL Transformation
![Page 48: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/48.jpg)
Tóm tắt bài học
48XSL Transformation
![Page 49: Document Type Definition xsl va xslt](https://reader035.vdocuments.site/reader035/viewer/2022062412/587693221a28abab2f8b5ec5/html5/thumbnails/49.jpg)
HẾT PHẦN 1
49XSL Transformation