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

  • Published on
    19-Dec-2015

  • View
    214

  • Download
    2

Embed Size (px)

Transcript

<ul><li> Slide 1 </li> <li> 1 Extensible Stylesheet Language (XSL) Extensible Stylesheet Language (XSL) </li> <li> Slide 2 </li> <li> 2 The XSL Standard XSL consists of 3 parts: XPathXPath (navigation in documents) XSLTXSLT (transformation of documents) XSLFOXSLFO (FO for formatting objects) - A rather complex language for typesetting (e.g., for preparing text for printing) - It will not be taught </li> <li> Slide 3 </li> <li> 3 XML Path Language (XPath) </li> <li> Slide 4 </li> <li> 4 XML example revisited: </li> <li> Slide 5 </li> <li> 5 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml </li> <li> Slide 6 </li> <li> 6 The XML DOM Model The root is implicit (Does not appear in the text of the XML document) </li> <li> Slide 7 </li> <li> 7 world.dtd </li> <li> Slide 8 </li> <li> 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&gt;10000000] </li> <li> Slide 9 </li> <li> 9 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml /countries/country[population&gt;10000000] </li> <li> Slide 10 </li> <li> 10 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //country[@continent="Asia"]/city </li> <li> Slide 11 </li> <li> 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 </li> <li> Slide 12 </li> <li> 12 Applying XPath to XML Formally, the result of applying an XPath e to an XML document (or a context node in the document) is the 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 </li> <li> Slide 13 </li> <li> 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 / </li> <li> Slide 14 </li> <li> 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 </li> <li> Slide 15 </li> <li> 15 Child-Axis Examples /countries </li> <li> Slide 16 </li> <li> 16 Child-Axis Examples /countries/country/city </li> <li> Slide 17 </li> <li> 17 Child-Axis Examples city/name Context </li> <li> Slide 18 </li> <li> 18 Child-Axis Examples /*/country/* An attribute is not an element child! </li> <li> Slide 19 </li> <li> 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 </li> <li> Slide 20 </li> <li> 20 Descendant Examples /countries//name </li> <li> Slide 21 </li> <li> 21 Descendant Examples.//* Context </li> <li> Slide 22 </li> <li> 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 -- </li> <li> Slide 23 </li> <li> 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 </li> <li> Slide 24 </li> <li> 24 Attribute Examples //country/@continent </li> <li> Slide 25 </li> <li> 25 Attribute Examples @continent Context </li> <li> Slide 26 </li> <li> 26 Attribute Examples //@* </li> <li> Slide 27 </li> <li> 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 </li> <li> Slide 28 </li> <li> 28 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //country[./population&gt;10000000] 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&gt;10000000] </li> <li> Slide 29 </li> <li> 29 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //country[.//city] An XPath evaluates to true if and only if its result is not empty </li> <li> Slide 30 </li> <li> 30 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //country[//city] Why? </li> <li> Slide 31 </li> <li> 31 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //country[population[.&gt;3000000 and @year&gt;2003]] </li> <li> Slide 32 </li> <li> 32 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //country[name="Israel" or name="Spain"]/population </li> <li> Slide 33 </li> <li> 33 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //country/city[2] 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) </li> <li> Slide 34 </li> <li> 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 </li> <li> Slide 35 </li> <li> 35 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //country/city[last()] </li> <li> Slide 36 </li> <li> 36 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //city[position()=1]"&gt; </li> <li> 39 Israel 6199008 Jerusalem Ashdod France 60424213 world.xml //country[count(./city)&gt;=1] </li> <li> Slide 40 </li> <li> 40 Final Remarks The syntax of XPath that was presented here is the abbreviated syntax For example, city/../@name is an abbrv. of child::city/parent::node() /attribute::name More details on XPath: - XPath tutorial in W3Schools XPath tutorial in W3Schools - XPath W3C Recommendation XPath W3C Recommendation </li> <li> Slide 41 </li> <li> 41 XSL Transformations (XSLT) An ExampleAn Example: Useful Links in the DBI site </li> <li> Slide 42 </li> <li> 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 </li> <li> Slide 43 </li> <li> 43 XSLT Processors XSLT Processor </li> <li> Slide 44 </li> <li> 44 Web Page Presentation Doc. Structure Data Web Pages The Whole Picture XML XSLXHTML Style Knowledge CSS Dynamics JavaScript </li> <li> Slide 45 </li> <li> 45 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog.xml </li> <li> Slide 46 cd catalog This is a cd catalog! Valid XML! Commands are XML elemen"&gt; cd catalog This is a cd catalog! Valid XML! Commands are XML elements with the namespace xsl Includes XHTML elements catalog.xsl"&gt; cd catalog This is a cd catalog! Valid XML! Commands are XML elemen" title="46 cd catalog This is a cd catalog! Valid XML! Commands are XML elemen"&gt; </li><li> 46 cd catalog This is a cd catalog! Valid XML! Commands are XML elements with the namespace xsl Includes XHTML elements catalog.xsl </li> <li> Slide 47 </li> <li> 47 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, not the original XML </li> <li> Slide 48 </li> <li> 48 Processing XSL in Java You can use the XALAN package of Apache in order to process XSL transformations java org.apache.xalan.xslt.Process -IN myXmlFile.xml -XSL myXslFile.xsl -OUT myOutputFile.html </li> <li> Slide 49 </li> <li> 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 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 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 /) </li> <li> Slide 50 </li> <li> 50 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 syntax for the pattern is a subset of XPath </li> <li> Slide 51 cd catalog This is a cd catalog! catalog1.xsl"&gt; cd catalog This is a cd catalog! catalog1.xsl"&gt; cd catalog This is a cd catalog! catalog1.xsl" title="51 cd catalog This is a cd catalog! catalog1.xsl"&gt; </li><li> 51 cd catalog This is a cd catalog! catalog1.xsl </li> <li> Slide 52 </li> <li> 52 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 catalog1.xml view catalog1.xml </li> <li> Slide 53 </li> <li> 53 The Result cd catalog This is a cd catalog! Implicitly added to </li> <li> Slide 54 </li> <li> 54 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" </li> <li> Slide 55 </li> <li> 55 Processing starts by applying a temp. to the root - If no specified template matches the root, then one is inserted by default (see the next slide) You must specify explicitly whether templates should be applied to descendants of a node Done using the instruction: - In xpath, cur. processed node is the context node Without the select attribute, this instruction processes all the children of the current node (including text nodes) </li> <li> Slide 56 </li> <li> 56 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) </li> <li> Slide 57 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.9"&gt; Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 "&gt; Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.9" title="57 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.9"&gt; </li><li> 57 Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 </li> <li> Slide 58 A cd! A cd!"&gt; A cd! A cd!"&gt; A cd! A cd!" title="58 A cd! A cd!"&gt; </li><li> 58 A cd! A cd! </li> <li> Slide 59 A cd!"&gt; A cd!"&gt; A cd!" title="59 A cd! Aretha: Lady Soul Aretha Franklin 9.90 A cd!"&gt; </li><li> 59 A cd! Aretha: Lady Soul Aretha Franklin 9.90 A cd! </li> <li> Slide 60 An artist!"&gt; </li><li> 60 An artist! An artist! </li> <li> Slide 61 An artist!"&gt; An artist! Why?"&gt; An artist!" title="61 An artist!"&gt; </li><li> 61 An artist! Why? </li> <li> Slide 62 </li> <li> 62 Frequently Used Elements of XSL - This element extracts the value of a node from the nodelist located by xpath - This element loops over all the nodes in the node list located by xpath,, etc. - This element is for conditional processing </li> <li> Slide 63 cd catalog CD catalog [ ] Currently selected element is the context"&gt; cd catalog CD catalog [ ] Currently selected element is the context node Example 1 catalog2.xsl view catalog2.xml"&gt; cd catalog CD catalog [ ] Currently selected element is the context" title="63 cd catalog CD catalog [ ] Currently selected element is the context"&gt; </li><li> 63 cd catalog CD catalog [ ] Currently selected element is the context node Example 1 catalog2.xsl view catalog2.xml </li> <li> Slide 64 cd catalog CD catalog Example 2 catalog3.xsl"&gt; cd catalog CD catalog Example 2 catalog3.xsl"&gt; cd catalog CD catalog Example 2 catalog3.xsl" title="64 cd catalog CD catalog Example 2 catalog3.xsl"&gt; </li><li> 64 cd catalog CD catalog Example 2 catalog3.xsl </li> <li> Slide 65 </li> <li> 65 : ( now on sale: $ ) Example 2 (cont.) Entities replace characters price&gt;10 price cd catalog CD catalog CDs are iterated in ascending order of the ti" title="68 cd catalog CD catalog CDs are iterated in ascending order of the ti"&gt; </li><li> 68 cd catalog CD catalog CDs are iterated in ascending order of the titles catalog4.xsl view catalog4.xml </li> <li> Slide 69 </li> <li> 69 Setting Values in Attributes The element cannot be used within an 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 </li> <li> Slide 70 </li> <li> 70 An Example In the following example, we add to each CD entitled t a link to the URL /showcd.php?title=t : view catalog5.xml </li> <li> Slide 71 </li> <li> 71 Using Using : showcd/?title= </li> <li> Slide 72 </li> <li> 72 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 </li> <li> Slide 73 </li> <li> 73 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...</li></ul>

Recommended

View more >