Presentation is loading. Please wait.

Presentation is loading. Please wait.

Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 1 XML Transformation: XSLT Semantic Web - Fall 2005 Computer.

Similar presentations


Presentation on theme: "Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 1 XML Transformation: XSLT Semantic Web - Fall 2005 Computer."— Presentation transcript:

1 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 1 XML Transformation: XSLT Semantic Web - Fall 2005 Computer Engineering Department Sharif University of Technology

2 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 2 Semantic web - Computer Engineering Dept. - Fall 2005 2 Outline Fundamentals of XSLT XPath Cocoon

3 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 3 Semantic web - Computer Engineering Dept. - Fall 2005 3 XSLT XSLT stands for Extensible Stylesheet Language Transformations It is used to transform XML documents into other kinds of documents, e.g. HTML, PDF, XML, … XSLT uses two input files: –The XML document containing the actual data –The XSL document containing both the “framework” in which to insert the data, and XSLT commands to do so

4 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 4 Semantic web - Computer Engineering Dept. - Fall 2005 4 XSLT Architecture Source XML doc XSL stylesheet XSL processor Target Document

5 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 5 Some special transforms XML to HTML— for old browsers XML to LaTeX—for TeX layout XML to SVG—graphs, charts, trees XML to tab-delimited—for db/stat packages XML to plain-text—occasionally useful XML to XSL-FO formatting objects

6 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 6 Semantic web - Computer Engineering Dept. - Fall 2005 6 XSLT Data Model XSLT reads an XML documents as a source tree Transforms the documents into a result tree Transformations are specified in a stylesheet To navigate the tree XSLT uses XPath

7 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 7 Semantic web - Computer Engineering Dept. - Fall 2005 7 Introduction to XPath XPath is a syntax for addressing parts of an XML document by –describing paths through the document hierarchy –specifying constraints to match against the document's structure XSL uses XPath expressions to –determine which elements match a template –select nodes upon which to perform operations

8 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 8 Semantic web - Computer Engineering Dept. - Fall 2005 8 XPath Basics XPath expressions superficially resemble UNIX pathnames, e.g. poem/stanza/line refers to "all line elements which are children of stanza elements which are children of poem elements" XPath expressions are evaluated relative to a "context node", which is analogous to the "current working directory" in UNIX or DOS. The XPath expression for this is "."

9 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 9 Semantic web - Computer Engineering Dept. - Fall 2005 9 XPath Basics: a Simple Example Consider the following XML document: Roses Ima Poet Roses are red violets are blue I'm a poet and you're not!

10 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 10 Semantic web - Computer Engineering Dept. - Fall 2005 10 XPath Basics: a Simple Example (cont.) The XPath " poem/stanza/line " selects Roses Ima Poet Roses are red violets are blue I'm a poet and you're not!

11 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 11 Semantic web - Computer Engineering Dept. - Fall 2005 11 XPath Basics: wildcards The XPath " poem/stanza/* " selects Roses Ima Poet Roses are red violets are blue I'm a poet and you're not!

12 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 12 Semantic web - Computer Engineering Dept. - Fall 2005 12 XPath Basics: descendants The XPath " poem//punch " selects: Roses Ima Poet Roses are red violets are blue I'm a poet and you're not!

13 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 13 Semantic web - Computer Engineering Dept. - Fall 2005 13 XPath Basics: sequencing " poem/stanza/line[1] " selects: Roses Ima Poet Roses are red violets are blue I'm a poet and you're not!

14 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 14 Semantic web - Computer Engineering Dept. - Fall 2005 14 XPath Basics: sequencing (cont.) " poem/stanza/line[position() = last()] " selects: Roses Ima Poet Roses are red violets are blue I'm a poet and you're not!

15 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 15 Semantic web - Computer Engineering Dept. - Fall 2005 15 XPath Basics: selecting text nodes " poem/author/text() " selects: Roses Ima Poet Roses are red violets are blue I'm a poet and you're not!

16 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 16 Semantic web - Computer Engineering Dept. - Fall 2005 16 XPath Basics: conditionals " poem/stanza[punch] " selects: Roses Ima Poet Roses are red violets are blue I'm a poet and you're not!

17 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 17 Semantic web - Computer Engineering Dept. - Fall 2005 17 XPath Basics: conditionals: equality “ //line[text()="I'm a poet"] ” Roses Ima Poet Roses are red violets are blue I'm a poet and you're not!

18 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 18 Semantic web - Computer Engineering Dept. - Fall 2005 18 A simple XSL example File data.xml : Hello World! File render.xsl :

