XML, XSLT. Discussion on Markup Languages, Trends.

85
XML, XSLT

Transcript of XML, XSLT. Discussion on Markup Languages, Trends.

Page 1: XML, XSLT. Discussion on Markup Languages, Trends.

XML, XSLT

Page 2: XML, XSLT. Discussion on Markup Languages, Trends.

Discussion on Markup Languages, Trends

Page 3: XML, XSLT. Discussion on Markup Languages, Trends.

What is XML ?

• XML is the Extensible Markup Language• It is designed to enable the use of SGML on the World Wide

Web.• It defines ‘an extremely simple dialect of SGML • XML is not a single, predefined markup language: it's a

metalanguage -- a language for describing other languages • XML lets you define your own customized markup languages • XML is a markup language for structured documentation

Page 4: XML, XSLT. Discussion on Markup Languages, Trends.

What is XML for?

• To make it easy and straightforward to use SGML on the Web– easy to define document types– easy to author and manage SGML-defined documents– easy to transmit and share them across the Web

• It defines ‘an extremely simple dialect of SGML which is completely described in the XML Specification

• The goal is to enable generic SGML to be served, received, and processed on the Web in the way that is now possible with HTML

• XML has been designed for ease of implementation, and for interoperability with both SGML and HTML

Page 5: XML, XSLT. Discussion on Markup Languages, Trends.

What is SGML?

• SGML is the Standard Generalized Markup Language (ISO 8879)

• It is the international standard for defining descriptions of the structure and content of different types of electronic document

• SGML is quite complex to implement and contains a lot of features that are very rarely used

• SGML parsers and browsers are complex and difficult to write

Page 6: XML, XSLT. Discussion on Markup Languages, Trends.

• Extensibility

• Structure

• Validation

• Media independence

• Platform independence

• Meaningful markup

• Strict hierarchies ensure clean structures.

• Define your own schema to suit tags

Why XML?

Page 7: XML, XSLT. Discussion on Markup Languages, Trends.

• Structure modeling (DTD)

• UNICODE - multilingual support

• XML replaces ASCII CSV for data interchange

• XML provides the means for structured information exchange

Why XML?

Page 8: XML, XSLT. Discussion on Markup Languages, Trends.

Why XML?

• A bridge between SGML and HTML• Simplified version of SGML

– SGML too complex and heavy to use– Difficult to interpret an SGML document – A new standard which would take the best features of SGML,

yet keep it SIMPLE

• HTML is to rigid. It has solved it’s purpose. XML is more extensible

• By defining your own markup language (DTD), you can encode the information of your documents much more precisely

Page 9: XML, XSLT. Discussion on Markup Languages, Trends.

Why XML?

• It removes two constraints which are holding back Web development– dependence on a single, inflexible document type (HTML)– the complexity of full SGML, whose syntax allows many powerful but hard-to-

program options

• Authors and providers can design their own document types using XML, instead of being stuck with HTML

• Document types can be explicitly tailored to an audience, so the cumbersome fudging that has to take place with HTML to achieve

special effects can become a thing of the past

Page 10: XML, XSLT. Discussion on Markup Languages, Trends.

Why XML?

• Information content can be richer and easier to use, because the hypertext linking abilities of XML are much greater than those of HTML

• XML can provide more and better facilities for browser presentation and performance, using CSS and XSL stylesheets

• Information will be more accessible and reusable, because the more flexible markup of XML can be used by any XML software instead of being restricted to specific manufacturers as has become the case with HTML

Page 11: XML, XSLT. Discussion on Markup Languages, Trends.

SGML, XML and HTML…

