1 / 34

eXtensible Stylesheet Language

eXtensible Stylesheet Language. XSLT Elements Functions XPATH. XSL. XPATH // <value-of-select> <for-each select> <sort> <if> Multiple Conditional tests <choose> XPath operators XPath Functions count() sum() format-number() {} XS-FO XSL Editors. XSL

Télécharger la présentation

eXtensible Stylesheet Language

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 XSLT Elements Functions XPATH

  2. XSL XPATH // <value-of-select> <for-each select> <sort> <if> Multiple Conditional tests <choose> XPath operators XPath Functions count() sum() format-number() {} XS-FO XSL Editors XSL eXtensible Stylesheet Language Transformation Structure of XSL XSLT XSL Elements Variable XPATH Navigating XPath Expressions Templates <apply-templates-select> <xsl:apply-templates> Ex. <apply-templates> Template in External File

  3. XSL = eXtensible Stylesheet Language • The World Wide Web Consortium (W3C) started to develop XSL because there was a need for an XML-based Stylesheet Language. • XSL consists of three parts: • XSLT - a language for transforming XML documents • XPath - a language for navigating in XML documents • XSL-FO - a language for formatting XML documents

  4. Transformation • XSL may be used to generate either XHTML, XML, SVG, SMIL, or text, on browser or printed, oral …. XSL XSLT Processor = Transformer XML Output

  5. XSLT = XSLT Transformation XSLT is the most important part of XSL. XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element. With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more. A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree.

  6. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0“ xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" media-type="text/html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" cdata-section-elements="script style" indent="yes" encoding="ISO-8859-1"/> <xsl:template match="/"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title> XSL: XSLT element, XPath Expressions and Functions</title> </head> <body> <h2> In the company <xsl:value-of select="phonebook/@company" /> there are <xsl:value-of select="count(/phonebook/person)"/> persons. </h2> <xsl:for-each select="phonebook/person"> <xsl:sort order="ascending" select="surname“ /> <xsl:value-of select="surname"/></td> <xsl:choose> <xsl:when test="phone/@confidential='yes'"> <---<br /> </xsl:when> <xsl:otherwise> <xsl:value-of select="phone/homenumber"/><br /> </xsl:otherwise> </xsl:choose> </xsl:for-each> Structure of XSL XSL style sheet is an XML document itself, it begins with the XML declaration: <?xml version="1.0" encoding="ISO-8859-1"?>. Element <xsl:stylesheet>,defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes). The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the XML source document. The content inside the <xsl:template> element defines some HTML to write to the output.

  7. XSL Elements http://www.w3schools.com/xsl/xsl_w3celementref.asp

  8. Variable This could be usefull sometimes even if no9t used in our exacmples. You can create a variable outside of <xsl:template match="/">. Then the variable is global. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:variable name="pi" select="'3.142857142857'"/> <xsl:template match="/" > <HTML> <HEAD> <TITLE>Value of Pi</TITLE> </HEAD> <BODY> The value of pi is <xsl:value-of select="$pi"/> </BODY> </HTML> </xsl:template>

  9. XSLT Uses XPath XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.

  10. Extracting and navigating • Extracting values with XSLT: • use the <xsl:value-of select="…"/> XSL element • Navigating with XPath: • The slash ("/") indicates parent/child relationship • A slash at the beginning of the path indicates that it is an absolute path, starting from the top of the XML document /cdcatalog/cd/title "Start from the top of the XML document, go to the cdcatalog element, from there go to the cd element, and from there go to the title element." / Parent – child relationship // Selects nodes in the document from the current node that matches the selection no matter where they are @ Selects attributes

  11. XPath Expressions XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:

  12. Templates XSLT uses Templates The <xsl:template> element contains rules to apply when a specified node is matched. The match attribute is used to associate the template with an XML element. The match attribute can also be used to define a template for a whole branch of the XML document (i.e. match="/" defines the whole document).

  13. <xsl:apply-templates select> The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/catalog"> <html> <body> <h2>My CD Collection</h2> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="cd"> <p> <xsl:apply-templates select="title"/> <xsl:apply-templates select="artist"/> </p> </xsl:template> <xsl:template match="title"> Title: <span style="color:#ff0000"> <xsl:value-of select="."/></span> <br /> </xsl:template> <xsl:template match="artist"> Artist: <span style="color:#00ff00"> <xsl:value-of select="."/></span> <br /> </xsl:template> </xsl:stylesheet>

  14. <xsl:apply-templates> Applies a template rule to the current element or to the current element's child nodes.

  15. Ex. <xsl:apply-templates>

  16. Template in External File ** w3c.html <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:template name="Validation"> <p> <a href="http://validator.w3.org/"> <img style="border:0;width:88px;height:31px" src="http://validator.w3.org/images/vxhtml10" alt="Valid XHTML 1.0!" /> </a> </p> </xsl:template> </xsl:stylesheet> In xx.xsl <xsl:includehref="w3c.html"/> .. <xsl:call-template name="Validation" />

  17. Xpath // <xsl:template match="school"> .... <h1> EVITech </h1> <h2> Espoo Univercity of Applied Sciences </h2> <p> <img alt="EVTEK" src="evtek.jpg"/> </p> <xsl:apply-templates select="//unit"/> </xsl:template> <xsl:template match="unit"> <xsl:for-each select="text"> <h3><xsl:value-of select="name"/> </h3> <xsl:for-each select="para"> <p> <xsl:value-of select="content"/> </p> </xsl:for-each> </xsl:for-each> </xsl:template> </xsl:stylesheet> See EvtekExtra

  18. <xsl:value-of-select> Extracts the value of a selected element <xsl:value-of> Element <xsl: value-of-select=“title”/> The <xsl:value-of> element can be used to select the value of an XML element and add it to the output stream of the transformation. or attribute <xsl:value-of select="catalog/@music" />

  19. <xsl:for-each select> Loops through each node in a specified node set. The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories. <xsl:for-each select="/catalog/cd"> <tr> <td> <xsl: value-of-select=“title”/></td> <td> <xsl: value-of-select=“artist”/></td> </tr> </xsl:for-each>

  20. <xsl:sort> with <xsl:for-each> Sorts the output. <xsl:for-each select="catalog/cd"> <!-- NOTE THAT SORTING IS DONE BY A NUMBER --> <!-- The data type must be specified number --> <!– Two keys given to sorting: first price, second title--> <xsl:sort data-type="number" order="descending" select="ageLimit" /> <xsl:sort order="ascending" select="title" / <tr bgcolor="#bbbbbb"> <td align="center"><xsl:value-of select="title"/></td> <td align="center"><xsl:value-of select="artist"/></td> <td align="center"><xsl:value-of select="country"/></td> <td align="center"><xsl:value-of select="company"/></td> <td align="center"><xsl:value-of select="price"/></td> </tr> </xsl:for-each>

  21. <xls:sort> with <xsl:apply-templates> <table> <xsl:apply-templatesselect="student"> <xsl:sortorder="ascending" select="surname"/> <xsl:sortorder="ascending" select="firstname"/> </xsl:apply-templates> </table>

  22. <xsl:if> Contains a template that will be applied only if a specified condition is true <xsl:for-each select="catalog/cd"> <!-- To put a conditional if test against the content of the file, add an <xsl:if> element --> <!-- The value of the required test attribute contains the expression to be evaluated.--> <xsl:if test="price&gt;'9'"> <tr bgcolor="#bbbbbb"> <td align="center"><xsl:value-of select="title"/></td> <td align="center"><xsl:value-of select="artist"/></td> <td align="center"><xsl:value-of select="country"/></td> <td align="center"><xsl:value-of select="company"/></td> </tr> </xsl:if> </xsl:for-each> • express single conditional tests:

  23. Multiple conditional tests Used in conjunction with <when> and <otherwise> to express multiple conditional tests <xsl:choose> <xsl:when test='something> [action] </xsl:when> <xsl:when test='something'> [action] </xsl:when> <xsl:otherwise> [action] </xsl:otherwise> </xsl:choose> • express multiple conditional tests: The first xsl:when statement that evaluates to true is executed. If none evaluates to true then the xsl:otherwise statement is executed.

  24. <xsl:choose> xsl:for-each select="catalog/cd"> <tr bgcolor="#00ffff"> <xsl:choose> <xsl:when test="price&lt;'9' and grade&gt;='7'"> <td align="center" bgcolor="#11ff11"> <xsl:value-of select="title"/></td> </xsl:when> <xsl:otherwise> <td align="center"><xsl:value-of select="title"/></td> </xsl:otherwise> </xsl:choose> <td align="center"><xsl:value-of select="artist"/></td> <xsl:choose> ……..

  25. XPath Operators < > = <= >= != Must be written as &lt; &gt; = &lt;= &gt;= !=

  26. XSLT functions 3. Functions on Strings 4. Functions on Booleans 5. Functions on Errors 6. Functions on Sequences 7. Functions on Nodes ... 2. Functions on Durations, Dates and Time year-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10"))Result: day-from-dateTime(xs:dateTime("2005-01-10T12:30-04:10")) Result: 10 …. See in more details: http://www.w3schools.com/xpath/xpath_functions.asp

  27. count() count(set of node) returns an integer representing the number of nodes (i.e., XML elements) in the set. Example. Number of cds = <xsl:value-of select="count(/cdcatalog/cd)"/>

  28. sum() sum(set of node) returns an idecimal point number representing the sum of nodes (i.e., XML elements) in the set. Example where sum is converted to integer: There are <xsl:value-of select="count(/catalog/cd)"/> cds in this catalog. The price of the whole catalog is <xsl:value-of select = "round(sum(/catalog/cd/price))"/> ecus.

  29. format-number() Average price is calculated by summing all the prices and dividing the result by the number of cds. The output is formatted to two digits after decimal point. Average price of a cd is <xsl:value-of select="format-number(sum(/catalog/cd/price) div count(/catalog/cd), '0.00')"/> ecus.

  30. { } = value-of inside brackets <img src="{course/logo}" alt="Metropolia"/> Value of an attribute (” ”) in xsl is written inside { }

  31. Mod() <xsl:element> <xsl:for-each select="//title"> <xsl:element name="tr"> <xsl:choose> <xsl:when test="position() mod 2 = 0"> <xsl:attribute name="class">blue</xsl:attribute></xsl:when> <xsl:otherwise> <xsl:attribute name="class">red</xsl:attribute> </xsl:otherwise> </xsl:choose> <td><xsl:value-of select="../artist"/></td> … <td><xsl:value-of select="position()"/></td> <td><xsl:value-of select="(position() mod 2)"/></td> </xsl:element> </xsl:for-each> </table> css .blue {background-color:#3399FF;} .red {background-color:#FF6666;}

  32. <xsl:element> The <xsl:element> element is used to create an element node in the output document. Exc. Create a "singer" element that contains the value of each artist element: <?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <xsl:for-each select="catalog/cd">    <xsl:element name="singer">      <xsl:value-of select="artist" />    </xsl:element>    <br />  </xsl:for-each></xsl:template></xsl:stylesheet>

  33. <xsl:attribute> Element The <xsl:attribute> element is used to add attributes to elements. Exc. Add a source attribute to the picture element and fill it with values from "images/name" : <picture>  <xsl:attribute name="source">    <xsl:value-of select="images/name" />  </xsl:attribute></picture>

  34. Altova Visual Stylesheet Designer

More Related