19 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 19 Semantic web - Computer Engineering Dept. - Fall 2005 19 Stylesheet (.xsl file) It is a well-formed XML document It is a collection of template rules A template rule consists of pattern and a template Pattern is specified in Xpath and locates the node of the XML tree. The located node is replaced by the template in the result tree

20 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 20 Semantic web - Computer Engineering Dept. - Fall 2005 20 The.xsl file An XSLT document has the.xsl extension The XSLT document begins with: Contains one or more templates, such as:... And ends with:

21 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 21 Semantic web - Computer Engineering Dept. - Fall 2005 21 Finding the message text The template says to select the entire file –You can think of this as selecting the root node of the XML tree Inside this template, – selects the message child –Alternative Xpath expressions that would also work:./message /message/text()./message/text()

22 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 22 Semantic web - Computer Engineering Dept. - Fall 2005 22 Putting it together The XSL was: The chooses the root The is written to the output file The contents of message is written to the output file The is written to the output file The resultant file looks like: Hello World!

23 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 23 Semantic web - Computer Engineering Dept. - Fall 2005 23 How XSLT works The XML text document is read in and stored as a tree of nodes The template is used to select the entire tree The rules within the template are applied to the matching nodes, thus changing the structure of the XML tree –If there are other templates, they must be called explicitly from the main template Unmatched parts of the XML tree are not changed After the template is applied, the tree is written out again as a text document

24 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 24 Semantic web - Computer Engineering Dept. - Fall 2005 24 Where XSLT can be used A server can use XSLT to change XML files into HTML files before sending them to the client A modern browser can use XSLT to change XML into HTML on the client side –This is what we will mostly be doing in this class Most users seldom update their browsers –If you want “everyone” to see your pages, do any XSL processing on the server side

25 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 25 Semantic web - Computer Engineering Dept. - Fall 2005 25 Modern browsers Internet Explorer 6 best supports XML Netscape 6 supports some of XML Internet Explorer 5.x supports an obsolete version of XML –If you must use IE5, the initial PI is different (you can look it up if you ever need it)

26 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 26 Semantic web - Computer Engineering Dept. - Fall 2005 26 xsl:value-of selects the contents of an element and adds it to the output stream –The select attribute is required –Notice that xsl:value-of is not a container, hence it needs to end with a slash Example (from an earlier slide):

