33
“หหหหหหหหหหหหหหห” (Group 1) Selected Topics in Computer Engineering II XSLT, Xpat Xqueries Try to use

To try use XSL for display group XML file movies

Embed Size (px)

Citation preview

“หนึ่งหน่วยกลุ่ม ” (Group 1)Selected Topics in Computer Engineering II

XSLT, XpathXqueries

Try to use

XSLT

XSLT = XSL Transformations

XSLT is a language for transforming XML documents into XHTML documents or to other XML documents.

XSLT<xsl:template>

XML File XSL File (Movie.xsl)

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="Movie.xsl"?><film> <movie><name>Always</name><types><type>Drama</type><type>Romance</type></types><stars> <name_actor>So Ji-seob</name_actor><name_actor>Han Hyo-joo</name_actor></stars><director>Song Il-Gon</director><date><day>2</day><month>Feb</month><year>2012</year></date></movie> . . .</film>

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body>   <h2>Movie</h2>   <table border="1">     <tr bgcolor="#0055EE">       <th>type</th>       <th>star</th>     </tr>   </table> </body> </html></xsl:template></xsl:stylesheet>

XSLTOutput

XSLT<xsl:value-of>

XSL File (Movie.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="/"> <html> <body>   <h2>Movie</h2>   <table border="1">     <tr bgcolor="#0055EE">       <th>types</th>       <th>stars</th>     </tr>     <tr>       <td><xsl:value-of select="film/movie/types"/></td>       <td><xsl:value-of select="film/movie/stars"/></td>     </tr>   </table> </body> </html></xsl:template></xsl:stylesheet>

XSLTOutput

XSLT<xsl:for-each>

XSL File (Movie.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="/"> <html> <body>   <h2>Movie</h2>   <table border="1">     <tr bgcolor="#0055EE">       <th>types</th>       <th>stars</th>     </tr>     <xsl:for-each select="film/movie">     <tr>       <td><xsl:value-of select="types"/></td>       <td><xsl:value-of select="stars"/></td>     </tr>     </xsl:for-each>   </table> </body> </html></xsl:template></xsl:stylesheet>

XSLTOutput

XSLT<xsl:sort>

XSL File (Movie.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="/"> <html> <body>   <h2>Movie</h2>   <table border="1">     <tr bgcolor="#0055EE">       <th>types</th>       <th>stars</th>     </tr>     <xsl:for-each select="film/movie">     <xsl:sort select="types"/>     <tr>       <td><xsl:value-of select="types"/></td>       <td><xsl:value-of select="stars"/></td>     </tr>     </xsl:for-each>   </table> </body> </html></xsl:template></xsl:stylesheet>

XSLTOutput

XSLT<xsl:if>

XSL File (Movie.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="/"> <html> <body> <h2>Movie</h2> <table border="1"> <tr bgcolor="#0055EE"> <th>types</th> </tr> <xsl:for-each select="film/movie"> <xsl:if test="date/year > 2016 "> <tr> <td><xsl:value-of select="name"/></td> </tr> </xsl:for-each> </table> </body> </html></xsl:template></xsl:stylesheet>

XSLTOutput

XSLT<xsl:choose>

XSL File (Movie.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="/"> <html> <body> <h2>Movie</h2> <table border="1"> <tr bgcolor="#0055EE"> <th>types</th> </tr>

<xsl:for-each select="film/movie"> <tr> <xsl:choose> <xsl:when test="date/year > 2016 "> <td bgcolor="#ff00ff"> <xsl:value-of select="name"/> </td>

</xsl:when> <xsl:otherwise> <td><xsl:value-of select="name"/></td> </xsl:otherwise> </xsl:choose> </tr> </xsl:for-each> </table> </body> </html></xsl:template></xsl:stylesheet>

XSLTOutput

XSLT<xsl:apply-templates>

XSL File (Movie.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="/"> <html> <body> <h2>Movie</h2>   <xsl:apply-templates/>   </body> </html></xsl:template>

<xsl:template match="movie"> <p>   <xsl:apply-templates select="name"/>     <xsl:apply-templates select="types"/> </p></xsl:template>

<xsl:template match="name"> Name: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /></xsl:template>

<xsl:template match="types"> Types: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /></xsl:template>

</xsl:stylesheet>

XSLTOutput

XpathWhat is XPath?

• XPath เป็น syntax ในการกำาหนดสว่นต่างๆของ XML• XPath ใชร้ะบบ path ในการนำาทางและเขา้ถึงในไฟล์ XML • XPath ประกอบไปด้วย library ของฟงัก์ชัน่พื้นฐาน• XPath เป็น element หลัก (major element) ของ XSLT• XPath เป็นท่ีแนะนำาของ W3C

XpathXPath Terminology

Nodes ใน XPath จะมี node อยู่ 7 ชนิดคือ : element, attribute, text,

namespace, processing-instruction, comment, และ document nodes.

ตัวอยา่ง ไฟล์ XML<?xml version="1.0" encoding="UTF-8"?>

<bookstore> <book> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book></bookstore>

XpathNode ในไฟล์ xml

<bookstore> (root element node)<author>J K. Rowling</author> (element node)lang="en" (attribute node)

XpathXPath Syntax

Selecting Nodes

Expression Descriptionnodename เลือกทกุ node ท่ีชื่อ "nodename"/ เลือกจาก root node// เลือก node ในไฟล์ XML เริม่จาก node ปัจจุบนัที่ตรง

กับการคัดเลือกโดยไมส่นวา่ node ท่ีเลือกมาจากไหน

. เลือก node ปัจจุบนั

.. เลือก parent ของ node ปัจจุบนั

@ เลือก attribute

Xpathตัวอยา่งการใชง้านและผลลัพธ์

Path Expression Result

bookstore เลือกทกุ node ท่ีชื่อ "bookstore"/bookstore เลือก root element bookstorebookstore/book เลือก book elements ทัง้หมดท่ีเป็น children ของ

bookstore//book เลือก book elements ทัง้หมดโดยไมส่นวา่ elements มาจาก

ไหนbookstore//book เลือก book elements ทัง้หมดท่ีเป็น descendant ของ

element bookstore//@lang เลือก attributes ทัง้หมดท่ีชื่อ lang

XpathPredicates

Predicates ใชห้า node ท่ีมคีณุสมบติัจำาเพาะ หรอืมคีณุสมบติัต่างๆกันโดยPredicates จะต้องเรยีกใชภ้ายใน [ ] เท่านัน้

Path Expression Result/bookstore/book[1] เลือก element book ตัวแรกท่ีเป็น child ของ element

bookstore /bookstore/book[last()] เลือก element book ตัวสดุท้ายท่ีเป็น child ของ element

bookstore /bookstore/book[last()-1] เลือก element book ตัวก่อนสดุท้ายท่ีเป็น child ของ element

bookstore /bookstore/book[position()<3] เลือก element book สองตัวแรกท่ีเป็น child ของ element

bookstore //title[@lang] เลือก element title   ทกุตัวที่มี attribute ชื่อ lang

//title[@lang='en'] เลือก element title   ทกุตัวที่มี attribute ชื่อ lang และ lang = en

/bookstore/book[price>35.00] เลือก element book ทกุตัวท่ีเป็น child ของ element bookstore และมคี่าของ element price มากกวา่ 35

/bookstore/book[price>35.00]/title เลือก element title ทกุตัวท่ีเป็น child ของ element book ท่ีมี ค่าของ element price มากกวา่ 35 และเป็น child ของ element

bookstore

XpathXPath Operators

Operator Description Example| Computes two node-sets //book | //cd

+ Addition 6 + 4- Subtraction 6 - 4* Multiplication 6 * 4div Division 8 div 4= Equal price=9.80!= Not equal price!=9.80< Less than price<9.80<= Less than or equal to price<=9.80

> Greater than price>9.80>= Greater than or equal to price>=9.80

or or price=9.80 or price=9.70

and and price>9.00 and price<9.90

mod Modulus (division remainder) 5 mod 2

Xqueries

• XQuery คือภาษาท่ีใชใ้นการ Query ขอ้มูลจากเอกสาร XML คล้ายกับ SQL• Xquery ใช ้Xpath ในการเขา้ถึงสว่นต่าง ๆ ของ xml แล้วทำาการแปลงขอ้มูลออกเป็นรูปแบบหรอืโครงสรา้งอ่ืน • ใชฟ้งัชัน่ doc() ในการเรยีก ไฟล์ xml มาเชน่• doc("MovieAll_SPN.xml")• แล้วใช ้path ในการเขา้ถึง element ต่างๆ• doc("MovieAll_SPN.xml")/film/movie

XqueriesFLWOR

• For - selects a sequence of nodes• Let - binds a sequence to a variable• Where - filters the nodes• Order by - sorts the nodes• Return - what to return (gets evaluated once for every node)

Xqueriesทดลองโค้ดโดยการใชร้ว่ม

กับ html for $x in film/movie

return <li>{data($x/name

)}</li>

Xqueries

ในการเปรยีบเทียบนัน้จะใชเ้ครื่องหมาย =, !=, <, <=, >, >= หรอื eq, ne, lt, le, gt, ge โดยจะแตกต่างกันท่ี eq, ne, lt, le, gt, ge จะใช้งานได้เมื่อมแีค่ค่าเดียวท่ีเป็นจรงิทดลองโค้ดเรยีกชื่อหนังท่ีม ีTom Hanks แสดงfor $x in film/moviewhere $x//name_actor="Tom Hanks"return $x/name

Xqueriesผลลัพธ์

Xqueries

ทดลองโค้ดเรยีกชื่อหนังท่ีชนิด Adventure เรยีงตามชื่อfor $x in film/moviewhere $x/types/type="Adventure"order by $x/namereturn $x/name

Xqueries

Reference• XSL Tutorial

http://www.w3schools.com/xsl/

Members

Group1นาย อภิวฒัธ ์วงศ์โท๊ะ รหสันักศึกษา 52-1116-530-2นางสาว พลัลภา เขมรงัสฤษฏ์ รหสันักศึกษา 56-010126-2008-1นางสาว อัญธกิา หนองบวั รหสันักศึกษา 56-010126-3028-1นาย ธรีวฒัน์ ผ่องสกลุ รหสันักศึกษา 56-010126-3015-9นาย ธนดล เตชะวชัรกีลุ รหสันักศึกษา 56-010126-3009-4นาย ภมูมฑิล ไชยเชดิเกียรติ รหสันักศึกษา 56-010116-2131-8