Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)

Similar presentations


Presentation on theme: "1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)"— Presentation transcript:

1 1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)

2 2 XHTML

3 3 XHTML XHTML stands for EXtensible HyperText Markup Language XHTML is aimed to replace HTML XHTML is almost identical to HTML 4.01 XHTML is a stricter and cleaner version of HTML XHTML is HTML defined as an XML application XHTML is a W3C Recommendation

4 4 HTML Vs. XHTML The Most Important Differences: -XHTML elements must be properly nested -XHTML documents must be well-formed -Tag names must be in lowercase -All XHTML elements must be closed

5 5 XML Namespaces

6 6 When an element name appears in two different XML documents, we would like to know that it has the same meaning in both documents -Is the tag used as the XHTML tag in both documents? -If two documents about books have the tag, does it mean that they use the same system for cataloging books?

7 7 What XML Namespaces are and What They are not Namespaces merely provide a mechanism for creating unique names (for elements and attributes) that can be used in XML documents all over the Web -A namespace is just a collection of names that were created for a specific domain of applications Namespaces are not DTDs and they do not provide a mechanism for validation of XML documents using multiple DTDs

8 8 A name space is identified by a URI The URI does not have to point to anything -It is merely used as a mechanism for creating unique names An element name or attribute name from a namespace has two parts prefix:name prefix identifies the namespace name is just a name from the namespace Identifying an XML Namespace

9 9 Recognizing Namespace When an application sees a qualified name, it may recognize it and act accordingly -A browser identifies tags that belong to the XHTML namespace and processes them -An XSLT processor identifies tags and attributes that belong to the XSLT namespace and executes them

10 10 Declaring a Namespace An XML namespace is declared in the xmlns attribute XML Namespaces John Doe Using foo as the prefix, instead of using the URI, is more convenient

11 11 The Default Namespace The default namespace is declared without a prefix XML Namespaces John Doe All the above elements belong to the default namespace

12 12 DTDs as Namespaces The URI of a namespace may point to a DTD A DTD defines a namespace comprising all its element names and attribute names -But it is just a namespace – not a DTD!

13 13 Example <bib:book xmlns:bib=“http://www.acm.org/bibliography.dtd” xmlns:isbn=“http://www.isbn-org.org/def.dtd”> Proceedings of SIGMOD 472010 1-58113-332-4 This document is invalid according to either DTD! But the document is well formed! (e.g., in the book element, attribute names are unique)

14 14 Alternatively, One Namespace can be Declared as the Default <book xmlns=“http://www.acm.org/bibliography.dtd” xmlns:isbn=“http://www.isbn-org.org/def.dtd”> Proceedings of SIGMOD 472010 1-58113-332-4 This document is well formed but invalid according to either DTD!

15 15 Scope of Namespaces The scope of a namespace declaration is the element containing the declaration and all descendant elements -Must use the prefix anywhere in the scope Only the default namespace can be redeclared More than one namespace can be declared in the same scope -At most one can be the default namespace -All others must have unique prefixes

16 16 What about Attributes? Recall that element names and attribute names must be qualified if they belong to a nondefault namespace Unqualified element names belong to the default namespace (if they are inside the scope) However, an unqualified attribute does not belong to the default namespace An unqualified attribute is processed according to the rules that apply to its element name

17 17 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)

18 18 XSL XSL is a standard that consists of three parts: XPath (navigation in documents) XSLT (transformation of documents) XSLFO (FO for formatting objects) -This is a rather complex language for typesetting (i.e., preparing text for printing) -It will not be taught

19 19 XSL Transformations (XSLT) An Example

20 20 XSLT XSLT is a language for transforming XML documents into other XML documents -For example, XHTML, WML -Can also transform XML to text documents An XSLT program is itself an XML document (called an XSL stylesheet) that describes the transformation process for input documents

21 21 XSLT Processors XSLT Processor

22 22 Web Page Layout Doc. Structure Data Web Pages – The Whole Picture XML XSLXHTML Style Knowledge CSS

23 23 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog.xml

24 24 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog This is a cd catalog! Valid XML! Commands are XML elements with the namespace xsl Includes XHTML elements catalog.xsl

25 25 Applying XSL Stylesheets to XML There are several ways of applying an XSL stylesheet to an XML document: -Directly applying an XSLT processor to the XML document and the XSL stylesheet -Calling an XSLT processor from within a program -Adding to the XML document a link to the XSL stylesheet and letting the browser do the transformation The resulting XHTML document is shown instead of the original XML

