xml iii. learning objectives formatting xml documents: overview using cascading style sheets to...

19
XML III

Upload: molly-cross

Post on 28-Mar-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

XML III

Page 2: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

Learning ObjectivesFormatting XML Documents: OverviewUsing Cascading Style Sheets to format XML

documentsUsing XSL to format XML documents

Page 3: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

Formatting XML documents Unlike HTML, XML documents have no bearing on how

they are displayed. In order to display XML documents, you must specify the

display information. There are 2 approaches for achieving this:

With the use of Cascading Style Sheets (CSS)With the use of Extensible Style Language (XSL)

The use of CSS in formatting XML documents is quite similar to its use in formatting HTML documents.

XSL, on the other hand is an evolving technology based on XML vocabulary, that is more robust than CSS.

Its subset used to transform XML documents is XSLT (XSL Transformation) which can transform XML documents from one format to another.

Page 4: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

XSLT can be used for instance to transform an XML document into an HTML document, for display purposes.

In addition, because XSLT is a transformation language, you can still use CSS with it to help style the rendered output.

It is important to realise that XSL and CSS are not competing technologies

Rather, their usage in styling XML documents is complementary. CSS can be used to style HTML, while XSL can not; XSL can be used to transform XML documents, while CSS can not.

XSL Formatting Objects technology is being designed as a superset of CSS, that will effectively replace CSS.

When an XML document is transformed with XSLT, the transformation applies to the logical tree structure of the XML document, and the result is a tree of XML data.

Page 5: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

Formatting XML documents with CSS CSS is nothing more than a formatting language, hence has

the following limitations when compared to XSL: It can not take a piece of document data and reuse it

elsewhere. Has no concept of sibling relationship between nodes Does not support decision structures (conditionals) It can not calculate quantities, or store values in variables. Can not be used to sort data contained in an XML

document.

Creating CSS for XML documents There are 2 approaches: External Style sheets and

Document-level style sheets

Page 6: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

External Style sheets are the preferred approach, as it truly separates content from presentation.

This approach also facilitates using one CSS to style multiple documents.

The external style sheet is basically a group of CSS style rules stored with a .css extension.

The file is referenced from XML documents through the ‘xml-stylesheet’ processing instruction:

<?xml-stylesheet type=“text/css” href=“Addressbook.css?> Within the stylesheet, the styles are defined for the required

nodes of the XML document.

Page 7: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

Example:<?xml version=“1.0”?>

<?xml-stylesheet type=“text/css” href=“Addressbook.css”?>

<!DOCTYPE addressbook SYSTEM “Addressbook.dtd”>

<addressbook>

<contact>

<name>Tony Benn</name>

<address>210 Temple road</address>

<city>London</city>

<postcode>NW9 0RT</postcode>

<phone>02082049565</phone>

</contact>

<contact>

<! – Definition of other contact(s) -- >

</addressbook>

Page 8: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

Addressbook.css

contact {display: block; width: 200px; border: 4px; color: blue;

background-color: silver; text-align: center; }

name { display: block; font-family: Times, serif; font-size: 16pt; font-weight: bold; }

address { display: block; font-family:Times, serif; font-size: 14pt; }

city, postcode {display: inline; font-family: Times, serif;

font-size: 14pt; }

phone {display: none;} Note that the contact element in the XML document does not

contain any text. Hence, the style properties defined for it are passed to its child elements.

The child elements in this example do have their own styles defined, and these would over-ride those defined at a higher hierarchy (the contact element), thus demonstrating the ‘cascading’ effect of CSS.

Page 9: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

XSL Extensible Style Language is a style sheet technology

designed specifically for XML. Goes much further than CSS in manipulating the logical

structure of XML documents. Includes high-powered programmatic constructs. Implemented in XML vocabulary, so looks like a typical

XML document. Includes 2 fundamental technologies:

XSL Transformation (XSLT)XSL Formatting Objects

XSLT is used to transform, while XSL Formatting Objects is used to format XML documents. These technologies can be used independently or jointly.

Page 10: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

XPath An expression language used by XSLT to address parts of an

XML document. XSLT uses XPath as the basis for traversing an XML document. The syntax used by XPath is designed for use in URIs and XML

attribute values, which requires it to be very concise. XPath operates under the assumption that a document has been

parsed into a tree of nodes. It defines different types of nodes that are used to describe the

