1 / 20

Chương 5: XSLT …

2.9- Câu lệnh <xsl:apply-templates> 3 - Sử dụng các hàm của XSLT 4 - Sử dụng biến trong XSLT 5 - Transform trong Visual Basic.net (XML+XSLT) Transform  HTML, XML, Text. Chương 5: XSLT …. Giảng viên: Ngô Đình Thưởng Khoa Tin học, ĐHSP Đà Nẵng http://ndtfit.brinkster.net.

finian
Download Presentation

Chương 5: XSLT …

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2.9- Câu lệnh <xsl:apply-templates> 3 - Sử dụng các hàm của XSLT 4 - Sử dụng biến trong XSLT 5 - Transform trong Visual Basic.net (XML+XSLT) Transform HTML, XML, Text Chương 5:XSLT… Giảng viên: Ngô Đình Thưởng Khoa Tin học, ĐHSP Đà Nẵng http://ndtfit.brinkster.net

  2. 2.9. Phần tử <xsl:apply-templates> Chức năng: Áp dụng kiểu đã khai khai báo ở phần dưới Cú pháp : <?xsl version=“1.0”?> <xsl:stylesheet version=“1.0” xmlns:xsl= ....> .... <xsl-apply-templatesselect = phần_tử /> ...... <xsl-template match= phần_tử> <!- - trích dữ liệu với xsl:value-of select - -> </xsl-template> .... </xsl-stylesheet>

  3. Nhận xét: Đây là giải pháp chia nhỏ vấn đề, như tổ chức chương trình thành các module (hàm, thủ tục) Ví dụ : cho tệp PCT.XML dùng để chứa danh sách GV chủ nhiệm từng lớp. Hãy “in” ra danh sách. <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="PCT.XSL"?> <PCT nam="2010-2011" truong="Phan Châu Trinh" > <Khoi ten="12"> <Lop ten="12A">Phạm Đằng</Lop> <Lop ten="12B">Nguyễn Văn Hưng</Lop> </Khoi> <Khoi ten="11"> <Lop ten="11A">Phan Văn Anh</Lop> <Lop ten="11B">Nguyễn Văn Hưng</Lop> <Lop ten="11C">Giang Kim Liên</Lop> </Khoi> <!-- tương tự cho khối 10--> </PCT> Xem

  4. <?xml version="1.0"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body style="margin: 10 10 10 30; font:normal 14pt Arial"> <xsl:apply-templates select="PCT"/> </body></html> </xsl:template> <xsl:template match="PCT"> Trường:<xsl:value-of select="@truong"/></b> Danh sách GVCN các khối lớp , Năm học:<xsl:value-of select="@nam"/> <table style="width:300px" border="1"> <tr><td>Lớp</td><td>GVCN</td></tr> <xsl:apply-templates select="Khoi"/> </table> </xsl:template> <xsl:template match="Khoi"> <xsl:apply-templates select="Lop"/> </xsl:template> <xsl:template match="Lop"> <tr> <td><xsl:value-of select="@ten" /></td><td><xsl:value-of select="."/> </td> </tr> </xsl:template> </xsl:stylesheet>

  5. Có thể thay thế bằng tệp XSL sau đây, gọn hơn <?xml version="1.0"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><body> Trường: <xsl:value-of select="/PCT/@truong"/><br /> Năm học:<xsl:value-of select="/PCT/@nam"/> <table style="width:300px" border="1"> <tr><td>Lớp</td><td>GVCN</td></tr> <xsl:for-each select="//Lop"> <tr> <td><xsl:value-of select="@ten" /></td> <td><xsl:value-of select="."/> </td> </tr> </xsl:for-each> </table> </body></html> </xsl:template> </xsl:stylesheet>

  6. 3- Các hàm trong XSLT • Tổng quan: • - Các hàm về node: text(), name(), node() • Các hàm định vị : position(), last(), first(), count() • Các hàm số : sum(), number() • - Các hàm logic: boolean(), not(), true(), false() • - Các hàm chuổi: string(), string-length(), concat(), substring(), contains() • Có 13 hàm quan trọng!

  7. 3.1. Các hàm về node • - name() : trả về tên một node • text(node name) : trả về nội dung PCDATA của một node. • Ví dụ : <parent> Đây là Trần Văn Phương • <child> sinh viên lớp 08T1, DCT </child> • </parent> • ----------------------------------------------------------------------------------------- • <xsl:template match= “parent”> • <xsl:value-of select=“.”/> • </xsl:template> • ---------------------------------------------------------------------------------- • Kết quả trả về : Đây là Trần Văn Phương • sinh viên lớp 08T1, DCT

  8. <parent> Đây là Trần Văn Phương <child> sinh viên lớp 08T1, DCT </child> </parent> --------------------------------------------------------------------------------- <xsl:template match= “parent”> <xsl:value-of select=“text(parent)”/> </xsl:template> --------------------------------------------------------------------------------- Kết quả trả về : Đây là Trần Văn Phương Không lấy giá trị của node child

  9. 3.2 Các hàm định vị - position(node name) : trả về vị trí của node trong cây XML Ví dụ 1: In ra danh sách GVCN có ghi thứ tự (slide 6, PCT.XML) ….. <xsl:for-each select="//Lop"> <tr> <td><xsl:value-of select=“position()" /></td> <td><xsl:value-of select="@ten" /></td> <td><xsl:value-of select="."/> </td> </tr> </xsl:for-each> ….. Trong ví dụ trên tham số của hàm position được lấy theo ngữ cảnh, tức là node Lop. -last(node name)trả về số thứ tự node cuối cùng trong cây XML

  10. Ví dụ <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="boolean.xsl"?> <emailList> <person type=“solder" id="p001"> <name>Văn Phương</name> <email>vp@hotmail.com</email> </person> <person type=“student" id="p002"> <name>Thuận Thành</name> <email>tt@hotmail.com</email> </person> <!- - other person - - > </emailList> In ra danh sách email theo một dòng, ngăn cách bằng dấu câu phù hợp (giữa hai mail ngăn bằng dấu phảy, cuối câu là dấu chấm)

  11. <?xml version="1.0" encoding="UTf-8" ?> • <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> • <xsl:template match="/"> • <xsl:for-each select="emailList/person"> • <xsl:if test="position()!=last()"> • <xsl:apply-templates select="name" />, • </xsl:if> • <xsl:if test="position()=last()"> • <xsl:apply-templates select="name" />. • </xsl:if> • </xsl:for-each> • </xsl:template> • <xsl:template match="name"> • <xsl:value-of select="." /> • </xsl:template> • </xsl:stylesheet>

  12. 3.3. Các hàm về chuổi string-length(str) :trả về chiều dài của chuổi str, kể cả khoảng trắng. concat(str1,str2) : nối hai chuổi contains(str,substr) : trả về giá trị logic khi substr có nằm trong str không substring(str,start,end) : trả về chuổi con được trích từ str từ vị trí start đến [end] normalize-space(str) : trả về chuổi đã chuẩn hóa (giữa hai từ chỉ có một khoảng trắng) ------------------------------------------------------------------------------------- Xem các ví dụ trang sau

  13. Ví dụ Tệp Data.xml <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="normalize.xsl"?> <P> <text>Normalized text</text> <text>Mình ơi! Ta gọi là nhà</text> <text> Nhà ơi, ta gọi mình là nhà tôi </text> </P> Chú ý: hai phần tử text cuối cùng chứa innerText có nhiều khoảng trắng. Chuẩn hóa, đếm số từ ở mỗi phần tử

  14. Tệp normalize.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="/"> • <TABLE> • <xsl:for-each select="//text"> • <TR> • <TD colspan="4"><xsl:value-of select="normalize-space(.)"/></TD> • </TR> • <TR> • <TD> Starting length: </TD> • <TD> <xsl:value-of select="string-length(.)"/> </TD> • <TD> Normalized length: </TD> • <TD><xsl:value-of select="string-length(normalize-space(.))"/></TD> • </TR> • </xsl:for-each> • </TABLE> • </xsl:template> • </xsl:stylesheet>

  15. 3.4. Các hàm về số count(node name): trả về số lượng node sum(node name): trả về tổng các giá trị chứa trong các node <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="sum.xsl"?> <math> <operand>12</operand> <operand>23</operand> </math> ------------------------------------------------------------------------------------------------------------------ sum.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="math"> <xsl:value-of select="sum(operand)" /> </xsl:template> </xsl:stylesheet>

  16. 4- Sử dụng biến trong XSLT • <?xml version="1.0" encoding="utf-8"?> • <?xml-stylesheet type="text/xsl" href=“book.xsl"?> • <!-- số trang trong mỗi chương của giáo trình XML & Ứng dụng --> • <book name=“Giáo trình XML & Ứng dụng”> • <chapter title = "Mở đầu"> 20 </chapter> • <chapter title= "XML cơ bản"> 30 </chapter> • <chapter title= "Mô hình DOM">40 </chapter> • <chapter title= "XSLT"> 50 </chapter> • </book> • ------------------------------------------------------------------------------------------------------------------------------------------------- • Yêu cầu: in ra danh sách các chương theo mẫu: • Chương 1, Mở đầu , có 20 trang • …. • Tổng số : 140 trang

  17. <?xml version="1.0" encoding="utf-8"?> • <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> • <xsl:variable name="totalPages" select="sum(//chapter)"/> • <xsl:template match="/"> • <TABLE> • <xsl:for-each select="//chapter"> • <TR> • <TD> • <xsl:text>Chương: </xsl:text> • <xsl:value-of select="position()"/> , • <xsl:value-of select="@title" />, • <xsl:value-of select="."/> trang • </TD> • </TR> • </xsl:for-each> • </TABLE> • Tổng số: <xsl:value-of select="$totalPages"/> trang. • </xsl:template> • </xsl:stylesheet> Khai báo và gán trị cho biến

  18. 5- Sử dụng XSLT trong VB.NET Tạo ra tệp HTML từ tệp dữ liệu XML và tệp XSLT Imports System.Xml Imports System.Xml.Xsl Sub CreateHTML(fxml, fxsl, fhtml) Dim xslt as new XslTransform() xslt.Load(fxsl) xslt.Transform(fxml, fhtml) End Sub --------------------------------------------------------------------------- Tên tệp XML, XSL và HTML lần lượt : fxml, fxsl, fhtml Tùy theo OutPut của tệp XSLT mà đầu ra là tệp HTML, XML hay TXT. DOTNET có nhiều cách tạo ra tệp HTML

  19. Sinh viên download bài giảng, copy & paste để thử nghiệm lại các ví dụ. Tuần sau: nêu câu hỏi, làm bài tại lớp Thank you! http://ndtfit.brinkster.net

More Related