Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents."— Presentation transcript:


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

3 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.

4 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.

5 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

6 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: Within the stylesheet, the styles are defined for the required nodes of the XML document.

7 Example: Tony Benn 210 Temple road London NW9 0RT

8 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.

9 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.

10 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.

11 Creating XSL Style Sheets An XSL style sheet consists of two fundamental constructs. Templates and Patterns 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.

12 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..... The data in the XML document that matches the pattern in the match attribute is passed on to the template for processing.

13 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.

14 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:if Element Used to perform conditional matches in templates. Uses the match attribute as the xsl:template element.

15 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 loops iteration. E.g.

16 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.

17 Example: Tony Benn 210 Temple road London NW9 0RT

18 The Addressbook Style sheet Address Book Example

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

Download ppt "XML III. Learning Objectives Formatting XML Documents: Overview Using Cascading Style Sheets to format XML documents Using XSL to format XML documents."

Similar presentations

Ads by Google