27 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 27 Semantic web - Computer Engineering Dept. - Fall 2005 27 xsl:for-each xsl:for-each is a kind of loop statement The syntax is Text to insert and rules to apply Example: to select every book ( //book ) and make an unordered list ( ) of their titles ( title ), use:

28 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 28 Semantic web - Computer Engineering Dept. - Fall 2005 28 Filtering output You can filter (restrict) output by adding a criterion to the select attribute’s value: This will select book titles by Terry Pratchett

29 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 29 Semantic web - Computer Engineering Dept. - Fall 2005 29 Filter details Here is the filter we just used: author is a sibling of title, so from title we have to go up to its parent, book, then back down to author This filter requires a quote within a quote, so we need both single quotes and double quotes Legal filter operators are: = != < > –Numbers should be quoted, but apparently don’t have to be

30 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 30 Semantic web - Computer Engineering Dept. - Fall 2005 30 But it doesn’t work right! Here’s what we did: This will output and for every book, so we will get empty bullets for authors other than Terry Pratchett There is no obvious way to solve this with just xsl:value-of

31 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 31 Semantic web - Computer Engineering Dept. - Fall 2005 31 xsl:if xsl:if allows us to include content if a given condition (in the test attribute) is true Example: This does work correctly!

32 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 32 Semantic web - Computer Engineering Dept. - Fall 2005 32 xsl:choose The xsl:choose... xsl:when... xsl:otherwise construct is XML’s equivalent of Java’s switch... case... default statement The syntax is:... some code...... some code... xsl:choose is often used within an xsl:for-each loop

33 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 33 Semantic web - Computer Engineering Dept. - Fall 2005 33 xsl:sort You can place an xsl:sort inside an xsl:for-each The attribute of the sort tells what field to sort on Example: by –This example creates a list of titles and authors, sorted by author

34 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 34 Semantic web - Computer Engineering Dept. - Fall 2005 34 xsl:text... helps deal with two common problems: –XSL isn’t very careful with whitespace in the document This doesn’t matter much for HTML, which collapses all whitespace anyway (though the HTML source may look ugly) gives you much better control over whitespace; it acts like the element in HTML –Since XML defines only five entities, you cannot readily put other entities (such as ) in your XSL &nbsp; almost works, but is visible on the page Here’s the secret formula for entities: &nbsp;

35 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 35 Semantic web - Computer Engineering Dept. - Fall 2005 35 Creating tags from XML data Suppose the XML contains Dr. Abolhassani's Home Page http://sharif.edu/~abolhassani And you want to turn this into Dr. Abolhassani's Home Page We need additional tools to do this!

36 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 36 Semantic web - Computer Engineering Dept. - Fall 2005 36 Creating tags--solution 1 Suppose the XML contains Dr. Abolhassani's Home Page http://sharif.edu/~abolhassani adds the named attribute to the enclosing tag The value of the attribute is the content of this tag Example: Result: Dr. Abolhassani's Home Page

37 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 37 Semantic web - Computer Engineering Dept. - Fall 2005 37 Creating tags--solution 2 Suppose the XML contains Dr. Abolhassani's Home Page http://sharif.edu/~abolhassani An attribute value template (AVT) consists of braces { } inside the attribute value The content of the braces is replaced by its value Example: Result: Dr. Abolhassani's Home Page

38 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 38 Semantic web - Computer Engineering Dept. - Fall 2005 38 Modularization Modularization--breaking up a complex program into simpler parts--is an important programming tool –In programming languages modularization is often done with functions or methods –In XSL we can do something similar with xsl:apply-templates For example, suppose we have a DTD for book with parts titlePage, tableOfContents, chapter, and index –We can create separate templates for each of these parts

39 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 39 Semantic web - Computer Engineering Dept. - Fall 2005 39 Book example Table of Contents Etc.

40 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 40 Semantic web - Computer Engineering Dept. - Fall 2005 40 xsl:apply-templates The element applies a template rule to the current element or to the current element’s child nodes If we add a select attribute, it applies the template rule only to the child that matches If we have multiple elements with select attributes, the child nodes are processed in the same order as the elements

41 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 41 Semantic web - Computer Engineering Dept. - Fall 2005 41 Applying templates to children XML Gregory Brill by With this line: XML by Gregory Brill Without this line: XML

42 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 42 Semantic web - Computer Engineering Dept. - Fall 2005 42 Tools for XSL Development There are a number of free and commercial XSL tools available –XSLT processors: MSXML, which currently supports the latest XSLT specification (native Win32) Xalan from Apache (C++, Java) –Editors and browsers Internet Explorer 6.0 XML Spy (commercial)

43 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 43 Semantic web - Computer Engineering Dept. - Fall 2005 43 Cocoon Cocoon is Apache’s dynamic XML Publishing Framework. Cocoon uses XSLT. Cocoon allows separation of content, logic and presentation. making sure people can interact and collaborate on a project, without stepping on each other toes, and component-based web development. Cocoon is a web-application that runs using Apache Tomcat (Cocoon.war).

44 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 44 Semantic web - Computer Engineering Dept. - Fall 2005 44 What Cocoon can do

45 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 45 Semantic web - Computer Engineering Dept. - Fall 2005 45 Cocoon Pipeline Cocoon introduced the idea of a pipeline to handle a request. A pipeline is a series of steps for processing a particular kind of content.

46 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 46 Semantic web - Computer Engineering Dept. - Fall 2005 46 Sitemap In Cocoon, configuration information for the pipelines that an application requires is defined in a file named sitemap.

47 Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 47 Semantic web - Computer Engineering Dept. - Fall 2005 47 References Specifications: –http://www.w3.org/Style/XSLhttp://www.w3.org/Style/XSL –http://www.w3.org/TR/xslthttp://www.w3.org/TR/xslt –http://www.w3.org/TR/xpathhttp://www.w3.org/TR/xpath –http://www.w3.org/TR/xslhttp://www.w3.org/TR/xsl An excellent XSLT tutorial: –http://www.cafeconleche.org/books/bible2/chapters/ch17.htmlhttp://www.cafeconleche.org/books/bible2/chapters/ch17.html Another tutorial: –http://www.w3schools.com/xslhttp://www.w3schools.com/xsl Microsoft (MSXML3): –http://msdn.microsoft.com/xmlhttp://msdn.microsoft.com/xml Saxon: –http://saxon.sourceforge.net/http://saxon.sourceforge.net/ Xalan: –http://xml.apache.org./xalan/overview.htmlhttp://xml.apache.org./xalan/overview.html


Download ppt "Semantic web course – Computer Engineering Department – Sharif Univ. of Technology – Fall 2005 1 XML Transformation: XSLT Semantic Web - Fall 2005 Computer."

Similar presentations


Ads by Google