• SGML is the `mother tongue', used for describing thousands of different document types

• HTML is just one of these document types, the one most frequently used in the Web– It defines a simple, fixed type of document with markup

designed for a common class of documents

• XML is an abbreviated version of SGML– Makes it easier for you to define your own document types– Omits the more complex and less-used parts of SGML

Page 12: XML, XSLT. Discussion on Markup Languages, Trends.

SGML, XML and HTML…

• XML itself does not replace HTML: instead, it provides an alternative which allows you to define your own set of markup elements

• HTML is expected to remain in common use for some time to come

– Document Type Definitions for HTML are available in XML versions (XHTML)

Page 13: XML, XSLT. Discussion on Markup Languages, Trends.

HTML• Presentation +

Unstructured data

• It is an application of SGML

• Predefined tags

• Inability to nest components properly.

• One view

XMLData structure divorced from presentation (more

OO like)

• It is a subset of SGML, meta language

• User defined tags

• Ability to nest components using DTD.

• One document, multiple views

HTML V/s XML

Page 14: XML, XSLT. Discussion on Markup Languages, Trends.

- ExampleA Resume

HTMLRepresentation

<HTML> <HEAD> <TITLE>RESUME</TITLE> </HEAD> <BODY> <B>Name</B> Amit Rekhi<BR/> <B>Age</B> 23yrs<BR/> </BODY></HTML>

XMLRepresentation

<!ELEMENT Resume (Name,Age, Sex,…)>

<!ELEMENT Name(#PCDATA)><!ELEMENT Age (#PCDATA)><!ELEMENT Sex (#PCDATA)>

<?xml version=“1.0”?>

<Resume> <Name>Amit

Rekhi</Name> <Age>23yrs</Age> <Sex>Male</Sex></Resume>

DTD

XML

Page 15: XML, XSLT. Discussion on Markup Languages, Trends.

What happened to HTML?

• HTML is already overburdened with dozens of interesting but incompatible inventions from different manufacturers, because it provides only one way of describing your information.

• It is too rigid and fixed. No easy, generic and standard way to extend HTML

• It mixes structure with presentation• It does not allows groups of people or organizations to create their

own customized, standardized markup applications for exchanging information in their domain

• HTML has served it’s purpose of making the web popular. Now something better and more extensible is needed

Page 16: XML, XSLT. Discussion on Markup Languages, Trends.

What happened to HTML?

• HTML is broken. A few start tags do not have end tags.

• Can I make my existing HTML files work in XML? If so how?

Page 17: XML, XSLT. Discussion on Markup Languages, Trends.

Why XML and not Words or Notes?

• Public information cannot afford to be restricted to one make or model or manufacturer

• It is helpful for such information to be in a standard form that can be reused in many different ways, as this can minimize wasted time and effort

• Proprietary data formats, no matter how well documented or publicized, are simply not an option

• XML gives a standard interchange format for document interchange which is easily understood programmatically

Page 18: XML, XSLT. Discussion on Markup Languages, Trends.

Is XML same as C/C++/Java?

• XML is a markup language• C/C++/Java are programming languages• XML does not have programming constructs.

– No if, for…..next etc.– No compiler ONLY a parser

• If no constructs, then how to represent

logic? Do I need to do it?

If so where is it done?

Page 19: XML, XSLT. Discussion on Markup Languages, Trends.

How to control presentation in XML?

• In XML, you can define your own tagset, consequently browsers cannot know anything about the names/elements you use so the use of a stylesheet is required

• XML deals only with structure. Style and presentation is taken care of seperately

• Concept of presentation is similar to Document View Architecture

• How should presentation be taken care of? Any reuse of XML here?

Page 20: XML, XSLT. Discussion on Markup Languages, Trends.

Can I use Java/C++/Scripts in XML?

• XML is ONLY about describing information

• Scripting languages and others enable embedded functionality which helps enables information to be manipulated at the user's end, are not used to represent structure

• No place for PLs in XML

• Do I need to represent PL logic in XML? How?

Page 21: XML, XSLT. Discussion on Markup Languages, Trends.

Can I use PLs to create XML files?

• Any programming language can be used to output data from any source in XML format

• XML is an interchange format. It is an input/output format. It only represents structure

• Implementations are available to manipulate XML

• Should you have APIs to access XML? What types? How would APIs relate to PLs?

Page 22: XML, XSLT. Discussion on Markup Languages, Trends.

How to execute XML files?

• You can't and you don't

• XML is not a programming language, so XML files don't ‘run’ or ‘execute’

• XML files are data: You have to

– Run a program which displays them (like a browser)

– Write a program that does some work with them (like a converter which writes the data in another format)

– Create a program that creates them

Page 23: XML, XSLT. Discussion on Markup Languages, Trends.

What does XML look like?

<?xml version="1.0" standalone="yes"?><conversation> <greeting>Hello, world!</greeting> <response> Stop the planet,I want to get off! </response></conversation>

Page 24: XML, XSLT. Discussion on Markup Languages, Trends.

What does XML look like?<?xml version="1.0" standalone="no" encoding="UTF-8"?><!DOCTYPE titlepage SYSTEM "http://www.frisket.org/dtds/typo.dtd" [<!ENTITY % active.links "INCLUDE">]><titlepage> <white-space type="vertical" amount="36"/> <title font="Baskerville" size="24/30" alignment="centered">Hello, world!</title><image location="http://www.foo.bar/fleuron.eps" type="URL" alignment="centered"/></titlepage>

Page 25: XML, XSLT. Discussion on Markup Languages, Trends.

XML - A Simple Example Structure Definition

<!ELEMENT order (order-no, deliver-to, item+) >

<!ELEMENT order-no (#PCDATA) > <!ELEMENT deliver-to (address) > <!ELEMENT item (name, quantity) >

• DTD - defines STRUCTURE of XML documents

• DTD - used for VALIDATION of XML documents

Page 26: XML, XSLT. Discussion on Markup Languages, Trends.

XML - Instance Data

<!DOCTYPE order SYSTEMS“http://www.something.org/messages/xml/message1.xml”>

<order><order-no>0000123</order-no><deliver-to> <address> <company>A. B. Infosys Private Limited</company> <street>B-102, Gulmohar Park</street> <town>New Delhi</town> <region>India</region> <postcode>110049</postcode> </address></deliver-to><item> <name>Pencil</name> <quantity>12</quantity></item>

</order>

Page 27: XML, XSLT. Discussion on Markup Languages, Trends.

• Separation of (XSL) from Structure (XML-DTD)

• All XML based languages parsed using single browser

• Easy Development of 3-tier Web Applications.

• Data integration from disparate sources.

• XML data is self-describing

• Interchange format of a variety of applications

Benefits of XML

Page 28: XML, XSLT. Discussion on Markup Languages, Trends.

• Local computation and manipulation.

• Multiple views of data.

• XML based on Open Standards

Benefits of XML

Page 29: XML, XSLT. Discussion on Markup Languages, Trends.

EXERCISE 1

XML

Page 30: XML, XSLT. Discussion on Markup Languages, Trends.

What do you see here?<?xml version="1.0" encoding="UTF-8” standalone="yes"?><!DOCTYPE FAQ SYSTEM "FAQ.DTD"><FAQ> <INFO> <SUBJECT>XML</SUBJECT> <AUTHOR>Lars Marius Garshol</AUTHOR> <EMAIL>[email protected]</EMAIL> <VERSION>1.0</VERSION> <DATE>20.jun.97</DATE> </INFO> <PART NO="1"> <Q NO="1"> <QTEXT>What is XML?</QTEXT> <A>SGML light.</A> </Q> </PART></FAQ>

Page 31: XML, XSLT. Discussion on Markup Languages, Trends.

… And here?<!ELEMENT FAQ (INFO, PART+)><!ELEMENT INFO (SUBJECT, AUTHOR, EMAIL?, VERSION?, DATE?)><!ELEMENT SUBJECT (#PCDATA)><!ELEMENT AUTHOR (#PCDATA)><!ELEMENT EMAIL (#PCDATA)><!ELEMENT VERSION (#PCDATA)><!ELEMENT DATE (#PCDATA)><!ELEMENT PART (Q+)><!ELEMENT Q (QTEXT, A)>

<!ELEMENT QTEXT (#PCDATA)><!ELEMENT A (#PCDATA)>

<!ATTLIST PART NO CDATA #IMPLIED TITLE CDATA #IMPLIED><!ATTLIST Q NO CDATA #IMPLIED>

Page 32: XML, XSLT. Discussion on Markup Languages, Trends.

XML Document

Well-formed

*Obeys XML syntax

Valid

*Conforms to DTD

Page 33: XML, XSLT. Discussion on Markup Languages, Trends.

Concepts

• Document Type Definition (DTD)

• Validity

• Well-formedness

Page 34: XML, XSLT. Discussion on Markup Languages, Trends.

Concepts - DTD

• A Document Type Definition (DTD) is a file written in XML's declaration syntax

• It contains a formal description of the syntax, structure particular type of document

– It sets out what names can be used for element types

– It sets out where elements may occur

– It also shows how all elements and other constructs fit together

• The concept of a DTD and XML is similar to a class and an object

Page 35: XML, XSLT. Discussion on Markup Languages, Trends.

DTD SampleA DTD fragment:

. . .

<!ELEMENT List (Item)+>

<!ELEMENT Item (#PCDATA)>

An XML instance:. . .<List> <Item>Chocolate</Item>

<Item>Music</Item>

<Item>Surfing</Item>

</List>

Page 36: XML, XSLT. Discussion on Markup Languages, Trends.

Concepts - Validity

• Valid XML files are those which have a Document Type Definition (DTD) and adhere to it

• They must already be well-formed

• A valid file begins with a Document Type Declaration (DTD)

• An XML version of the specified DTD must be accessible to the XML processor. This can be specified by supplying the URL for the DTD in a System Identifier

• Sample XML file:

– <?xml version="1.0"?> <!DOCTYPE advert SYSTEM “….”>

Page 37: XML, XSLT. Discussion on Markup Languages, Trends.

Concepts - Well-formedness

• All tags must be balanced: that is, all elements which may contain character data must have both start- and end-tags present

• All attribute values must be in quotes

• Any EMPTY element tags must either end with ‘/>’ or you have to make them appear non-EMPTY by adding a real end-tag

• There must not be any isolated markup-start characters (< or &) in your text data. If present it should be escaped.

• Elements must nest inside each other properly

Page 38: XML, XSLT. Discussion on Markup Languages, Trends.

Sample well-formed XML file?<?xml version="1.0" standalone="yes"?><foo> <bar>...<blort/>...</bar></foo>

Page 39: XML, XSLT. Discussion on Markup Languages, Trends.

Structure of an XML file

Word Document

Page 40: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure: Elements

• Elements are the most common form of markup

• Delimited by angle brackets (start tags, end tags), most elements identify the nature of the content they surround.

• Some elements may be empty in which case they have no content and are shown as <element/>

• If an element is not empty, it begins with a start-tag, <element>, and ends with an end-tag, </element>

• Element Sample

– <Element>Sample Content </Element>

Page 41: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure: Attributes

• Attributes are name-value pairs that occur inside tags after the element name

• All attribute values must be quoted

• Attribute Sample

– <Element attr1=“sample attribute”>Sample Content </Element>

Page 42: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure: Entity References

• An entity reference refers to the content of a named entity

• They are used to insert reserved markup characters (<, &, “) characters into your document as content

• Entities are also used to refer to often repeated or varying text and to include the content of external files

• In order to use an entity, you simply reference it by name

• References to parsed general entities use ampersand (&) and semicolon (;) as delimiters. Parameter-entity references use percent-sign (%) and semicolon (;) as delimiters

Page 43: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure: Char. References

• Entity References Sample

– &amp; and %pe1;

• Is a special form of entity reference

• Can be used to insert arbitrary Unicode characters into your document

• This is a mechanism for inserting characters that cannot be directly typed

• Character references take one of two forms:

– decimal references eg. &#8478;

– hexadecimal references eg. &#x211E;

Page 44: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Comments

• Comments begin with “<!--” and end with “-->”

• They can contain any data except the literal string “--”

• You can place comments between markup anywhere in your document

• They are not part of the textual content of an XML document

• XML Comment Sample

– <!-- This is a sample comment -->

Page 45: XML, XSLT. Discussion on Markup Languages, Trends.

XML Struc.:Processing Instructions

• Processing instructions (PIs) are an escape hatch to provide information to an application

• They are not textually part of the XML document

• They have the form: <?name pidata?>

– The name, called the PI target, identifies the PI to the application

• The names used in PIs may be declared as notations in order to formally identify them

• Any data that follows the PI target is optional, it is for the application that recognizes the target

• XML PI Sample

– <? xml version=“1.0” ?>

Page 46: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure.:CDATA Sections

• CDATA sections may occur anywhere character data may occur

• They are used to escape blocks of text containing characters which would otherwise be recognized as markup

• They begin with the string "<![CDATA[" and end with the string "]]>”

• The only string that cannot occur in a CDATA section is “]]>”

• Sample XML CDATA Section:

– <![CDATA[ I < 3; ]]>

Page 47: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure

Document Type Declarations

Page 48: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Doc.Type Decl.

• For any XML document to have meaning there must be some constraint on the sequence and nesting of tags

• Declarations are where these constraints can be expressed

• Declarations allow a document to communicate meta-information to the parser about its content

• Meta-information includes

– Allowed sequence and nesting of tags

– Attribute values and their types and defaults

– the names of external files that may be referenced and whether or not they contain XML

– the formats of some external (non-XML) data, and entities

Page 49: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Doc.Type Decl.

• There are four kinds of declarations in XML: – element declarations

– attribute list declarations

– entity declarations

– notation declarations

Page 50: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Element Decl.

• The element structure of an XML document may, for validation purposes, be constrained using element type and attribute-list declarations.

• Element type declarations often constrain which element types can appear as children of the element

• Element declarations identify the names of elements and the nature of their content (content model)

• In addition to element names, the special symbol #PCDATA is reserved to indicate character data

• Elements with both element content and PCDATA content are said to

have “mixed content”

Page 51: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Element Decl.

• Three other content models are possible:

– EMPTY indicates that the element has no content (and consequently no end-tag)

– ANY indicates that any content is allowed

– ELEMENT content indicates only elements within an element. Significance of *, +, ?, |, ,

• XML Element Decl. Sample• <!ELEMENT oldjoke (burns+, allen, applause?)>• <!ELEMENT burns (#PCDATA | quote)*>• <!ELEMENT allen (#PCDATA | quote)*>• <!ELEMENT quote (#PCDATA)*>• <!ELEMENT applause empty>

Page 52: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Attribute Decl.

• Attribute declarations identify

– Which elements may have attributes

– What attributes they may have

– What values the attributes may hold

– What default value each attribute has

• Each attribute in a declaration has three parts: a name, a type, and a default value

• There are six possible types:

– CDATA: CDATA attributes are strings, any text is allowed

Page 53: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Attribute Decl.

– ID: The value of an ID attribute must be a name.

• All of the ID values used in a document must be different

• IDs uniquely identify individual elements in a document

• Elements can have only a single ID attribute

– IDREF/IDREFS:An IDREF attribute's value must be the value of a single ID attribute on some element in the document

• The value of an IDREFS attribute may contain multiple IDREF values separated by white space.

Page 54: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Attribute Decl.

– ENTITY or ENTITIES: An ENTITY attribute's value must be the name of a single entity.

• The value of an ENTITIES attribute may contain multiple ENTITY values separated by white space

– NMTOKEN or NMTOKENS: Name token attributes are a restricted form of string attribute

• The value of an NMTOKENS attribute may contain multiple NMTOKEN values separated by white space.

– A list of names: The value of an attribute must be taken from a specific list of names

• This is frequently called an enumerated type

Page 55: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Attribute Decl.

• There are four possible default values:

– #REQUIRED: The attribute must have an explicitly specified value on every occurrence of the element in the document

– #IMPLIED: The attribute value is not required, and no default value is provided

• If a value is not specified, the XML processor must proceed without one.

– "value”: An attribute can be given any legal value as a default

• The attribute value is not required on each element, but if it is not present, it will appear to be the default.

Page 56: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Attribute Decl.

– #FIXED "value”: An attribute declaration may specify that an attribute has a fixed value

• The attribute is not required, but if it occurs, it must have the specified value

• One use for fixed attributes is to associate semantics with an element

• XML Attribute Declaration sample: <!ATTLIST oldjoke name ID #required label CDATA #implied status ( funny | notfunny ) 'funny'>

Page 57: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Entity Decl.

• Entity declarations allow you to associate a name with some other fragment of the document. The other fragment could be:

– A chunk of regular text

– A chunk of the document type declaration

– A reference to an external file containing either text or binary data

• There are three kinds of entities:

– Internal Entities: The replacement text is stored in the declaration

• Internal entities allow to define shortcuts for frequent text or text that is expected to change

Page 58: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Entity Decl.

• The XML specification predefines five internal entities:

– &lt; produces the left angle bracket, <

– &gt; produces the right angle bracket, >

– &amp; produces the ampersand, &

– &apos; produces a single quote character (an apostrophe), '

– &quot; produces a double quote character, "

• Internal Entity Declaration Sample:

– <!ENTITY ATI "ArborText, Inc.">

Page 59: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Entity Decl.

– External Entities: External entities allow an XML document to refer to an external file

• External entities contain either text or binary data

• If they contain text, the content of the external file is inserted at the point of reference and parsed as part of the referring document (Parsed)

• Binary data is not parsed and may only be referenced in an attribute (Unparsed)

– External Entity Declaration Sample:• <!ENTITY boilerplate SYSTEM "/standard/legalnotice.xml">

• <!ENTITY ATIlogo SYSTEM "/standard/logo.gif" NDATA GIF87A>

Page 60: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Entity Decl.

– Parameter Entities: Parameter entities can only occur in the document type declaration

• A parameter entity is identified by placing “% ” (percent-space) in front of its name in the declaration

• Parameter entity references are immediately expanded in the document type declaration and their replacement text is part of the declaration

– Parameter Entity Declaration Sample:

– <!ENTITY % ATI "ArborText, Inc.">

Page 61: XML, XSLT. Discussion on Markup Languages, Trends.

XML Structure:Notation Decl.

• Notation declarations identify specific types of external binary data

– This information is passed to the processing application, which may make whatever use of it it wishes

– Notation Declaration Sample: – <!NOTATION GIF87A SYSTEM "GIF">

Page 62: XML, XSLT. Discussion on Markup Languages, Trends.

EXERCISE 2

DTD

Page 63: XML, XSLT. Discussion on Markup Languages, Trends.
Page 64: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT - Introduction

• Has evolved from the early Extensible Stylesheet Language (XSL) standard

• An XSLT style sheet is an XML document

• Specifies a language definition for – XML data presentation (XSL-FO): Future uncertain!– XML data transformations (XSLT)

• XSLT is a programming language for transforming XML documents

Page 65: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT - Introduction

• Adopts the XPath language syntax for expressions

• Supports a small, flexible data types:– Boolean, number, string, node-set

• Supports a full set of operations:– <xsl:template>, <xsl:apply-templates>, <xsl:sort>, <xsl:output>

• Supports programming flow-control:– <xsl:if>, <xsl:for-each>, <xsl:choose>

Page 66: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT – Hello World!

<?xml version="1.0"?>

<?xml-stylesheet type="text/xsl" href="hello.xsl"?>

<hello-world>

<greeter>An XSLT Programmer</greeter>

<greeting>Hello, World!</greeting>

</hello-world>

Page 67: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT - Introduction<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform

version="1.0">

<xsl:template match="/hello-world">

<HTML> <HEAD> <TITLE></TITLE> </HEAD>

<BODY><H1><xsl:value-of select="greeting"/></H1>

<xsl:apply-templates select="greeter"/>

</BODY></HTML>

</xsl:template>

<xsl:template match="greeter">

<DIV>from <I><xsl:value-of select="."/></I> </DIV>

</xsl:template>

</xsl:stylesheet>

Page 68: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT – Introduction

• XSLT is rule-based and declarative

• Rules are called template rules– A template rule is an instruction to transform a

specified source element in a particular way

• XSLT processors: – Put XML document and XSLT style sheets together

as data and code– Invoke template rules to produce the desired output

Page 69: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT – Behind the Scenes

• XSLT processors:– Put XML, XSLT together as data, code

– Invoke template rules to produce the desired output

• XSLT Processing Steps:– Reading XML, associated XSLT style sheets– Parsing XML files, associated XSLT files, into

trees of nodes

Page 70: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT – Behind the Scenes

• XSLT Processing Steps:– Applying XSLT transformation to the source

trees– Producing result trees according to XSLT

specification– Serializing the result trees as output files

• Formats such as: XML, HTML, Text

Page 71: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT – Behind the Scenes

• The XSLT processor does not work on raw XML data

• Raw XML data is first parsed into an XML DOM object (tree of nodes)

• Tree representation allows building expressions in the XPath language

• A template rule is invoked when the specified element, as expressed in an XPath pattern, is matched

Page 72: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT – Linking to XML

• Embed an XSLT style sheet inside the source XML document

<?xml-stylesheet type="text/xsl" href="hello.xsl"?>

• Explicitly call:– transformNode()/ transformNodeToObject()

method on the source XML DOM

Page 73: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT – Linking to XML

• Embed an XSLT style sheet inside the source XML document

<?xml-stylesheet type="text/xsl" href="hello.xsl"?>

• Explicitly call:– transformNode()/ transformNodeToObject()

method on the source XML DOM

Page 74: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT: Behind the Scenes

Page 75: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• XML Declaration– An XSLT file is an XML file– An XML declaration could be used:

<?xml version="1.0" encoding="UTF-8"?>

• XSLT Stylesheet Declaration– The style sheet declaration is <xsl:styesheet>

<xsl:stylesheet version="1.0“

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Page 76: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• Name Space Declaration Prefix– The following name space declaration is required:

xmlns:xsl="http://www.w3.org/1999/XSL/Transform – Additional namespace prefixes can also be added

xmlns:msxsl="urn:schemas-microsoft-com:xslt" – Default namespace has no prefix, usually used for

literal result elements

Page 77: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• Output Method Declaration– <xsl:output> used by the processor to

determine how to serialize the result tree– Must be a child of the <xsl:stylesheet>

element <xsl:output method="xml"/>

<xsl:output method="html"/>

<xsl:output method="text"/>

Page 78: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• XSLT Variables– Identify generically named objects, from which

a value can be retrieved as needed– Can be used as a cache for a value– Provide mechanism for

• Setting a value• Referring to result tree fragment

<xsl:variable name=“variable1”>variable_val</xsl:variable>

Page 79: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• XSLT Parameters– Used to pass data– Local parameters pass data from one

template rule to another– Global parameters pass a value into a style

sheet from outside

<xsl:paramater name=“variable1” select=“”param_val/>

Page 80: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• External Document References

– document() XSLT function allows you to incorporate another XML file

– <xsl:import> element imports another style sheet into the current style sheet

– <xsl:include> element includes another style sheet into the

current style sheet

<xsl:import href=“…”/><xsl:include href=“…”/>

Page 81: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• Match Rules– Consists of two components

• Match pattern• Template itself

– Match Pattern consists of• Match Attribute• XML Path Language (XPath) expression

<xsl:template match="region">[template]</xsl:template> <xsl:template match="region">

Page 82: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• Match Rules– When template finds match in the source tree

that matches match pattern, it places content in the result tree

– template rule instantiates the template for each match

Page 83: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• Literal Result Elements– Templates can contain 2 kinds of elements:

• Elements in the XSLT namespace, such as <xsl:if>• Elements not in the XSLT namespace, such as

<HTML>, <BODY>– Such elements are treated as literal result elements– Such elements are copied to the output with minimal

processing– HTML elements in the XSLT file must be closed

Page 84: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• Comments– Comments in XSLT work the same as

comments in XML

• XPath Expressions – XPath expression selects a node or nodes

that match specified pattern– Expression is run against node tree

representation of XML document

Page 85: XML, XSLT. Discussion on Markup Languages, Trends.

XSLT Components

• Namespace Prefixes– Commonly used namespace prefixes in an

XSLT style sheet are as follows:

<xsl:stylesheet version="1.0"

xmlns="urn:schemas-mycompany-com:xslt“

xmlns:xsl=http://www.w3.org/1999/XSL/Transform

xmlns:msxsl="urn:schemas-microsoft-com:xslt“

xmlns:fo="http://www.w3.org/1999/XSL/Format">