Rendering XML Document

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

description

Rendering XML Document

Transcript of Rendering XML Document

Page 1: Rendering XML Document

Rendering XML DocumentsRendering XML Documents

XMLXML

http://yht4ever.blogspot.com

[email protected] - NIIT Quang Trung

08/2007

Page 2: Rendering XML Document

Rendering XML Documents Slide 2

Contents

Formatting Data using XSLT

Introducing XSL

Introducing CSS

Define rendering

Page 3: Rendering XML Document

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 Document

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 Document

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 Document

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 Document

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 Document

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 Document

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 Document

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 Document

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 Document

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 Document

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 Document

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 Document

Rendering XML Documents Slide 15

Formatting Data using XSLT (cont.)

Page 16: Rendering XML Document

Rendering XML Documents Slide 16

Defining Conditional Formatting

Conditional formatting refers to the formatting of data based on a specified condition.

The two elements used for conditional formatting are:

• if • choose

Page 17: Rendering XML Document

Rendering XML Documents Slide 17

Defining Conditional Formatting (cont.)

The if Element Allows specifying condition using the if-then

construct. Follows the syntax:

<xsl:if test="condition">

[actions to be performed if the condition is true]

</xsl:if>

Page 18: Rendering XML Document

Rendering XML Documents Slide 18

Defining Conditional Formatting (cont.)

The choose Element Enables you to choose from two or more possible

courses of action by testing multiple conditions. Follows the syntax: <xsl:choose> [action to be taken] </xsl:when> : : <xsl:otherwise> [action to be taken] </xsl:otherwise> </xsl:choose>

Page 19: Rendering XML Document

Rendering XML Documents Slide 19

Defining Conditional Formatting (cont.)

Comparison and Boolean Operators Used with the if and choose elements to narrow

down the formatting criteria. The following table lists various comparison and

Boolean operators:

Operator Meaning Example

= Equal to PRICE[. = 20]

PRODUCTNAME[. = ‘Mini Bus’]

!= Not equal to PRICE[. != 20]

PRODUCTNAME[. != ‘Barbie Doll’]

Page 20: Rendering XML Document

Rendering XML Documents Slide 20

Defining Conditional Formatting (cont.)

Comparison and Boolean OperatorsOperator Meaning Example

&lt; Less than PRICE[. &lt; 20]

&gt; Greater than PRICE[. &gt; 20]

&lt;= Less than or equal to PRICE[. &lt;= 20]

&gt;= Greater than or equal to PRICE[. &gt;= 20]

and Logical AND PRICE[. &gt 20 and . &lt; 30]

or Logical OR PRICE[. = 20 or . = 45]

not Negation operator PRICE[not(. = 30)]

Page 21: Rendering XML Document

Rendering XML Documents Slide 21

Creating Nested Style Sheets

The import statement can be used to nest one XSLT style sheet within another.

The general form of the import statement is: <xsl:import href="location"/>. The import element is the top-level element

that appears immediately after the stylesheet element :

Page 22: Rendering XML Document

Rendering XML Documents Slide 22

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 23: Rendering XML Document

Rendering XML Documents Slide 23

Q&A

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

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

Page 24: Rendering XML Document

http://yht4ever.blogspot.com