26 26 How Does XSLT Work? An XSL stylesheet is a collection of templates that are applied to source nodes (i.e., nodes of the given XML) Each template has a match attribute that specifies to which source nodes the template can be applied Each source node has a template that matches it The current source node is processed by applying a template that matches this node When processing a node, it is possible (but not necessary) to recursively process other nodes, e.g., the children of the processed node The XSLT processor processes the document root ( / )

27 27 Templates A template has the form... The content of a template consists of -XML elements (e.g., XHTML) and text that are copied to the result -XSL elements ( ) that are actually instructions The pattern syntax is a subset of XPath

28 28 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog This is a cd catalog! catalog1.xsl

29 29 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog1.xml

30 30 The Result cd catalog This is a cd catalog! Automatically added to

31 31 Examples of Match Attributes match="cd", -All elements with tag name cd match="//cd", match="/catalog/cd/artist" -All matches of the absolute XPath match="cd/artist" -All artist nodes that have a cd parent match="catalog//artist" -All artist nodes that have a catalog ancestor match="cd[@country='UK']/artist"

32 32 Processing starts by applying a template to the root -If no specified template matches the root, then one is inserted by default (see the next slide) The XSL stylesheet must specify explicitly whether templates should be applied to descendants of a node It is done by putting inside a template the instruction: -In xpath, the current processed node is used as the context node Without the select attribute, this instruction processes all the children of the current node (including text nodes) <xsl:apply-templates>

33 33 Default Templates XSL provides implicit built-in templates that match every element and text nodes Templates we write always override these built-in templates (when they match)

34 34 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 …

35 35 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> A cd! A cd!

36 36 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> A cd! A cd! Aretha: Lady Soul Aretha Franklin 9.90

37 37 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:apply-templates select="catalog/cd[@country='UK']/artist"/> An artist! An artist!

38 38 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:apply-templates select="cd[@country='UK']/artist"/> An artist!

39 39 The Most Frequently Used Elements of XSL -This element extracts the value of a node from the node list located by xpath -This element loops over all the nodes in the node list located by xpath,, etc. -This element is for conditional processing

40 40 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog CD catalog [ ] Currently selected element is the context (current) node Example 1 catalog2.xsl

41 41 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog CD catalog Example 2 catalog3.xsl

42 42 : ( now on sale: $ ) Example 2 (cont.) Entities replace characters Price<10 → price<10 catalog3.xsl

43 43 Special price! Good price! (Normal price.) Example 3 : Switch syntax for conditions

44 44 The Element The element is used to sort the list of nodes that are looped over by the element Thus, the must appear inside the element The looping is done in sorted order

45 45 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog CD catalog CDs are iterated in ascending order of the titles catalog4.xsl

46 46 Setting Values in Attributes The element cannot be used within attribute value However, we can insert expressions into attribute values, by putting the expression inside curly braces ( {} ) Alternatively, we can use in order to construct XML elements

47 47 An Example In the following example, we add to each CD entitled t a link to the URL /showcd.php?title=t :

48 48 Using Using : showcd/?title=

49 49 On XSL Code Typically, an XSLT program can be written in several, very different ways -Templates can sometime replace loops and vice versa -Conditions can sometimes be replaced with XPath predicates (e.g., in the select attribute) and vice versa A matter of convenience and elegancy

50 50 On Recursive Templates It is not always possible to avoid recursive templates -That is, use only the template of the root Suppose that we want to write an XSL stylesheet that generates a copy of the source document -It is rather easy to do it when the structure of the source XML document is known Can we write an XSL stylesheet that does it for every possible XML document? -Yes! (see next slide)

51 51 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> Identity Transformation Stylesheet

52 52 Generating Valid XHTML By default, the documents that XSL stylesheets generate are not valid XHTML Next, we will show how XSL stylesheet can be changed in order to generate valid XHTML

53 53 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> cd catalog This is a cd catalog! The Original XSL Example

54 54 cd catalog This is a cd catalog! Uppercase tag name, unclosed element No DOCTYPE The Original Transformation Result

55 55 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="html" doctype-public= "-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system= " http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd "/> … Modifying the XSL Example

56 56 cd catalog This is a cd catalog! META is not inserted The Transformation Result

57 57 Some Other XSL Elements The element inserts free text in the output The creates a copy of the specified nodes The element creates a comment node in the result tree The element defines a variable (local or global) that can be used within the program

58 58 W3Schools Tutorial on XSLT The W3Schools XSLT Tutorial has (among other things) tables that list all the elements and functions of XSLTW3Schools XSLT Tutorial It also has some details about implementations -Some browsers may not implement all features or may implement some features differently from the specifications

59 59 The End! These slides are based on those developed for the course: http://www.cs.huji.ac.il/~dbi http://www.cs.huji.ac.il/~dbi


Download ppt "1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL)"

Similar presentations


Ads by Google