1 / 91

XSLT – Extensible Style Language for Transformation

XSLT – Extensible Style Language for Transformation. Presented by: Igor Borodin Ilya Brodin. Motivation. Transformation of XML document – Why do we need it? Store in one format, display in another Transforming XML to XHTML and displaying in browser, for example

kipp
Download Presentation

XSLT – Extensible Style Language for Transformation

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. XSLT – Extensible Style Language for Transformation Presented by: • Igor Borodin • Ilya Brodin

  2. Motivation Transformation of XML document – Why do we need it? • Store in one format, display in another Transforming XML to XHTML and displaying in browser, for example • Convert to a more useful format • Make the document more compact Extracting from XML documents only the data we need The common: we are interested to get another document that looks like we specify

  3. Transformation basics • For performing a transformation, we need: • Input document • The specification of the transformation (how to perform it, what is the input, what is the output) • Transformation processor • XSLT is a language for defining transformations of XML documents

  4. Transformation basics (cont.) • XSLT is a declarative XML-based stylesheet language • XSLT stylesheet is an XML document by itself • It consists of a set of rules that match patterns in input XML document • The rules declare what output should be produced when a pattern in the XML document is matched • The order of the definition of rules doesn’t matter

  5. Transformation basics (cont.) • Transformation processor applies the rules on an XML document, producing the output • The output is a text document that may be an XML document, of course. So, XSLT transformation allows us to transform XML in any text based format.

  6. Simple example • Lets examine a simple example of using XSLT to transform an XML document into an HTML table: • Input document: people.xml • Stylesheet: people.xsl with the rules of transformation • In order to perform transformation specified by people.xsl, we have to insert the following processor instruction at the beginning of the input document: <?xml-stylesheet type="text/xsl" href="people.xsl"?>

  7. people.xml:<?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="people.xsl"?><PEOPLE> <PERSON> <NAME>Mark Wilson</NAME> <ADDRESS>911 Somewhere Circle, Canberra, Australia</ADDRESS> <TEL>(++612) 12345</TEL> <FAX>(++612) 12345</FAX> <EMAIL>Mark.Wilson@somewhere.com</EMAIL> </PERSON> <PERSON> <NAME>Tracey Wilson</NAME> <ADDRESS>121 Zootle Road, Cape Town, South Africa</ADDRESS> <TEL>(++2721) 531 9090</TEL> <FAX>(++2721) 531 9090</FAX> <EMAIL>Tracey.Wilson@somewhere.com</EMAIL> </PERSON> Note the assignment of XSLT stylesheet

  8. <PERSON> <NAME>Jodie Foster</NAME> <ADDRESS>30 Animal Road, New York, USA</ADDRESS> <TEL>(++1) 3000 12345</TEL> <FAX>(++1) 3000 12345</FAX> <EMAIL>Jodie.Foster@somewhere.com</EMAIL> </PERSON> <PERSON> <NAME>Lorrin Maughan</NAME> <ADDRESS>1143 Winners Lane, London, UK</ADDRESS> <TEL>(++94) 17 12345</TEL> <FAX>(++94) 17 12345</FAX> <EMAIL>Lorrin.Maughan@somewhere.com</EMAIL> </PERSON> <PERSON> <NAME>Steve Rachel</NAME> <ADDRESS>90210 Beverly Hills, California, USA</ADDRESS> <TEL>(++1) 2000 12345</TEL> <FAX>(++1) 2000 12345</FAX> <EMAIL>Steve.Rachel@somewhere.com</EMAIL></PERSON> </PEOPLE>

  9. people.xsl <?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><HTML><BODY><TABLE BORDER="2"> <TR> <TD>Name</TD> <TD>Address</TD> <TD>Tel</TD> <TD>Fax</TD> <TD>Email</TD> </TR> <xsl:for-each select="PEOPLE/PERSON"> <TR> <TD><xsl:value-of select="NAME"/></TD> <TD><xsl:value-of select="ADDRESS"/></TD> <TD><xsl:value-of select="TEL"/></TD> <TD><xsl:value-of select="FAX"/></TD> <TD><xsl:value-of select="EMAIL"/></TD> </TR> </xsl:for-each></TABLE></BODY></HTML></xsl:template></xsl:stylesheet> rule

  10. The result (after transformation in Mozilla browser)

  11. What have we seen? • Stylesheet is an XML document • XSLT has its own set of tags and its own namespace • Rules are being declared using <xsl:template match=”node-set-expression”> tag • node-set-expression is an XPath expression • The contents of rule definition define the output of the application of the rule

  12. So, why XSLT? • Simple syntax • Easy to write • Easy to read and understand • Reusable • + all the advantages of being the XML based language

  13. The stylesheet as XML document • XSLT stylesheet is an XML document, so: • The whole stylesheet should be well-formed, the following rule is invalid: <xsl:template match="/"> <trifle> <piffle> <flim> </piffle> </trifle> </xsl:template> • Like any XML document, contains XML declaration at the top

  14. The stylesheet as XML document • Its documentelement is <xsl:stylesheet> which contains all the template rules • The XSLT namespace has the URI http://www.w3.org/1999/XSL/Transform • The xsl prefix is used by the transformation processor to determine which elements are part of the stylesheet infrastructure and which are part of the output

  15. Tree representation of an XML document • It is convenient to think about transformation process and output producing as about traveling on the input tree and building the output tree piece-by-piece • Traveling starts from the root of the tree, and proceeds down • So, we will represent XML document as a tree • The same representation we met before in this course

  16. Tree representation of an XML document • Moreover, as we saw in example, XSLT uses XPath to access or refer to parts of an XML document • <xsl:for-each select="PEOPLE/PERSON"> • For understanding and writing XPATH expressions, it is convenient to think of an XML document as a tree • So, let’s briefly recall what the tree representation of an XML document is:

  17. Tree representation of an XML document • There are 7 kinds of nodes: • Element: may contain another elements • Attribute: leaf node • Text: leaf node. One element can have several text child nodes • Comment • Processing instruction • Namespace • Root: an abstract point above the document element

  18. Tree representation of an XML document The following document contains all these types of nodes: <?xml version="1.0"?> <!-- Dee-licious! --> <sandwich xmlns="http://www.food.org/ns"> <ingredient type="grape">jelly</ingredient> <ingredient> <?knife spread thickly?> peanut-butter </ingredient> <ingredient> bread <!-- white bread, preferably --> </ingredient> </sandwich>

  19. Tree representation of an XML document Here is its tree representation:

  20. Expressing Structure with Templates • The rule in the transformation is an XML element called <xsl:template> whose contents are the elements and data that will form the result subtree. Here is an example of template rule: <xsl:template match="/"> <html> <head> <title>My first template rule</title> </head> <body> <h1>Hello, world!</h1> </body> </html> </xsl:template>

  21. Expressing Structure with Templates • The output is an HTML document: • But we can consider the output as a tree. This tree was actually built by the rule, node by node <html> <head> <title>My first template rule</title> </head> <body> <h1>Hello, world!</h1> </body> </html>

  22. Expressing Structure with Templates • Notice the match attribute in the <xsl:template> element. This attribute selects the appropriate level of a source tree. In the example, the attribute selects the root node. This is where traveling on the input tree (transformation) starts, and therefore, our example rule will be the first rule executed • Since the rule doesn't allow processing to continue past the root node (there are no references to the children of this node), it effectively blocks all other rules. The transformation not only begins with this rule, but ends here as well

  23. Expressing Structure with Templates • There are special elements that transmit the processing to another levels of the input tree, i.e. another rules will continue the constructing of the result tree. In the example we saw some of them: • <xsl:for-each select=“node-set-expression”> • <xsl:value-of select=“node-set-expression”> • There are more, will be described later

  24. Selecting nodes • XSLT needs to be able to move through the document tree, to select nodes or data, to describe the location of a node or a group of nodes somewhere in a document • These skills are provided by XPath • XPath calls the description of the location a location path • The structure of a location path: • Axis – describes the direction to travel • Node test – specifies what kinds of nodes are applicable • Set of optional Boolean predicates – winnowing down the candidates even further

  25. XPath- node axes

  26. XPath- node tests

  27. XPath- location path examples

  28. XPath- predicate examples

  29. XPath- location path shortcuts

  30. Match patterns • Location paths most often used in match attributes of <xsl:template> elements (we will call them match patterns) • But there is a little difference from the generic case: • Only descending or self-referential axes may be used • Axes like parent and preceding make things way too complicated and could possibly set up infinite loops

  31. Match patterns - examples • <xsl:template match="aphorism"> . . . </xsl:template> • Will be called each time element <aphorism> is found • <xsl:template match=“quote | aphorism”> . . . </xsl:template> • Will be called each time elements <aphorism> or <quote> are found • <xsl:template match=“category/quote”> . . . </xsl:template> • Will be called each time element <quote> that live inside <category> is found

  32. Match patterns - examples • <xsl:template match=“quote[@id = ‘4’]"> . . . </xsl:template> • Will be called each time <quote> having attribute id with value 4is found • <xsl:template match=“@type”> . . . </xsl:template> • Will be called each time attribute node type is found • <xsl:template match=“quote[source/text(‘Greg Travis’)]”> . . . </xsl:template> • Will be called each time element <quote> whose source is Greg Travis is found

  33. Resolving conflicts among rules • It's possible for more than one rule to match a node • XSLT processor selects the best match • Here are the rules of precedence: • If the pattern contains multiple alternatives separated by | , each alternative is treated with equal importance, as though there were a separate rule for each • A specific pattern has higher priority than a pattern that contains general information. For example, the pattern quotelist/quote/body is more specific than body, and takes precedence

  34. Resolving conflicts among rules • The rules of precedence (continued): • A pattern that relies on a wildcard is more general and therefore has lower priority than a specific pattern. The pattern stuff/cruft defeats the wildcard pattern stuff/* • A pattern with a successful test expression in square brackets ([]) overrides a pattern with no test expression but that is otherwise identical. So bobo[@role="clown"] is better than bobo • Other information, such as position in the stylesheet, may be used to pare down the set if there is still more than one rule remaining

  35. Resolving conflicts among rules • The <xsl:template> element has an optional priority attribute that can be set to give it precedence over other rules and override the process of determination • The value must be a real number (i.e., it must have a decimal point) between 1.0 and -1.0, with a default of 0 • A larger number overrides a smaller number

  36. Transformation process • Transformation starts at the root of the document, “/” in XPath • It maintains the stack of rules • For each node it meets, it calls the template rule that match this node • What to do if there is no rule defined? Next slide answers… • The rule produces the output that it specifies. It can call another template, putting it at the top of the stack • If the stack becomes empty, transformation stops! • We have seen the example of such situation

  37. Default rules • What happens if transformation process finds a node in the tree and there is no rule defined that match that node? • There aredefault (built-in) template rules to allow recursive processing to continue in the absence of a successful pattern match by an explicit template rule in the stylesheet • Each node type has a built-in rule that matches it • So, there is always rule for any node • Default rules have the lowest priority, thus defined rules override them

  38. Default rules • Root or any element: • <xsl:template match="*|/"> <xsl:apply-templates/> </xsl:template> • <xsl:apply-templates/>instruction calls the templates that match immediate children of the current node (puts them on stack) • We found a problem – it doesn’t calls the templates of attribute child nodes • Text and attribute nodes: • <xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template> • <xsl:value-of select="."/>instruction copies the value of the matched node to the output (text in this case). Formal definition of value – in next slides.

  39. Default rules • Processing instruction or comment: • <xsl:template match="processing-instruction()|comment()"/> • Takes no action – filters that nodes out • Namespace nodes: • The built-in template rule for namespace nodes is also to do nothing • There is no pattern that can match a namespace node • So, the built-in template rule is the only template rule that is applied for namespace nodes

  40. Boolean conditions • What if we interested to produce the output under some conditions? • There are several ways to do so, each relies on the result of certain XPath expression • First, XPath expression can evaluate to the following types of values: • Boolean (true or false) • Node set • Number • String • Result tree fragment (a mixture of nodes in a tree structure that aren't well-formed XML)

  41. Boolean conditions • Each of the returned types can be converted to a Boolean value according to the following rules:

  42. Boolean conditions • So, lets return to our question: what if we interested to produce the output under some conditions? • One way: we can write several rules, each one will be called when our condition is satisfied • For example: <xsl:template match="category[@type=‘humor’]"> . . . </xsl:template> <xsl:template match="category[@type=‘philosophy’]"> . . . </xsl:template>

  43. Boolean conditions • Another way – to use XSLT special elements for conditional processing • <xsl:if  test = “boolean-expression”>. . . </xsl:if> • For example: <xsl:template match="category"> <xsl:if test="@type='humor'"> . . . </xsl:if> </xsl:template> <xsl:if test="@type='philosophy'"> . . . </xsl:if> </xsl:template> <xsl:template match="*"> <xsl:if test="child::*"> I have children. </xsl:if> </xsl:template>

  44. Boolean conditions <xsl:template match="category[@type=‘humor’]"> Hello, world! </xsl:template> • Let’s define stylesheet with one of these rules only. Will the output be the same? (the . . . is the same text) <xsl:template match="category"> <xsl:if test="@type='humor'"> Hello, world! </xsl:if> </xsl:template> No! Can you explain why?

  45. Boolean conditions <xsl:template match="*"> <xsl:choose> <xsl:when test="self::chapter"> I am a chapter. </xsl:when> <xsl:when test="self::appendix"> I am an appendix. </xsl:when> <xsl:otherwise> I don't know what I am. </xsl:otherwise> </xsl:choose> </xsl:template> • Another useful element is<xsl:choose> • Each <xsl:when> is evaluated sequentially. The first node that matches is the one whose content is used • If nothing matches, the content of <xsl:otherwise> is used

  46. XPath node set functions • XSLT uses XPath expressions not only for matching, but for manipulating nodes too • We can use all the node set function of XPath

  47. XPath node set functions • There are also some functions that create node sets

  48. Numeric expressions • A number in XSLT is defined to be a 64-bit floating point number, whether it has a decimal point or no • Any expression type can be converted into Number:

  49. Numeric expressions • XSLT provides a number of functions and operators to manipulate numeric values (actually, XPath functions)

  50. String expressions • Any expression can be converted intoa string using the string() function

More Related