nodes that appear within a tree. There is always a single root node that serves as the root of an

XPath tree. Every element in the document has a corresponding element

node. Also used in conjunction with XPointer and XLink technologies

to provide advanced linking features for XML.

Page 11: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

Creating XSL Style Sheets An XSL style sheet consists of two fundamental constructs.

Templates andPatterns

A template is an XSL structure that describes output to be generated on the basis of certain pattern-matching criteria.

In effect, it defines a transformation structure that is applied to a certain portion of XML document.

Templates are defined in XSL by using the xsl:template element

It also uses an optional attribute named match to match patterns in an XML document.

Page 12: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

The widest possible match for a document is to set the match attribute to “/”, which indicates that the root of the tree is to be matched.<xsl:template match=“/”>

....

</xsl:template>

The data in the XML document that matches the pattern in the match attribute is passed on to the template for processing.

<xsl: template match=“contact”>

<xsl:value-of/>

</xsl:template>

Page 13: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

Patterns Used in XSL templates to perform matches to the different

parts of the XML document. It describes a branch of an XML tree Syntax is similar to specifying paths on a disk drive. For e.g. addressbook/contact/phone selects phone elements

that appear beneath a contact element, that in turn appears beneath an addressbook element.

To select an entire document tree, you use the root pattern which consists of a single forward slash (/).

XSLT Template Constructs Define several constructs that control the application of

templates in XSL style sheets.

Page 14: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

xsl:value-of Element Used to insert the value of an element or attribute in the resulting

output of the style. Allows you to output XML content in virtually any context, e.g

between a pair of HTML tags. E.g.<xsl:template match=“name”><h2><xsl:value-of/></h2></xsl:template>

xsl:if Element Used to perform conditional matches in templates. Uses the match attribute as the ‘xsl:template’ element.<xsl:if match=“@rating=5”<xsl:apply-templates select=“movie”/></xsl:if>

Page 15: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

xsl:for each Element Used to establish a loop for iterating through elements in a

document. Contains the order-by attribute for sorting data. Its select attribute determines which elements (or attributes) are

selected as part of the loop’s iteration. E.g.

<xsl:for-each order-by=“+price” select=“vehicles/vehicle”>

<tr>

<td><xsl:value-of select=“@year”/></td>

<td><xsl:value-of select=“@make”/></td>

<td><xsl:value-of select=“mileage”/></td>

</tr>

</xsl:for-each>

Page 16: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

xsl:apply-templates Element Used to apply templates that are defined in a style sheet. When an XSL processor encounters an ‘xsl:apply templates’

element in a style sheet, the template corresponding to the pattern in the select attribute is applied.

E.g.

<xsl:apply-templates select=“state”/>

<xsl:apply-templates select=“address”/>

<!-- Definition of other elements -->

<xsl:template match=“state”>

<b><xsl:value-of/><b>

</xsl:template>

Page 17: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

Example:<?xml version=“1.0”?>

<!DOCTYPE addressbook SYSTEM Addressbook.dtd>

<addressbook>

<contact>

<name>Tony Benn</name>

<address>210 Temple road</address>

<city>London</city>

<postcode>NW9 0RT</postcode>

<phone>02082049565</phone>

</contact>

<!-- More contacts defined here -->

Page 18: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

The Addressbook Style sheet

<?xml version=“1.0”?>

<?xsl:stylesheet xmlns:xsl=“http://www.w3.org/TR/WD-xsl”>

<xsl:template match=“/”>

<html><head><title>Address Book Example</title></head>

<body bgcolor=“aqua”>

<xsl:for-each select=“addressbook/contact”>

<xsl:apply-templates select=“name”/>

<xsl:apply-templates select=“address”/>

<xsl:apply-templates select=“city”/>

<xsl:apply-templates select=“postcode”/>

<xsl:apply-templates select=“phone”/>

</xsl:for-each>

</body>

</html>

Page 19: XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents

<xsl:template match=“name”><h2><xsl:value-of/></h2></xsl:template><xsl:template match=“address”><xsl:value-of/><br/></xsl:template><xsl:template match=“city”><xsl:value-of/>,</xsl:template><xsl:template match=“postcode”><xsl:value-of/><br/></xsl:template></xsl:stylesheet>

Note that a template is first defined for the entire document, before individual templates are defined for each selected component of the document.