Πανεπιστήμιο Πειραιώς Τμήμα Ψηφιακών Συστημάτων Σημασιολογικός Ιστός (Semantic Web) - XML 22/11/2016 Δρ. Ανδριάνα Πρέντζα Αναπληρώτρια Καθηγήτρια aprentza@unipi.gr
Πανεπιστήμιο Πειραιά Τμήμα Ψηφιακών Συστημάτων XSL extensible Stylesheet Language
XSL - EΧtensible Stylesheet Language Δεν είναι μία απλή style sheet γλώσσα Ένα σύνολο γλωσσών XSLT- μετατροπή/μετασχηματισμός XML αρχείων (περιγράφει τη μετατροπή ενός XML αρχείου που αναπαρίσταται σε μορφή δέντρου σε άλλη μορφή) XPath εντοπισμός τμημάτων του source tree εγγράφου που ταιριάζουν με πρότυπα templates που ορίζονται στο XSL stylesheet XQuery υποβολή ερωτημάτων σε XML δεδομένα
XSLT Σημαντικότερο κομμάτι του XSL standard Μετατρέπει XML αρχεία σε άλλα XML αρχεία αρχεία αναγνωρίσιμα από τους browsers, πχ HTML, XHTML Αντιστοιχώντας σε ένα XML element ένα (X)HTML element Προσθέτει ή αφαιρεί καινούρια elements στο παραγόμενο αρχείο Ταξινομεί ή αναδιατάσει elements, κάνει δοκιμές, παίρνει αποφάσεις για το ποια στοιχεία να εμφανίσει Ορίζει τον τρόπο αναπαράστασης των δεδομένων Το XSLT μετατρέπει ένα XML source tree σε ένα XML result tree ή ένα πηγαίο έγγραφο (source document) της XML σε ένα έγγραφο αποτελέσματος (result document) της XML
Η διαδικασία XSLT
XML και Δέντρο Αποτελεσμάτων Ένα XSL stylesheet μετατρέπει τα δέντρα των αρχείων εισόδου σε μία δομή που ονομάζεται result tree και η οποία αποτελείται από τα result objects
Επισκόπηση διαδικασίας μετασχηματισμού Πέρασμα ενός εγγράφου (source document) σε έναν XSLT επεξεργαστή Ο επεξεργαστής περιέχει ένα φορτωμένο XSLT style sheet Ο επεξεργαστής στη συνέχεια: Φορτώνει τα καθορισμένα Stylesheet templates... Διασχίζει το έγγραφο, κόμβο, κόμβο... Όπου κάποιος κόμβος ταιριάζει με ένα template... Εφαρμόζει το template στον κόμβο Εξάγει ως αποτέλεσμα το (νέο) XML ή HTML έγγραφο
Διαδικασία «μεταστοιχείωσης» ( Transmutation )
XML Path - XPath Ένας τρόπος για να: ορίζουμε τμήματα ενός xml αρχείου πλοηγούμαστε σε xml αρχεία στοχεύουμε στα περιεχόμενα συγκεκριμένου στοιχείου σε XML έγγραφο ή στην τιμή συγκεκριμένου χαρακτηριστικού Χρησιμοποιεί εκφράσεις για να επιλέξει κόμβους ή σύνολα κόμβων ενός xml αρχείου. Περιέχει ένα σύνολο συναρτήσεων για αλφαριθμητικές και αριθμητικές εκφράσεις, εκφράσεις σύγκρισης ημερομηνίας και χρόνου, διαχείρισης κόμβων, τιμές Boolean. Σε αυτό το πρότυπο βασίζονται τα πρότυπα XQuery και XPointer. Αποτελεί πρότυπο της W3C από το Νοέμβριο, 1999.
Nodes XML document Δενδρική δομή με κόμβους Κάθε κόμβος αναπαριστά μέρος του of XML document Επτά τύποι Root ο κόμβος ρίζας ενός XML εγγράφου είναι ο κόμβος που προηγείται των πάντων στο έγγραφο Element Attribute Text Comment Processing instruction Namespace Οι ιδιότητες και οι χώροι ονομάτων δεν είναι παιδιά του parent node Περιγράφουν τον parent node
XPath Terminology Nodes XML documents are treated as trees of nodes. The topmost element of the tree is called the root element. Look at the following XML document: <?xml version="1.0" encoding="iso-8859-1"?> <bookstore> <book> <title lang="en">harry Potter</title> <author>j K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore> Example of nodes in the XML document above: <bookstore> (root element node) <author>j K. Rowling</author> (element node) lang="en" (attribute node)
XPath Terminology Atomic values Atomic values are nodes with no children or parent. Example of atomic values: J K. Rowling "en" Items Items are atomic values or nodes.
Relationship of Nodes Parent Each element and attribute has one parent. In the following example; the book element is the parent of the title, author, year, and price: <book> <title>harry Potter</title> <author>j K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
Relationship of Nodes Children Element nodes may have zero, one or more children. In the following example; the title, author, year, and price elements are all children of the book element: <book> <title>harry Potter</title> <author>j K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
Relationship of Nodes Siblings Nodes that have the same parent. In the following example; the title, author, year, and price elements are all siblings: <book> <title>harry Potter</title> <author>j K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
Relationship of Nodes Ancestors A node's parent, parent's parent, etc. In the following example; the ancestors of the title element are the book element and the bookstore element: <bookstore> <book> <title>harry Potter</title> <author>j K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
Relationship of Nodes Descendants A node's children, children's children, etc. In the following example; descendants of the bookstore element are the book, title, author, year, and price elements: <bookstore> <book> <title>harry Potter</title> <author>j K. Rowling</author> <year>2005</year> <price>29.99</price> </book> </bookstore>
XPath Expressions /library /library/book /library/book/@name /library/book [@name="the Fourth Civilization"] /library/book[1] //book[2] <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Expressions /library /library/book /library/book/@name /library/book [@name="the Fourth Civilization"] /library/book[1] //book[2] <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Expressions /library /library/book /library/book/@name /library/book [@name="the Fourth Civilization"] /library/book[1] //book[2] <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Expressions /library /library/book /library/book/@name /library/book [@name="the Fourth Civilization"] /library/book[1] //book[2] <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Expressions /library /library/book /library/book/@name /library/book [@name="the Fourth Civilization"] /library/book[1] //book[2] <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Expressions /library /library/book /library/book/@name /library/book [@name="the Fourth Civilization"] /library/book[1] //book[2] <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Node Selectors /library/* /library/book[1]/text() /library/node()[1]. <library> <!-- comment --> <book>the Principles of Computer Hardware</book> <book name="the Fourth Civilization /> </library>
XPath Node Selectors /library/* /library/book[1]/text() /library/node()[1]. <library> <!-- comment --> <book>the Principles of Computer Hardware</book> <book name="the Fourth Civilization /> </library>
XPath Node Selectors /library/* /library/book[1]/text() /library/node()[1]. <library> <!-- comment --> <book>the Principles of Computer Hardware</book> <book name="the Fourth Civilization /> </library>
XPath Node Selectors /library/* /library/book[1]/text() /library/node()[1]. <library> <!-- comment --> <book>the Principles of Computer Hardware</book> <book name="the Fourth Civilization /> </library>
XPath Node Selectors
XPath Axes /library/child::book (or /library/book for short) /descendent-or-self::book (or //book for short) //book[1]/parent::* (or //book[1]/.. for short) //book[2]/precedingsibling::book //book[1]/attribute::name (or //book[1]/@name for short) <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Axes /library/child::book (or /library/book for short) /descendent-or-self::book (or //book for short) //book[1]/parent::* (or //book[1]/.. for short) //book[2]/precedingsibling::book //book[1]/attribute::name (or //book[1]/@name for short) <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Axes /library/child::book (or /library/book for short) /descendent-or-self::book (or //book for short) //book[1]/parent::* (or //book[1]/.. for short) //book[2]/precedingsibling::book //book[1]/attribute::name (or //book[1]/@name for short) <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Axes /library/child::book (or /library/book for short) /descendent-or-self::book (or //book for short) //book[1]/parent::* (or //book[1]/.. for short) //book[2]/precedingsibling::book //book[1]/attribute::name (or //book[1]/@name for short) <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Axes /library/child::book (or /library/book for short) /descendent-or-self::book (or //book for short) //book[1]/parent::* (or //book[1]/.. for short) //book[2]/precedingsibling::book //book[1]/attribute::name (or //book[1]/@name for short) <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization" /> </library>
XPath Axes
XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains( @name, "C++"))] <library> <book name="c++ How to Program /> <book name="the Fourth Civilization /> </library>
XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains( @name, "C++"))] <library> <book name="c++ How to Program /> <book name="the Fourth Civilization /> </library> Αποτέλεσμα: 2
XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains( @name, "C++"))] <library> <book name="c++ How to Program /> <book name="the Fourth Civilization /> </library> Αποτέλεσμα: library
XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains( @name, "C++"))] <library> <book name="c++ How to Program /> <book name="the Fourth Civilization /> </library>
XPath Functions //book[last()] count(//book) name(/*) //book[contains(@name, "C++")] //book[not(contains( @name, "C++"))] <library> <book name="c++ How to Program /> <book name="the Fourth Civilization /> </library>
XPath Operators //book[last() or contains(@name, "C++")] //book[stringlength(@name) > 5] //book[1] //book[2] <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization /> </library>
XPath Operators //book[last() or contains(@name, "C++")] //book[stringlength(@name) > 5] //book[1] //book[2] <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization /> </library>
XPath Operators //book[last() or contains(@name, "C++")] //book[stringlength(@name) > 5] //book[1] //book[2] <library> <book name="c++ How to Program" /> <book name="the Fourth Civilization /> </library>
XPath Operators
XSL Η XSL είναι μια γλώσσα που «λέει» σε έναν browser πώς να επεξεργαστεί ένα XML αρχείο. Συγκεκριμένα, η XSL επιτρέπει την επιλογή των δεδομένων της XML που θέλουμε να εμφανισθούν, την παρουσίασή τους με οποιαδήποτε σειρά ή διάταξη, την τροποποίησή τους και την προσθήκη πληροφοριών. Συνδέεται με ένα XML έγγραφο και δίνει εντολές στον browser πώς να εμφανίσει τα δεδομένα XML. Κομμάτι της XML αποτελεί η XSLT (XSL Transform) που χρησιμοποιείται για να μετατρέψει ένα XML έγγραφο σε ένα άλλο XML έγγραφο που μπορεί να αναγνωριστεί από έναν browser, όπως ένα HTML ή ένα (X)HTML έγγραφο.
XSL Η XSLT χρησιμοποιεί την XPath για να βρει πληροφορία σε ένα XML έγγραφο. Η XPath χρησιμοποιείται για την πλοήγηση στα elements και τα attributes των XML εγγράφων. Το XSL Style Sheet αποτελείται από ένα ή περισσότερα σύνολα κανόνων που ονομάζονται templates.
XSL Η πρώτη γραμμή του XSL αρχείου δείχνει ότι το XSL είναι γραμμένο στην XML. <?xml version= 1.0?> Η γραμμή: <xsl:stylesheet xmlns:xsl= http://www.w3.org/tr/wdxsl > «λέει» στον browser ότι αυτό το αρχείο είναι XSL stylesheet. Εναλλακτικά, μπορούμε να χρησιμοποιήσουμε μια από τις παρακάτω συνώνυμες εντολές: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">
XSL Τα XSL tags ορίζουν τους κανόνες που πρέπει να εφαρμοστούν στα elements ή στα attributes της XML. Οποιοδήποτε άλλο tag, συμπεριλαμβανομένων των HTML tags, ή κομμάτια κειμένου θα διατηρηθούν όπως είναι. Συνδέουμε ένα XML αρχείο με το XSL αρχείο με την εντολή: <?xml-stylesheet type="text/xsl" href="class.xsl"?>
XSL Paths Χρησιμοποιώντας XSL paths μπορούμε να εμφανίσουμε το περιεχόμενο των elements ή τις τιμές των attributes. / : αντιπροσωπεύει το root element. Όταν το / χρησιμοποιείται σαν separator ανάμεσα σε δύο elements δείχνει ότι πηγαίνουμε ένα επίπεδο πιο κάτω στην ιεραρχία. Παραδείγματα: / class/* class/title class/students/student/name/first
XSL Paths Επιπλέον, ο browser μπορεί να χρησιμοποιήσει και να εμφανίζει τιμές των attributes. Η σύνταξη είναι @attributename Παράδειγμα: class/students/student/major/@decided
XSL Stylesheets Ένα XSL stylesheet αποτελείται από ένα σύνολο προτύπων (templates) Κάθε πρότυπο αντιστοιχεί ορισμένα σύνολα στοιχείων - elements του δέντρου πηγής και μετά περιγράφει τη σημασία της αντιστοίχισης στο result tree Γενικά, τα elements του stylesheet στο "xsl" namespace είναι μέρος της XSLT Δημιουργούμε ένα πρότυπο XSLT το οποίο αντιστοιχούμε σε ένα στοιχείο στο έγγραφό μας. «Ότι βάζουμε στο πρότυπο, αντικαθιστά τον κόμβο στον οποίο ταιριάζει» Χρήση patterns για τον εντοπισμό των κόμβων στο έγγραφο πηγής
Δομή ενός Stylesheet Τα XSL Stylesheets είναι αρχεία XML Namespaces (http://www.w3.org/tr/rec-xml-names) χρησιμοποιούνται για τον προσδιορισμό σημασιολογιών σημαντικών στοιχείων (semantically significant elements) Τα περισσότερα stylesheets είναι stand-alone αρχεία <xsl:stylesheet> ή <xsl:transform>
Δήλωση ενός XSL αρχείου Root element <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> Για να έχουμε πρόσβαση στα XSLT στοιχεία, ιδιότητες και χαρακτηριστικά πρέπει να δηλώσουμε τον XSLT χώρο ονομάτων στην αρχή του εγγράφου. Το xmlns:xsl="http://www.w3.org/1999/xsl/transform" δείχνει στον επίσημο χώρο ονομάτων W3C XSLT
XSL Template Κάθε template περιέχει κανόνες που πρόκειται να εφαρμοστούν όταν ένα element ταιριάζει με το path που παίρνει σαν τιμή το attribute match. Επομένως, το παρακάτω template, σημαίνει ότι οι κανόνες που βρίσκονται ανάμεσα στο tag αρχής και τέλους πρέπει να εφαρμοστούν στο root element. <xsl:template match= / > </xsl:template>
XSL value-of Το element <xsl:value-of select=. > χρησιμοποιείται για να εξάγουμε την τιμή ενός element ή attribute. Παραδείγματα: <xsl:value-of select= class/title > <xsl:value-of select= class/students/student/name/first > <xsl:value-of select= class/students/student/major/@decided >
Ένα απλό XSL αρχείο <mymessage> <?xml version = "1.0"?> <!-- Simple XSLT document for intro.xml --> <xsl:stylesheet version = "1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match = "mymessage"> <html> <body><xsl:value-of select = "message"/></body> </html> </xsl:template> </xsl:stylesheet> <?xml version = "1.0"?> <!-- Simple introduction to XML markup --> <message>welcome to XSLT!</message> </mymessage>
Μετατροπή XML αρχείου σε HTML Δήλωση μέσα στο XML αρχείο για τη σύνδεση με το XSL αρχείο <?xml-stylesheet type = "text/xsl" href = "intro.xsl"?> intro.xsl intro.xml
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.1 : intro.xsl --> 4 <!-- Simple XSLT document for intro.xml --> 5 6 <xsl:stylesheet version = "1.0" 7 xmlns:xsl = "http://www.w3.org/1999/xsl/transform"> 8 9 <xsl:template match = "mymessage"> 10 <html> 11 <body><xsl:value-of select = "message"/></body> 12 </html> 13 </xsl:template> 14 15 </xsl:stylesheet> Root element stylesheet XSLT namespace Use XPath expression in template element to match any mymessage element nodes template element contents are placing in resulting tree when mymessage element node is matched 2002 Prentice Hall, Inc. All rights reserved.
1 <?xml version = "1.0"?> 2 3 <!-- Fig. 12.2 : intro.xml --> 4 <!-- Simple introduction to XML markup --> 5 6 <?xml:stylesheet type = "text/xsl" href = "intro.xsl"?> 7 8 <mymessage> 9 <message>welcome to XSLT!</message> 10 </mymessage> Element stylesheet attaches style sheet to XML document 2002 Prentice Hall, Inc. All rights reserved.
1 <html><body>welcome to XSLT!</body></html> 2002 Prentice Hall, Inc. All rights reserved.
Άσκηση Δίνεται το XML αρχείο note.xml: <?xml version="1.0"?> <note> <from>john</from> <to>merry</to> <message title="reminder" >Do nοt forget me</message> </note> There was a message from John to Merry with title Reminder.
Άσκηση Ζητείται να γράψετε ένα XSL stylesheet για το note, το οποίο να εμφανίζει το παρακάτω μήνυμα: There was a message from John to Merry with title Reminder. Διευκρινήσεις: Το όνομα John είναι γραμμένο με bold και το όνομα Merry με italic. Αρχείο stylesheet note.xsl Πρέπει να προσθέσουμε στο note.xml τη γραμμή που καθοδηγεί τον browser στο note.xsl. <?xml-stylesheet type = "text/xsl" href="note.xsl"?>
Άσκηση Λύση note.xsl <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <HTML> <HEAD><TITLE> Note Stylesheet</TITLE></HEAD> <BODY> There was a message from <B> <xsl:value-of select="note/from"/> </B> to <I> <xsl:value-of select="note/to"/> </I> with title <xsl:value-of select="note/message/@title"/>. </BODY> </HTML> </xsl:template> </xsl:stylesheet>
Άσκηση Χρησιμοποιήστε το note.xml της προηγούμενης άσκησης: <?xml version="1.0"?> <note> <from>john</from> <to>merry</to> <message title="reminder" >Do nοt forget me</message> </note> Ζητείται να γράψετε ένα XSL stylesheet (note2.xsl), το οποίο να εμφανίζει το note.xml στην παρακάτω μορφή (HTML table): Διευκρινήσεις: 1. Οι τίτλοι στον πίνακα είναι bold. 2. Ο πίνακας έχει border=5 και cellpadding=5. 3. Ονομάστε το νέο αρχείο note2.xsl.
Άσκηση Λύση note2.xsl <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="/"> <HTML> <HEAD><TITLE> Note Stylesheet </TITLE></HEAD> <BODY> <TABLE BORDER="5" CELLPADDING="5"> <STRONG> <TR> <TH>From</TH> <TH>To</TH> <TH>Message Title</TH> </TR> </STRONG> <TR> <TD><xsl:value-of select="note/from"/> </TD> <TD> <xsl:value-of select="note/to"/> </TD> <TD> <xsl:value-of select="note/message/@title"/> </TD> </TR> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
XSL Templates <?xml version= 1.0?> <xsl:stylesheet version= 1.0 xmlns:xsl= http://www.w3.org/1999/xsl/transform > <xsl:template match= / > <html> <xsl:apply templates/> </html> </xsl:template> <xsl:template match= Ingredients > Ingredients List </xsl:template> </xsl:stylesheet> Επαναλαμβανόμενη επεξεργασία για όλους τους κόμβους Ο XSL processor παράγει μία λίστα για κάθε εμφάνιση του tag Ingredients στο αρχείο εισόδου
XSL Templates <xsl:template/> Χρησιμοποιείται για την επιλογή ενός κόμβου ή ενός υπό δέντρου ενός κόμβου Χρήση του match attribute για την επιλογή ενός συγκεκριμένου κόμβου <xsl:template match =... > Εφαρμογή αλλαγών <xsl:apply-templates /> Χρησιμοποιείται για την αναδρομική επεξεργασία των παιδιών ενός επιλεγμένου κόμβου <xsl:apply-templates select =... /> Χρησιμοποιείται για την επιλογή όλων των κόμβων με μια συγκεκριμένη τιμή
XSL Templates Ουσιαστικά Το στοιχείο <xsl:apply templates> λέει: «Σταμάτα εδώ και ψάξε για άλλα πρότυπα που ίσως ταιριάζουν σε αυτό το στάδιο» Κατά την αναζήτηση για στοιχεία απογόνους, το XSLT προχωρά μόνο ένα βήμα προς τα κάτω κάθε φορά, συνεπώς πάμε στο στοιχείο ingredients και δημιουργούμε ένα πρότυπο για να ταιριάζει στα στοιχεία ingredients Αυτό το νέο πρότυπο θα εφαρμοστεί όταν το XSLT συναντήσει το στοιχείο <xsl:apply templates> Σημείωση σε κάθε πρότυπο, εργαζόμαστε αυτομάτως με τα στοιχεία απογόνους του στοιχείου με το οποίο ταιριάζει το πρότυπο.
Πηγές / Acknowledgements Βιβλιογραφία http://www.w3schools.com/xml/ http://www.w3.org/xml/ http://www.xml.com/ http://www.xml.org/