Rendering XML Documents


Published on

Rendering XML Documents

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Rendering XML Documents

    1. 1. Rendering XML Documents XML [email_address] B070066 - NIIT Quang Trung 08/2007
    2. 2. Contents Formatting Data using XSLT Introducing XSL Introducing CSS Define rendering
    3. 3. Define rendering <ul><li>Rendering refers to processing an XML document to present it with the desired formatting. </li></ul><ul><li>XML does not focus on formatting. Therefore, a style sheet is required for formatting and rendering XML documents. </li></ul><ul><li>A style sheet performs the following tasks: </li></ul><ul><ul><li>Transform an XML document into another structure and render it to a specific target. </li></ul></ul><ul><ul><li>Describe how to present the transformed information. </li></ul></ul><ul><li>Style sheets can be of the following three types: </li></ul><ul><ul><li>Cascading Style Sheets </li></ul></ul><ul><ul><li>eXtensible Style Sheets </li></ul></ul><ul><ul><li>Document Style Semantics and Specification Language (DSSSL) </li></ul></ul>
    4. 4. Introducing CSS <ul><li>Creating CSS </li></ul><ul><ul><li>The syntax for coding a CSS is: </li></ul></ul><ul><ul><li>elementname { </li></ul></ul><ul><ul><li>property1: value; </li></ul></ul><ul><ul><li> property2: value; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>In the preceding syntax: </li></ul></ul><ul><ul><ul><li>elementname specifies the name of the element. </li></ul></ul></ul><ul><ul><ul><li>property1 and property2 specify the property names. </li></ul></ul></ul><ul><ul><ul><li>Value specifies the property values for a property name. </li></ul></ul></ul>
    5. 5. Introducing CSS (cont.) <ul><li>Applying CSS </li></ul><ul><ul><li>The CSS file needs to be associated with the XML document for applying formatting specifications. </li></ul></ul><ul><ul><li>A CSS can be applied to an XML document using the Processing Instruction: </li></ul></ul><ul><ul><li><?xml:stylesheet type=&quot;text/css&quot; href=&quot;path-name&quot;?> </li></ul></ul><ul><ul><li>In the above statement: </li></ul></ul><ul><ul><ul><li>xml:stylesheet instructs the browser that the XML document uses a stylesheet. </li></ul></ul></ul><ul><ul><ul><li>type specifies the type of formatting used. </li></ul></ul></ul><ul><ul><ul><li>href specifies the name of the CSS file used to format the XML document. </li></ul></ul></ul>
    6. 6. Demo & Exercises <ul><li>Ex1: </li></ul><ul><ul><li>The employee information is stored in the emp.xml file. Use this file to display the employee details in the following format: </li></ul></ul><ul><ul><ul><li>Name of the employee: Times New Roman, blue, 20pts </li></ul></ul></ul><ul><ul><ul><li>Designation: Arial, green, 10pts </li></ul></ul></ul><ul><ul><ul><li>Department: Arial, green, 10pts </li></ul></ul></ul><ul><ul><ul><li>Salary: Times New Roman, blue, 20pts </li></ul></ul></ul><ul><li>Use cascading style sheets to format the data in the emp.xml file. </li></ul>
    7. 7. Introducing XSL <ul><li>CSS does not support the reorder, sort, and display of elements based on a condition. </li></ul><ul><li>For such advanced formatting, XML supports e X tensible S tylesheet L anguage (XSL). </li></ul><ul><li>XSL has two parts: </li></ul><ul><ul><li>XSL Transformations (XSLT) </li></ul></ul><ul><ul><li>XML Path (XPath) </li></ul></ul><ul><li>XSL: </li></ul><ul><ul><li>Contains instructions on how an XML document should be transformed into an HTML or XHTML document. </li></ul></ul><ul><ul><li>Uses XPath expressions to extract specific data from an XML document. </li></ul></ul><ul><li>The XSLT processor transforms the XML document into an HTML or XHTML or into another XML document. </li></ul>
    8. 8. Introducing XSL (cont.) <ul><li>Comparing CSS and XSLT </li></ul><ul><ul><li>The following table draws a comparison between CSS and XSLT: </li></ul></ul>Follows the XML syntax. Does not use XML syntax. Uses more memory and processor power. Uses less memory. Allows access and manipulation of the comments, PI, and attribute values and names within an XML document. Does not offer access to non-elements, such as attributes, attribute values, and PI. Can reorder, add, or delete elements. Cannot reorder, add, delete, or perform operations on elements. Complex to use. Simple to use, and suitable for simple documents. XSLT CSS
    9. 9. Introducing XSL (cont.) <ul><li>Analyzing the Working of the XSLT Processor </li></ul><ul><ul><li>The XSLT processor applies the transformation information to the source document and builds the result tree as shown in the following figure: </li></ul></ul>
    10. 10. Formatting Data using XSLT <ul><li>Formatting Data using XSLT </li></ul><ul><ul><li>XSLT provides the following elements to select and format data: </li></ul></ul><ul><ul><ul><li>stylesheet </li></ul></ul></ul><ul><ul><ul><li>value-of </li></ul></ul></ul><ul><ul><ul><li>for-each </li></ul></ul></ul><ul><ul><ul><li>sort </li></ul></ul></ul><ul><ul><ul><li>text </li></ul></ul></ul><ul><ul><li>The stylesheet element: </li></ul></ul><ul><ul><ul><li>Instructs the browser that the document is a style sheet file. </li></ul></ul></ul><ul><ul><ul><li>Is the root element for all XSLT style sheets. </li></ul></ul></ul><ul><ul><ul><li>Is written as: </li></ul></ul></ul><ul><ul><ul><li><xsl:stylesheet xmlns:xsl=&quot;; version=&quot;1.0&quot;> </li></ul></ul></ul>
    11. 11. Formatting Data using XSLT (cont.) <ul><li>Formatting Data using XSLT (cont.) </li></ul><ul><ul><li>The value-of element: </li></ul></ul><ul><ul><ul><li>Displays the value of the specified element or attribute. </li></ul></ul></ul><ul><ul><ul><li>Follows the syntax: </li></ul></ul></ul><ul><ul><ul><li><xsl:value‑of select=&quot;elementname/attributename&quot;/> </li></ul></ul></ul><ul><ul><li>The for-each element: </li></ul></ul><ul><ul><ul><li>Instructs the XSLT processor to process the information for each instance of the specified pattern. </li></ul></ul></ul><ul><ul><ul><li>Follows the syntax: </li></ul></ul></ul><ul><ul><ul><li><xsl:for-each select=&quot;pattern&quot;> </li></ul></ul></ul><ul><ul><ul><li>[action to be performed] </li></ul></ul></ul><ul><ul><ul><li></xsl:for-each> </li></ul></ul></ul>
    12. 12. Formatting Data using XSLT (cont.) <ul><li>Formatting Data using XSLT (cont.) </li></ul><ul><ul><li>The sort element: </li></ul></ul><ul><ul><ul><li>Sorts data based on the values assigned to elements and attributes. </li></ul></ul></ul><ul><ul><ul><li>Follows the syntax: </li></ul></ul></ul><ul><ul><ul><li><xsl:sort select=&quot;expression&quot; order=&quot;ascending|descending&quot; case-order=&quot;upper-first|lower-first“ </li></ul></ul></ul><ul><ul><ul><li>data-type=&quot;text|number|qname&quot;/> </li></ul></ul></ul><ul><ul><li>The text element: </li></ul></ul><ul><ul><ul><li>Generates constant text in the output and displays labels. </li></ul></ul></ul><ul><ul><ul><li>Follows the syntax: </li></ul></ul></ul><ul><ul><ul><li><xsl:text> Text to be displayed as label </xsl:text> </li></ul></ul></ul>
    13. 13. Formatting Data using XSLT (cont.) <ul><li>Creating XSLT Template Rules </li></ul><ul><ul><li>A template rule: </li></ul></ul><ul><ul><ul><li>Describes how an XML element and its contents are converted into a specific format for displaying in the browser. </li></ul></ul></ul><ul><ul><ul><li>Consists of two parts, pattern and action. </li></ul></ul></ul><ul><ul><li>The template element: </li></ul></ul><ul><ul><ul><li>Defines a template for the desired output </li></ul></ul></ul><ul><ul><ul><li>Follows the syntax: </li></ul></ul></ul><ul><li><xsl:template match=&quot;pattern&quot;> </li></ul><ul><li>[action to be taken] </li></ul><ul><li></xsl:template> </li></ul>
    14. 14. Formatting Data using XSLT (cont.) <ul><li>Creating XSLT Template Rules (cont.) </li></ul><ul><ul><li>The apply-template element: </li></ul></ul><ul><ul><ul><li>Instructs the XSLT processor to find an appropriate template and perform the specified tasks on selected elements. </li></ul></ul></ul><ul><ul><ul><li>Follows the syntax: </li></ul></ul></ul><ul><ul><ul><li><xsl:apply‑templates [select=&quot;pattern&quot;]> </li></ul></ul></ul>
    15. 15. Formatting Data using XSLT (cont.)
    16. 16. Demo & Exercises <ul><li>To be continued… </li></ul>
    17. 17. Reference <ul><li>XML How to program </li></ul><ul><li> </li></ul><ul><li>Teach Yourself XML in 21 Days, 3 rd Edition </li></ul><ul><li>Learning XML, 2 nd Edition </li></ul><ul><li>Andy Clark presentation. </li></ul><ul><li>XML tutorial </li></ul>
    18. 18. Q&A <ul><li>Feel free to post questions at </li></ul><ul><li>or email to: [email_address] or [email_address] </li></ul>
    19. 19. Thank You !