1 / 82

Extensible Stylesheet Language (XSL)

Extensible Stylesheet Language (XSL). The XSL Standard. XSL consists of 3 parts: XPath (navigation in documents) XSLT (transformation of documents) XSLFO (FO for formatting objects ) A rather complex language for typesetting (e.g., for preparing text for printing) It will not be taught.

tia
Télécharger la présentation

Extensible Stylesheet Language (XSL)

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. Extensible Stylesheet Language (XSL)

  2. The XSL Standard XSL consists of 3 parts: • XPath(navigation in documents) • XSLT(transformation of documents) • XSLFO(FO for formatting objects) • A rather complex language for typesetting (e.g., for preparing text for printing) • It will not be taught

  3. XML Path Language(XPath)

  4. XML example revisited:

  5. <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries> world.xml

  6. The root is implicit (Does not appear in the text of the XML document) The XML DOM Model

  7. <!ELEMENTcountries(country*)> <!ELEMENTcountry(name,population?,city*)> <!ATTLISTcountrycontinentCDATA#REQUIRED> <!ELEMENTname (#PCDATA)> <!ELEMENTcity (name)> <!ATTLISTcitycapital(yes|no) "no"> <!ELEMENTpopulation (#PCDATA)> <!ATTLISTpopulationyearCDATA#IMPLIED> <!ENTITYeu"Europe"> <!ENTITYas"Asia"> <!ENTITY af"Africa"> <!ENTITY am"America"> <!ENTITY au"Australia"> world.dtd

  8. The XPath Language • XPath expressions are used for addressing elements (nodes) of an XML document • Used in XSLT(next subject today)and in XQuery(a query language for XML) • The syntax resembles that of the Unix file system • But the semantics have some substantial differences /countries/country[population>10000000]

  9. /countries/country[population>10000000] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries>

  10. //country[@continent="Asia"]/city world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries>

  11. XPath Expressions • An XPath expression (or just XPath for short) matches paths in the XML tree • An absolute path begins with the root of the document • Starts with "/" (or "//") • For example,/countries/country/city, //city • A relative path begins with a context node that is defined by the application that uses the XPath • For example, city/name, or ./name

  12. Applying XPath to XML • Formally, the result of applying an XPath e to an XML document (or a context node in the document) isthe list of all nodes n in the document, such that e matches the path from the root (or the context node) to n • The order in the list is defined by the order of the nodes in the document

  13. XPath Steps and Axis • An XPath describes a sequence of steps that together characterize a path • A step is defined by an axis that specifies a tree relationship between nodes • More particularly, the axis describes how to get from the current node to the next one • For example, parent-child, child-parent, ancestor-descendant, etc. • Consecutive steps are separated by /

  14. Child Axis • A child axis has the simple form tagName • Go to an element child with the tag tagName • For example, • /tagName matches the tagName child of root • city/name • /countries/country/city • The child axis * matches every tag • For example: /*/*/city,*/name

  15. Child-Axis Examples /countries

  16. Child-Axis Examples /countries/country/city

  17. Child-Axis Examples city/name Context

  18. Child-Axis Examples /*/country/* An attribute is not an element child!

  19. Self and Descendant-or-Self • The self axis “.” denotes the identity relationship • That is, the step “remain in the current node” • /countries/country/.≡/countries/country • country/./city ≡ country/city • The descendant-or-self axis means: either stay in the current node or go to some descendant of the current node • descendant-or-self:node(), • // is a shotrcut notation for /descendant-or-self:node()/ • For example, country//name

  20. /countries//name Descendant Examples

  21. .//* Descendant Examples Context

  22. Other Axis Types • The parent axis “..” denotes the parent relationship • That is, the step “go to the parent of the current node” • For example, //name/../population • XPath has more axis types (denoted by a different syntax from the ones shown earlier): • descendant • ancestor • ancestor-or-self • following-sibling • preceding-sibling • …

  23. Referring Attributes • The attribute axis is denoted @attName • That is, “go to the attribute attName of the current node” • The operator @* matches every attribute

  24. //country/@continent Attribute Examples

  25. Attribute Examples @continent Context

  26. //@* Attribute Examples

  27. XPath Predicates • Predicates in XPath are used for filtering out steps • For example, //city[@captial="yes"] will match only capital cities • Formally, given a predicate [PExpr], the expression PExpr is transformed into a Boolean value and the step is taken only if this value is true • The node reached in the last step is the context node • XPath has a rather rich language for predicate expressions; we only demonstrate common ones

  28. //country[./population>10000000] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries> • The XPath ./population is transformed into a number by taking its embedded text • The XPath ./population is relative to the current node (i.e., country) in the path • Equivalent to //country[population>10000000]

  29. //country[.//city] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries> An XPath evaluates to true if and only if its result is not empty

  30. //country[//city] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries> Why?

  31. //country[population[.>3000000 and @year>2003]] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries>

  32. //country[name="Israel" or name="Spain"]/population world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries>

  33. //country/city[2] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries> • A number acts as an index • That is, the number n evaluates to true if n is the position of the node among all those reached in the last step (i.e., city)

  34. Functions • Inside XPath predicates, you can use a set of predefined functions • Here are some examples: • last() – returns the number of nodes obtained from the last axis step • position() – returns the position of the node in the list of nodes satisfying the last axis step • name() – returns the name (tag) of the current node • count(XPath) – returns the number of nodes satisfying XPath

  35. //country/city[last()] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries>

  36. //city[position()<2] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries>

  37. //*[name()="city" or name()="country"] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries>

  38. //*[starts-with(name(),“c")] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries>

  39. //country[count(./city)>=1] world.xml <?xmlversion="1.0"?> <!DOCTYPEcountriesSYSTEM"world.dtd"> <countries> <country continent="&as;"> <name>Israel</name> <population year="2001">6199008</population> <city capital="yes"><name>Jerusalem</name></city> <city><name>Ashdod</name></city> </country> <country continent="&eu;"> <name>France</name> <population year="2004">60424213</population> </country> </countries>

  40. Final Remarks • The syntax of XPath that was presented here is the abbreviated syntax • For example, city/../@nameis an abbrv. of child::city/parent::node() /attribute::name • More details on XPath: • XPath tutorial in W3Schools • XPath W3C Recommendation

  41. XSL Transformations (XSLT) An Example: Useful Links in the DBI site

  42. XSLT • XSLT is a language for transforming XML documents into other XML documents • For example, XHTML, WML • Can also transform XML to general text documents, e.g., SQL programs • An XSLT program is itself an XML document (called an XSL stylesheet) that describes the transformation process for input documents

  43. XSLT Processors XSLT Processor

  44. Web Pages – The Whole Picture Web Page Data Presentation Knowledge Doc. Structure XSL XHTML Style CSS XML Dynamics JavaScript

  45. <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cdcountry="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> <cdcountry="UK"> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> </cd> <cdcountry="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> <price>9.90</price> </cd> </catalog> catalog.xml

  46. Valid XML! Commands are XML elements with the namespace xsl Includes XHTML elements <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:templatematch="/"> <html> <head><title>cd catalog</title></head> <body><h1>This is a cd catalog!</h1></body> </html> </xsl:template> </xsl:stylesheet> catalog.xsl

  47. Applying XSL Stylesheets to XML There are several ways of applying an XSL stylesheet to an XML document: • Directly applying anXSLT 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, not the original XML

  48. Processing XSL in Java You can use the XALAN package of Apache in order to process XSL transformations javaorg.apache.xalan.xslt.Process -INmyXmlFile.xml -XSLmyXslFile.xsl -OUTmyOutputFile.html

  49. 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 attributethat 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 to recursively process other nodes, e.g., the children of the current node • Finally, the XSLT processor simply processes the root node of the document (that matches /)

  50. Templates • A template has the form <xsl:templatematch="pattern"> ... </xsl:template> • The content of a template consists of • XML elements (e.g., XHTML) and text that are copied to the result • XSL elements (<xsl:…>) that are actually instructions • The syntax for the pattern is a subset of XPath

More Related