Top Banner
Rendering XML Documents Rendering XML Documents XML XML http://yht4ever.blogspot.com [email protected] B070066 - NIIT Quang Trung 08/2007
19

Rendering XML Documents

Jan 21, 2015

Download

Technology

yht4ever

Rendering XML Documents
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rendering XML Documents

Rendering XML DocumentsRendering XML Documents

XMLXML

http://yht4ever.blogspot.com

[email protected] - NIIT Quang Trung

08/2007

Page 2: Rendering XML Documents

Rendering XML Documents Slide 2

Contents

Formatting Data using XSLT

Introducing XSL

Introducing CSS

Define rendering

Page 3: Rendering XML Documents

Rendering XML Documents Slide 3

Define rendering

Rendering refers to processing an XML document to present it with the desired formatting.

XML does not focus on formatting. Therefore, a style sheet is required for formatting and rendering XML documents.

A style sheet performs the following tasks: Transform an XML document into another structure and render it

to a specific target. Describe how to present the transformed information.

Style sheets can be of the following three types: Cascading Style Sheets eXtensible Style Sheets Document Style Semantics and Specification Language

(DSSSL)

Page 4: Rendering XML Documents

Rendering XML Documents Slide 4

Introducing CSS

Creating CSS The syntax for coding a CSS is:elementname {

property1: value; property2: value;

} In the preceding syntax:

• elementname specifies the name of the element. • property1 and property2 specify the property

names.• Value specifies the property values for a property

name.

Page 5: Rendering XML Documents

Rendering XML Documents Slide 5

Introducing CSS (cont.)

Applying CSS The CSS file needs to be associated with the XML

document for applying formatting specifications. A CSS can be applied to an XML document using the

Processing Instruction: <?xml:stylesheet type="text/css" href="path-name"?>

In the above statement:• xml:stylesheet instructs the browser that the XML document

uses a stylesheet.

• type specifies the type of formatting used. • href specifies the name of the CSS file used to format the

XML document.

Page 6: Rendering XML Documents

Rendering XML Documents Slide 6

Demo & Exercises

Ex1: The employee information is stored in the

emp.xml file. Use this file to display the employee details in the following format:

• Name of the employee: Times New Roman, blue, 20pts

• Designation: Arial, green, 10pts• Department: Arial, green, 10pts• Salary: Times New Roman, blue, 20pts

Use cascading style sheets to format the data in the emp.xml file.

Page 7: Rendering XML Documents

Rendering XML Documents Slide 7

Introducing XSL

CSS does not support the reorder, sort, and display of elements based on a condition.

For such advanced formatting, XML supports eXtensible Stylesheet Language (XSL).

XSL has two parts: XSL Transformations (XSLT) XML Path (XPath)

XSL: Contains instructions on how an XML document should be

transformed into an HTML or XHTML document. Uses XPath expressions to extract specific data from an XML

document. The XSLT processor transforms the XML document

into an HTML or XHTML or into another XML document.

Page 8: Rendering XML Documents

Rendering XML Documents Slide 8

Introducing XSL (cont.)

Comparing CSS and XSLT• The following table draws a comparison between CSS and XSLT:

CSS XSLT

Simple to use, and suitable for simple documents.

Complex to use.

Cannot reorder, add, delete, or perform operations on elements.

Can reorder, add, or delete elements.

Does not offer access to non-elements, such as attributes, attribute values, and PI.

Allows access and manipulation of the comments, PI, and attribute values and names within an XML document.

Uses less memory. Uses more memory and processor power.

Does not use XML syntax. Follows the XML syntax.

Page 9: Rendering XML Documents

Rendering XML Documents Slide 9

Introducing XSL (cont.)

Analyzing the Working of the XSLT Processor The XSLT processor applies the transformation information to

the source document and builds the result tree as shown in the following figure:

Page 10: Rendering XML Documents

Rendering XML Documents Slide 10

Formatting Data using XSLT

Formatting Data using XSLT XSLT provides the following elements to select and

format data: • stylesheet• value-of• for-each• sort• text

The stylesheet element: • Instructs the browser that the document is a style sheet file.• Is the root element for all XSLT style sheets.• Is written as:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

Page 11: Rendering XML Documents

Rendering XML Documents Slide 11

Formatting Data using XSLT (cont.)

Formatting Data using XSLT (cont.) The value-of element:

• Displays the value of the specified element or attribute. • Follows the syntax:

<xsl:value‑of select="elementname/attributename"/>

The for-each element: • Instructs the XSLT processor to process the information for

each instance of the specified pattern.• Follows the syntax:

<xsl:for-each select="pattern">

[action to be performed]

</xsl:for-each>

Page 12: Rendering XML Documents

Rendering XML Documents Slide 12

Formatting Data using XSLT (cont.)

Formatting Data using XSLT (cont.) The sort element:

• Sorts data based on the values assigned to elements and attributes.

• Follows the syntax:

<xsl:sort select="expression" order="ascending|descending" case-order="upper-first|lower-first“

data-type="text|number|qname"/>

The text element: • Generates constant text in the output and displays labels. • Follows the syntax:

<xsl:text> Text to be displayed as label </xsl:text>

Page 13: Rendering XML Documents

Rendering XML Documents Slide 13

Formatting Data using XSLT (cont.)

Creating XSLT Template Rules A template rule:

• Describes how an XML element and its contents are converted into a specific format for displaying in the browser.

• Consists of two parts, pattern and action. The template element:

• Defines a template for the desired output• Follows the syntax:

<xsl:template match="pattern"> [action to be taken] </xsl:template>

Page 14: Rendering XML Documents

Rendering XML Documents Slide 14

Formatting Data using XSLT (cont.)

Creating XSLT Template Rules (cont.) The apply-template element:

• Instructs the XSLT processor to find an appropriate template and perform the specified tasks on selected elements.

• Follows the syntax:<xsl:apply‑templates [select="pattern"]>

Page 15: Rendering XML Documents

Rendering XML Documents Slide 15

Formatting Data using XSLT (cont.)

Page 16: Rendering XML Documents

Rendering XML Documents Slide 16

Demo & Exercises

To be continued…

Page 17: Rendering XML Documents

Rendering XML Documents Slide 17

Reference

XML How to programhttp://www.w3.orgTeach Yourself XML in 21 Days, 3rd EditionLearning XML, 2nd EditionAndy Clark presentation.XML tutorial

http://www.w3schools.com/w3c/

Page 18: Rendering XML Documents

Rendering XML Documents Slide 18

Q&A

Feel free to post questions at http://yht4ever.blogspot.com

or email to: [email protected] or [email protected]

Page 19: Rendering XML Documents

http://yht4ever.blogspot.com