valid xml documents to be valid, an xml document –must be well-formed according to the general xml...

104
Valid XML documents To be valid, an XML document must be well-formed according to the general XML syntax rules and, in addition, it must satisfy the syntax rules for an application-specific language based on XML Well-formed XML documents are useful but, to be really useful, a document must be valid For example, if two companies wish to exchange information using XML, the companies must design a special XML-based language which will be capable of expressing the data they wish to exchange and all documents that are exchanged must be valid according to the syntax rules for this special language

Post on 15-Jan-2016

248 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Valid XML documents• To be valid, an XML document

– must be well-formed according to the general XML syntax rules

– and, in addition, it must satisfy the syntax rules for an application-specific language based on XML

• Well-formed XML documents are useful but, to be really useful, a document must be valid

• For example, if two companies wish to exchange information using XML,

– the companies must design a special XML-based language which will be capable of expressing the data they wish to exchange and

– all documents that are exchanged must be valid according to the syntax rules for this special language

Page 2: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Valid XML documents (contd.)• A valid XML document must declare the special XML-

based language according to whose rules the XML document is claimed to be valid

• This done using a document type declaration, which must appear before the start tag of the root element in a valid XML document

• It has the following general format

<!DOCTYPE nameOfRootElement someDTDspec >

where someDTDspec would specify the rules for a valid element whose name is nameOfRootElement

• someDTDspec would either

– directly contain an internal document type definition (a DTD)

– consist of a reference to an external DTD

Page 3: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Valid XML documents (contd.)

• For example, if we wanted to turn the example well-formed XML document we have just seen into a valid document, we would insert a line which would look like

<!DOCTYPE people someDTDspec >

where someDTDspec would specify what rules must be followed by a valid people element

• Although a doctype statement may directly contain an internal DTD, we will consider only the cases where a doctype statement refers to an external DTD, because these are the most useful

Page 4: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

• CS4400 got to here at 14:00 on 25 February 2002

Page 5: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

DOCTYPEs referring to external DTDs• A doctype statement which refers to an external DTD has

one of two formats:

<!DOCTYPE nameOfRootElement SYSTEM “fileNameAndPath” >

<!DOCTYPE nameOfRootElement PUBLIC “fpi” ”url” >

• In the first format,

<!DOCTYPE nameOfRootElement SYSTEM “fileNameAndPath” >

the keyword SYSTEM says that the external DTD is on the local machine and fileNameAndPath says where to find it

• In the second format,

<!DOCTYPE nameOfRootElement PUBLIC “fpi” ”url” >

the keyword PUBLIC says that the external DTD is publicly available on the Internet, the fpi (Formal Public

Identifier) gives its formal name and the url says where it can be found

Page 6: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Example DOCTYPEs referring to external DTDs• The doctype statement

<!DOCTYPE people SYSTEM “myFirstDTD.dtd” >

says that the external DTD is on the local machine, in a file called myFirstDTD.dtd

• The doctype statement

<!DOCTYPE people PUBLIC “-//UCC//DTD PEOPLE v1.0//EN” ”student.cs.ucc.ie/path/to/myFirstDTD.dtd” >

says that the external DTD is publicly available on the Internet, gives it a formal name which should be used by everybody who wants to refer to it, and says where it can be found

Page 7: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Formal Public Identifiers• We will be focussing on system DTDs but some of you

may be wondering about the format of FPIs like

-//UCC//DTD PEOPLE v1.0//EN

• This format is actually defined by an ISO standard and FPIs like this can be registered with organizations which are authorized by the ISO Council to act as FPI registrars

• The format is

• the - (which could also be a + ) indicates whether/not the organization name which follows is ISO-registered; UCC is not

• UCC is an OwnerID, the name of the organization which owns and maintains the document identified by the FPI

• DTD is a Public Text Class, a keyword identifying the type of doc

• PEOPLE v1.0 is a unique descriptive name for the document

• EN is the Public Text Language, in which the doc is written

Page 8: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Formal Public Identifiers (contd.)• For example, this is the formal public identifier for one

version of the HTML 4 specification:

-//W3C//DTD HTML 4.01//EN

• the W3C is not an ISO-registered organization

• the HTML4 specification is a DTD

• HTML 4.01 is a unique descriptive name

• EN says that the specification is written in English

Page 9: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XML document which claims to be valid<?xml version="1.0”?><!DOCTYPE poem SYSTEM “poem.dtd”><poem>

<title>The Daffodils</title>

<poet>William Wordsworth</poet>

<verse>

<line>I wandered lonely as a cloud</line>

<line>That floats on high o’er vales and hills</line>

<line>When all at once I saw a crowd</line>

<line>A host of golden daffodils</line>

</verse>

<verse>

<line>Something something something</line>

<line> ... </line>

</verse>

</poem>

Page 10: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Another XML document claiming to be valid<?xml version="1.0”?><!DOCTYPE people SYSTEM “personnel.dtd”><people> <man> <name> Ray Burke </name> <numberOfWives> 1 </numberOfWives> </man> <woman> <name> Margaret Thatcher </name> <age> 75 </age> </woman></people>

Page 11: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Document Type Definitions• A DTD defines the syntax rules for an application-specific

type of XML-documents.

• A DTD describes the root element that may appear in a valid document and, in doing so, also describes the kinds of children elements that the root element may possess

• A DTD must contain element type declarations, statements which have the following general format:

<!ELEMENT elementName contentModel >

• In addition, a DTD may contain a range of other types of statement, only one of which we will cover here, attribute declarations, statements which are used to define the attributes which elements may have.

Page 12: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Element Type Declarations• An element type declaration is a statements which has the

following general format:

<!ELEMENT elementName( contentModel )>

where contentModel describes the content (if any) that may or must exist between the start tag and the closing tag (if any) of the element whose name is elementName

Page 13: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Empty Elements• Empty elements are declared using the keyword EMPTY

inside the parentheses

<!ELEMENT element-name (EMPTY)>• Example:

<!ELEMENT trustworthy (EMPTY) >

which declares an element which only has a start tag and which, unless it has some attribute(s), must be written as

<trustworthy/>

Page 14: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with Character content• Elements that contain character data are declared as

follows:

<!ELEMENT element-name (#PCDATA)> where #PCDATA stands for “parsed character data”• Example:

<!ELEMENT month (#PCDATA)> This element declaration would be satisfied by the

following text in an XML file

<month>January</month>

Page 15: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with element content• Elements that contain only children elements are declared

as follows:

<!ELEMENT element-name(child-element-name)>

or

<!ELEMENT element-name

(child-name1, child-name2, ...)>

• Example

<!ELEMENT country (position)>

which specifies that a country element contains exactly one child element, a position element:

<country>

<position> ..... </position>

</country>

Page 16: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with element content (contd.)• Example

<!ELEMENT memorandum (sender,recipient,message)>

which specifies that a memorandum element contains a sender element, followed by a recipient element, followed by a message element

• When a sequence of children elements is declared, as above, the children must appear in exactly the same sequence in conforming XML documents:

<memorandum>

<sender> ..... </sender>

<recipient> ...... </recipient>

<message> ...... </message>

</memorandum>

Page 17: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Full declaration• When an element is declared to have element content, the

children element types must also be declared

• Example:

<!ELEMENT memorandum (sender,recipient,message)>

<!ELEMENT sender (#PCDATA)><!ELEMENT recipient (#PCDATA)><!ELEMENT message (#PCDATA)>

to which the following XML fragment would conform:<memorandum>

<sender>Bertie Ahern</sender>

<recipient>Ned O’Keefe</recipient>

<message>Please resign immediately</message>

</memorandum>

Page 18: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with element content (contd.)• When an element has children, the children may be

optional, may occur only once, or may be repeated• Declaring exactly one occurrence of a child:

<!ELEMENT element-name (child-name)>• For example

<!ELEMENT poem (verse)> declares that a poem element must contain exactly one verse child-element.

• Declaring one or more occurrences of a child:

<!ELEMENT element-name (child-name+)>• For example

<!ELEMENT poem (verse+)> declares that a poem element must contain one or more verse child-elements.

Page 19: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with element content (contd.)• Declaring zero or more occurrences of a child:

<!ELEMENT element-name (child-name*)>• For example

<!ELEMENT poem (author*, verse+)> declares that a poem element contains zero or more author elements, followed by one or more verse elements.

• Declaring zero or one occurrence of a child:

<!ELEMENT element-name (child-name?)>• For example

<!ELEMENT poem (author?, verse+)> declares that a poem element contains an optional author

element, followed by one or more verse elements.

Page 20: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with element content (contd.)• Parentheses can be used to group sequence of child-

elements and subject them to + * ? quantification• For example

<!ELEMENT song (author?, verse, (chorus, verse)* )>

declares that a song element contains an optional author element, followed by a verse element, followed by zero or more instances of a chorus-verse sequence.

• In other words, a song element contains an optional author element, followed by one or more verse elements, the verse elements being separated by chorus elements

Page 21: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with element content (contd.)• An element can have alternative children

<!ELEMENT element-name (option1|option2|option3)>

• For example<!ELEMENT person (male|female)>

declares that a person element contains either a male child-element or a female child-element

• It would be satisfied by either<person>

<male>Bertie Ahern</male>

</person>

or<person>

<female>Celia Larkin</female>

</person>

Page 22: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with element content (contd.)• Alternatives can also be subjected to quantification• For example

<!ELEMENT family (father? mother? (male|female)* ) >

declares that a family element contains an optional father, followed by an optional mother, followed by zero or more male or female children

Page 23: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with mixed content• Example

<!ELEMENT person(#PCDATA|name)* > declares that a person element contains either #PCDATA or name elements

• It would be satisfied by <person>Ned O’Keefe</person>

or <person><name>Bertie Ahern</name></person>

or <person><name>Bertie</name>

<name>Boss</name></person>

or <person>Bertie<name>Boss</name>

Taoiseach<name>PM</name></person>

or <person></person>

• Note that * is required in mixed content. Why? Dunno.

Page 24: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements with arbitrary content• In

<!ELEMENT element-name (ANY)>ANY means that the element may contain arbitrary content• Typically, this ANY content model is only used at the start

of developing a DTD and is replaced as the DTD is fleshed out..

Page 25: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Attribute Declarations

• The attributes for an element are declared in a statement which has the following general format:

<!ATTLIST elementName attributeDefinition,

attriouteDefinition,

... ...

attriouteDefinition >

where an attributeDefinition has the following general format

attributeName attributeType attributeDefault

• An attribute default specifies whether the attribute is required and, if not, how a program processing an XML document should behave if the attribute is absent

Page 26: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Attribute Declarations (contd.)

• Example:

<!ATTLIST person name CDATA #IMPLIED

id ID #REQUIRED

sex (male|female) “male”>

which would be satisfied by the following XML start tag

<person name=“Fred” id=“p001” sex=“male”>

or by

<person id=“p001”>

Page 27: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Attribute Declarations (contd.)

• The statement

<!ATTLIST person name CDATA #IMPLIED

id ID #REQUIRED

sex (male|female) “male”>

used three attribute types: – CDATA denoting a string-valued attribute, which can contain any

character apart from < > & ‘ “

– ID one of a set of types with predefined validity constraints,

– (male|female) an enumerated type

• The defaults in the statement specified that

– the id attribute must be provided in a <person> tag

– the name attribute is optional

– if no sex attribute is given the value male should be assumed

Page 28: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Cs 607 got here on 8 march 2005

Page 29: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Attribute Declarations (contd.)

• The ID, type token imposes the following requirements (called validity constraints) on an attribute with this type:

– an element can have only one attribute of type ID;

– the value of an ID attribute must start with a letter, underscore or colon which may be followed by a sequence containing any of these or digits

– the value of an ID attribute must uniquely identify the element which bears it -- no other element may have the same value for an ID attribute;

– an ID attribute must have a default of #REQUIRED or #IMPLIED

• Apart from ID, other types with predefined validity constraints are IDREF IDREFS ENTITY ENTITIES NMTOKEN and NMTOKENS -- their details are beyond our scope here

Page 30: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Attribute Declarations (contd.)

• Attribute defaults are

– the keyword #REQUIRED, which means that an explicit value must be given for the attribute

– the keyword #IMPLIED, which means that the attribute is optional

– one value from an enumerated attribute type, which means that this value can be assumed if no explicit value is given

– the keyword #FIXED followed by a default value, which means that instances of the attribute must match the default value

Page 31: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Other types of Declarations

• Apart from Element and Attribute declarations, DTDs can contain other types of declarations, but they are beyond our scope here

Page 32: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Example DTD• The following is a very simple example of a DTD

<!ELEMENT people (person)+>

<!ELEMENT person (female|male)>

<!ELEMENT male (#PCDATA)>

<!ELEMENT female (#PCDATA)>• It would be satisfied by the following XML document:

<?xml version="1.0" ?>

<!DOCTYPE people SYSTEM "personnel2.dtd">

<people>

<person> <female>Celia Larkin</female> </person>

<person> <male>Bertie Ahern</male> </person>

</people>

Page 33: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Another Example DTD• The following is a very simple example of a DTD

<!ELEMENT people (person)+>

<!ELEMENT person (female|male)>

<!ELEMENT male (#PCDATA)>

<!ATTLIST male age CDATA #REQUIRED>

<!ELEMENT female (#PCDATA)>

• It would be satisfied by the following XML document:<?xml version="1.0" ?>

<!DOCTYPE people SYSTEM "personnel3.dtd">

<people>

<person> <female>Celia Larkin</female> </person>

<person> <male age=“52”>Bertie Ahern</male> </person>

</people>

Page 34: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XHTML

Page 35: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XHTML• XHTML is one of the many XML-based languages that

have been defined

• XHTML is, essentially, a “cleaned-up” version of HTML 4, reformulated using XML DTD technology

– there are three XHTML DTDs, corresponding to the three versions of HTML 4 (strict, transitional and frameset)

• XHTML is designed to be compatible with XML-oriented user-agents

• XHTML is also acceptable to HTML 4-oriented user agents

• Therefore, Web developers who write their HTML documents to conform to XHTML will give a longer working-life to these documents

Page 36: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XHTML versus HTML• An XHTML document must be a well-formed XML document

and must be valid according to one of the DTDs which define the three varieties of XHTML: the Strict DTD, which should be used when rendering is controlled by

CSS<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

the Transitional DTD, to be used for browsers that cannot handle CSS <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML

1.0Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> the Frameset DTD, to be used when frames are used to divide up the

browser window: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

Page 37: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XHTML versus HTML• Since an XHTML document must be a well-formed XML

document and must be valid according to one of the DTDs,

– an XHTML document must contain one root element

• (an XML well-formedness requirement)

– the root element must be delimited by <html> and </html> tags

• (a validity requirement, since html is defined as the root element in the XHTML DTDs)

– all XHTML tags and attributes must be in lower-case

• (a validity requirement, since the XHTML DTDs define the tags and attributes as lower-case and XML is case-sensitive)

Page 38: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

CS4400 got here at 14:00 on 26 Feb 2002

Page 39: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XHTML versus HTML (contd.)– a non-empty element must have start and closing tags, for

example, every <p> tag must have a corresponding </p> tag and every <li> tag must have a corresponding </li> tag

• (a well-formedness requirement)

– the start tag for an empty element must have a final /, for example <img src=“some.jpg” />

• (a well-formedness requirement)

– elements must be properly nested• (a well-formedness requirement)

– attribute values must be quoted• (a well-formedness requirement)

Page 40: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XHTML versus HTML (contd.)– attributes must have values

• (a well-formedness requirement)

• Ill-formed example:

<input type=“checkbox” checked />

• Well-formed example:

<input type=“checkbox” checked=“checked” />

Page 41: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XHTML versus HTML (contd.)• Since style-sheets and scripts are not XML, they must be

escaped by placing them inside the special CDATA tags which XML provides for escaping non-XML text

• Example style element <style><![CDATA[ body {background-color:white;color:red} h1 {background-color:orange;color:blue} ]]>

</style> • Example script element <script language=“JavaScript” type=“text/javascript”><![CDATA[ alert(“Check-out the specimen exam paper”);]]>

</script>

Page 42: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XHTML versus HTML (contd.)• Use the id attribute instead of the name attribute

– although the name attribute is still supported in XHTML 1.0, it is expected to be eliminated in future DTDs

• One advantyage of adopting XHTML is that you can validate your documents, instead hoping that users who find them on the web will be able to view them. So use one of the following document type declarations

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

Page 43: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Cs 607 got here on 15 mar 2005

Page 44: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Rendering XML documents

Page 45: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Rendering XML documents• Unlike HTML tags, no XML tag (other than XHTML tags)

has any pre-defined rendering semantics

• However, at least four rendering possibilities exist:

– some browsers, such as MSIE 5.5, are starting to introduce very simple default rendering semantics for arbitrary XML tags

– some browsers, including MSIE 5.5, accept CSS specifications for rendering XML tags

– the most powerful approaches involve using XSL (eXtensible Stylesheet Language), a very powerful language which enables arbitrary ways of rendering XML documents

• some browsers are starting to accept XSL stylesheets

• server-side software, driven by XSL stylesheets, can transform XML documents into HTML documents before serving them to browsers which cannot understand XML+XSL

Page 46: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Default Rendering Semantics

• As said before, some browsers, such as MSIE 5.5, are starting to introduce very simple default rendering semantics for XML tags

• MSIE 5.5 renders XML documents as interactively-expandable/contractable tree structures

Page 47: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Default Rendering Semantics (contd.)• Consider the XML specification below:

<?xml version="1.0" ?>

<!DOCTYPE people SYSTEM "personnel2.dtd">

<people>

<person>

<female>Celia Larkin</female>

</person>

<person>

<male>Bertie Ahern</male>

</person>

</people>

• This is displayed by MISE 5.5 as shown on the next slide

• Notice how the start tags of elements with element content, <people> and <person> tags have a - before them– if we click on this -, MSIE will hide the children

Page 48: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the
Page 49: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Default Rendering Semantics (contd.)

• Say we click on the - before the <people> tag

• MSIE will hide all the children of this element, as shown on the next slide

• Notice, however that the - changes to a + which indicates that, if we click on it, MSIE will display the children again

Page 50: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the
Page 51: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the
Page 52: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Default Rendering Semantics (contd.)

• Say we click on the - before the first <person> tag

• MSIE will hide the child of this element, as shown on the next slide

• Notice, however that the - changes to a + which indicates that, if we click on it, MSIE will display the child again

Page 53: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the
Page 54: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Using CSS with XML documents

• Some browsers, including MSIE 5.5, accept CSS specifications for rendering XML tags

Page 55: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Using CSS with XML documents (contd.)• Consider the XML specification below:

<?xml version="1.0" ?>

<!DOCTYPE people SYSTEM "personnel2.dtd">

<?xml:stylesheet type="text/css" href="personnel2.css"?>

<people>

<person>

<female>Celia Larkin</female>

</person>

<person>

<male>Bertie Ahern</male>

</person>

</people>

• This refers to a CSS style-sheet whose contents are shown on the next slide

Page 56: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Using CSS with XML documents (contd.)• The contents of the CSS file personnel2.css are:

male {color : blue; background-color : orange}

female {color : pink; background-color : green}

• Remember that the XML content was:<people>

<person><female>Celia Larkin</female></person>

<person><male>Bertie Ahern</male></person>

</people>

• Thus Celia Larkin should appear in pink on green while Bertie Ahern should appear in blue on orange

• See next slide

Page 57: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the
Page 58: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Rendering XML documents with XSL

• The most powerful approaches to rendering XML documents involve using XSL (eXtensible Stylesheet Language)

• XSL enables arbitrary ways of rendering XML documents

– XSL enables an XML document to be translated into any arbitrary format, including, say, PDF or HTML

• Server-side software, driven by XSL stylesheets, can transform XML documents into HTML documents before serving them to browsers

• In addition, some browsers, such as MSIE 5.5, can accept XSL stylesheets

• We will consider browser-processed XSL stylesheets first and, later, consider server-side use of XSL

Page 59: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Browser-processed XSL

• Consider the XML specification below:<?xml version="1.0" ?>

<!DOCTYPE people SYSTEM "personnel2.dtd">

<?xml:stylesheet type="text/xsl" href="personnel2.xsl"?>

<people>

<person>

<female>Celia Larkin</female>

</person>

<person>

<male>Bertie Ahern</male>

</person>

</people>

• This refers to a XSL style-sheet, whose content we will examine later

Page 60: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Browser-processed XSL (contd.)• When the XML document on the previous slide is loaded into an XSL-

enabled browser, such as newer versions of MSIE, it is rendered as shown below

Page 61: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Browser-processed XSL (contd.)• The rendering on the previous slide is produced because the

XML document is transformed, by the XSL style-sheet, into the following HTML document:

<HTML>

<BODY>

<TABLE>

<THEAD>

<TR><TH>Name</TH><TH>Sex</TH></TR>

</THEAD>

<TBODY>

<TR><TD>Celia Larkin</TD><TD>female</TD</TR>

<TR><TD>Bertie Ahern</TD><TD>male</TD></TR>

</TBODY>

</TABLE>

</BODY>

</HTML>

Page 62: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Browser-processed XSL (contd.)• The XSL style-sheet which transformed the XML

document into a HTML document is specified on the following slide

• It will be discussed in detail later

Page 63: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XSL style-sheet<?xml version="1.0"?><xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform” version="1.0"> <xsl:template match="/"> <HTML><BODY><TABLE rules="all">

<THEAD><TR><TH>Name</TH><TH>Sex</TH></TR></THEAD> <TBODY> <xsl:apply-templates/> </TBODY></TABLE></BODY></HTML> </xsl:template> <xsl:template match="person"> <TR><xsl:apply-templates/></TR> </xsl:template> <xsl:template match="male"> <TD><xsl:value-of select="."/></TD><TD>male</TD> </xsl:template> <xsl:template match="female"> <TD><xsl:value-of select="."/></TD><TD>female</TD> </xsl:template></xsl:transform>

Page 64: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Server-side use of Style-sheets

• Unlike the newer versions of MSIE, most browsers are still not able to handle XSL style-sheets

• For example, the XML document we have just seen transformed to a HTML table by an XSL style-sheet in MSIE 5.5 would be rendered in Opera 5 as shown on the next slide

– Opera 5 cannot handle XSL so it uses its own default XML “rendering” which is simply to ignore the tags and output the element content as it finds it, without any attempt at formatting

Page 65: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Rendering of XML document in Opera

Page 66: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Server-side use of style-sheets (contd.)• Thus, if we are to deliver XML content to browsers like

Opera, we must use XSL on the server

– we must transform the XML content to HTML on the server, so that a non XML-enabled browser requesting an XML document actually receives an “equivalent” HTML document

• Technologies exist for doing this

– Perl provides modules for doing it, specifically XML::XSLT and the modules which this module calls, that is XML::DOM and XML::Parser

– Apache provides a technology called Cocoon for doing it

Page 67: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

A short overview of the Extensible Stylesheet Language

(XSL)

Page 68: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

• There are two main stages to rendering an XML document using XSL:

– Tranforming the source document into a new notation which has a rendering semantics

– Formatting the resultant document according to the semantics of the notation

• XSL provides two (sub-) languages for these two tasks

Page 69: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

• XSL provides a (sub-)language called XSL Transformations (XSLT)

for tranforming a source XML document into a new notation which has a rendering semantics

• XSL provides a (sub-)language with rendering semantics called

XSL Formatting Objects (XSL FO)

Page 70: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Browser-side usage of XSL• In browser-side usage of XSL, we simply use the

XSLT part of XSL to transform XML into HTML

• This HTML is then rendered by the browser

Page 71: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Trees in XSLT• In XSLT, the source and result document are viewed as

trees– the types of nodes include element nodes, attribute nodes and text

nodes.

• A stylesheet written in XSLT consists of a set of a set of template rules.

• A template rule has two parts: – a pattern which is matched against nodes in the source tree and – a template which can be instantiated to form part of the result tree.

Page 72: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XSLT Stylesheets• An XSLT stylesheet is itself an XML document• The root element is of type xsl:stylesheet but xsl:transform may be used as a synonym for xsl:stylesheet

• Thus, an XSLT stylesheet may look like this<?xml version="1.0"?><xsl:stylesheet … >…</xsl:stylesheet>

• Or like this:<?xml version="1.0"?><xsl:transform … >…</xsl:transform>

Page 73: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XSLT Stylesheets• Two attributes are required for the root element:

version – at present the correct value is “1.0”xmlns:xsl – different XSLT processessing software

require different values of this.

• Different MSIE browsers contain different XSLT processors, so you may need to try these two different values for the xmlns:xsl attribute:

"http://www.w3.org/1999/XSL/Transform” "http://www.w3.org/TR/WD-xsl"

Page 74: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

• Thus, for some MSIE browsers, an XSLT stylesheet may have to look like this<?xml version="1.0"?>

<xsl:transform version=“1.0”

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

</xsl:transform>

• For other MSIE browsers, it may have to look like this:<?xml version="1.0"?>

<xsl:stylesheet version=“1.0”

xmlns:xsl= "http://www.w3.org/TR/WD-xsl” >

</xsl:stylesheet>

Page 75: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Elements within the root element• The root element (xsl:stylesheet or xsl:transform) may contain a great many different types of elements

• However, the most commonly-used element is the xsl:template element– Each xsl:template element is used to specify a

template rule for transforming one kind of node in the tree for the source XML document

Page 76: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Fragment from an XSLT style-sheet• The following shows the root node and first-level child

nodes of an XSLT stylesheet

<?xml version="1.0"?>

<xsl:transform version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” >

<xsl:template> … </xsl:template>

<xsl:template> … </xsl:template>

<xsl:template> … </xsl:template>

<xsl:template> … </xsl:template>

</xsl:transform>

Page 77: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

xsl:template elements• An xsl:template element is used to specify a

template rule• Each xsl:template element has a match

attribute which is used to specify a type of node in the source tree

• The content of an xsl:template element specifies the corresponding structure in the result tree

Page 78: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

The match attribute in xsl:template elements• The value of a match attribute is a pattern which

specifies the type of source node to which the template rule applies

• A pattern may just be the name of a certain type of element in the source document

• For example, the following template can be used to transform any person element

<xsl:template match=“person”> … </xsl:template>

• However, certain meta-characters may also be used in match patterns

Page 79: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Meta-characters in match patterns

• Meta-characters that may be used in match patterns include: / * | //

• Here are some examples of patterns:– The pattern / matches the root node

– The pattern * matches any element

– The pattern male|female matches any male element and any female element

– The pattern poem/verse matches any verse element with a poem parent

– The pattern poem//line matches any line element with a poem element as an ancestor

Page 80: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

The xsl:template element (contd.)• Thus, here are some example xsl:template elements:

<xsl:template match=“/”> … </xsl:template>

<xsl:template match=“*”> … </xsl:template>

<xsl:template match=“person”> … </xsl:template>

<xsl:template match=“male”> … </xsl:template>

<xsl:template match=“male|female”> … </xsl:template>

<xsl:template match=“person/male”> … </xsl:template>

<xsl:template match=“person//age”> … </xsl:template>

Page 81: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Fragment from an XSLT style-sheet• The following shows the top-most levels of the XSLT

stylesheet for processing our XML document about Bertie and Celia<?xml version="1.0"?>

<xsl:transform version=“1.0”

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

<xsl:template match="/"> … </xsl:template>

</xsl:transform>

• The template matches the root of the source document– The content of the template will specify how to generate the

corresponding result document

Page 82: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Content of xsl:template elements

• The content of an xsl:template element specifies how to generate the result text that corresponds to the source node

• This content may simply specify some canned text• Or the xsl:template element may contain

other XSLT elements (called instruction elements) which specify certain kinds of processing that should be performed on the source node in order to compute the appropriate result text

• Or the xsl:template element may contain a mixture of canned text and XSLT processing elements

Page 83: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Canned text in an XSLT stylesheet • The stylesheet below transforms the entire source XML

document (below-left) to some canned HTML text (below-right)

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/">

<html><body> <h1>People</h1>

<p>This document contains information about some people.</p></body></html>

</xsl:template>

</xsl:stylesheet>

Page 84: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Canned HTML text must be well-formed XML • The stylesheet below is not well-formed XML

because there is no closing tag for the <p> tag– An error message is produced by the MS XML parser

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html><body> <h1>People</h1> <p>This document contains information about some people.</body></html> </xsl:template></xsl:stylesheet>

Page 85: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XSLT instruction elements• We have seen that a template can contain non-XSLT text,

canned text that it inserts into the result tree

• A template can also contain XSLT instruction elements– when the template is instantiated, each instruction is executed and

the text fragment that it creates is placed in the result tree

• Instructions can select and process descendant source elements.

• Processing a descendant element creates a result tree fragment by finding the applicable template rule and instantiating its template.

Page 86: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

An XSLT instruction element

• The instruction <xsl:apply-templates/>

recursively processes the children of the source element.

• It does this by applying the template rules that match those children

Page 87: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

The xsl:apply-templates instruction • The stylesheet below contains an example application

of this instruction:<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform“ version="1.0">

<xsl:template match="/“ > <xsl:apply-templates /> </xsl:template>

<xsl:template match="people“ > <p>I found some people.</p> </xsl:template>

</xsl:stylesheet>

Page 88: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

xsl:apply-templates instruction (contd.) • The stylesheet below contains another application of this

instruction• Notice that, by recursion, it finds the male and female

elements<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform“ version="1.0"> <xsl:template match="/“ > <xsl:apply-templates /> </xsl:template><xsl:template match=“male“ > <p>I found a man.</p> </xsl:template><xsl:template match=“female“ > <p>I found a woman.</p> </xsl:template></xsl:stylesheet>

Page 89: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

xsl:apply-templates instruction (contd.) • In this third example, the person elements are found

– but their children (the male and female elements) are not processed, even thought there are templates for them

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform“ version="1.0"> <xsl:template match="/“ > <xsl:apply-templates /> </xsl:template><xsl:template match=“person“ > <p>I found a person.</p> </xsl:template><xsl:template match=“male“ > <p>I found a man.</p> </xsl:template><xsl:template match=“female“ > <p>I found a woman.</p> </xsl:template></xsl:stylesheet>

Page 90: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

xsl:apply-templates instruction (contd.) • In this fourth example, the person elements are found

– their children (the male and female elements) are also processed– because the template for person elements uses the xsl:apply-templates instruction to process the children of person elements

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform“ version="1.0"><xsl:template match="/“ > <xsl:apply-templates /> </xsl:template><xsl:template match=“person“ > <p>I found a person. It was <xsl:apply-templates/>.</p>

</xsl:template><xsl:template match=“male“ >a man</xsl:template><xsl:template match=“female“ >a woman</xsl:template></xsl:stylesheet>

Page 91: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

the select attribute • The xsl:apply-templates instruction has a select

attribute which can be used to limit its application– Below, only male children of person elements are processed, even

though there is a template for female elements<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform“ version="1.0"><xsl:template match="/“ > <xsl:apply-templates /> </xsl:template><xsl:template match=“person“ > <p>I found a person. <xsl:apply-templates select=“male”/></p>

</xsl:template><xsl:template match=“male“ >It was a man.</xsl:template><xsl:template match=“female“ >It was a woman.</xsl:template></xsl:stylesheet>

Page 92: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

the select attribute (contd.) • The select attribute can have complicated values

– Below, only male descendents of people elements are processed, even though there is a template for female elements

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform“ version="1.0">

<xsl:template match="/"> <xsl:apply-templates select="people//male” /> </xsl:template>

<xsl:template match="male"><p>I found a man.</p></xsl:template>

<xsl:template match="female"><p>I found a woman.</p></xsl:template>

</xsl:stylesheet>

Page 93: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

the xsl:value-of instruction • This can be used to generate result text by extracting data from

the source tree. – Its required select attribute species the data to be extracted– In its value patterns, the character . means “the current node”

<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform“ version="1.0"><xsl:template match="/"><xsl:apply-templates/> </xsl:template><xsl:template match="female"> <p>I found a woman. Her name is <xsl:value-of select="."/>.</p></xsl:template> <xsl:template match="male"> <p>I found a man. His name is <xsl:value-of select="."/>.</p></xsl:template></xsl:stylesheet>

Page 94: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

the xsl:value-of instruction (contd.) • This can also extract attribute values from the source tree

– In its value patterns, the character @ prefixes an attribute name<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform“ version="1.0"><xsl:template match="/"><xsl:apply-templates/> </xsl:template><xsl:template match="female"> <p>I found a woman. Her name is <xsl:value-of select="."/> and her age is <xsl:value-of select="@age"/.</p></xsl:template> <xsl:template match="male"> <p>I found a man. His name is <xsl:value-of select="."/> and his age is <xsl:value-of select="@age"/>.</p> </xsl:template></xsl:stylesheet>

Page 95: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Back to the table example • We can now return to considering in detail the first

XSLT style-sheet we saw– The one which, when applied to the XML document below-

left produces the HTML document below-right

– It is repeated on the next slide and then considered in detail

Page 96: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

The XSL style-sheet for the TABLE example<?xml version="1.0"?><xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform” version="1.0"> <xsl:template match="/"> <HTML><BODY><TABLE rules="all">

<THEAD><TR><TH>Name</TH><TH>Sex</TH></TR></THEAD> <TBODY> <xsl:apply-templates/> </TBODY></TABLE></BODY></HTML> </xsl:template> <xsl:template match="person"> <TR><xsl:apply-templates/></TR> </xsl:template> <xsl:template match="male"> <TD><xsl:value-of select="."/></TD><TD>male</TD> </xsl:template> <xsl:template match="female"> <TD><xsl:value-of select="."/></TD><TD>female</TD> </xsl:template></xsl:transform>

Page 97: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

The table example (contd.)

• In this case, our style-sheet consists of four templates:– one template, whose match attribute has the value “/”, is

satisfied when it finds the root element of the XML document. It then produces the main skeleton of the resultant HTML document and calls other templates to transform its children elements.

– The template whose match attribute has the value “person” is satisfied when it finds a person element. It produces a <TR> and </TR> tag-pair, in-between which it calls templates to transform its children elements

Page 98: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

The table example (contd.)

– The template whose match attribute has the value “male” is satisfied when it finds a male element. It produces a <TD> tag, followed by the content of the male element, followed by the following sequence of HTML: </TD> <TD>male</TD>

– The template whose match attribute has the value “female” is satisfied when it finds a female element. It produces a <TD> tag, followed by the content of the female element, followed by the following sequence of HTML: </TD> <TD>female</TD>

Page 99: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Another Example• Consider the following XML document<?xml version="1.0" ?><?xml:stylesheet type="text/xsl" href="bertie3.xsl"?><people>

<person><sex>female</sex><age>21</age> <name>Julia Larkin</name>

</person><person>

<sex>male</sex><age>22</age> <name>Bertie Ahern</name>

</person></people>

Page 100: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Another example (contd.)• Suppose we wanted to render it as in the image

below

Page 101: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Another example (contd.)

• Here is the stylesheet:<?xml version="1.0"?><xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/"> <html><body> <h1>Tabular Personnel Descriptions</h1> <p>Here are some tables, one for each person.</p> <xsl:apply-templates/></body></html> </xsl:template><xsl:template match="person"> <table rules="all"> <caption> Details of <xsl:value-of select="./name"/> </caption> <thead><tr><th>Name</th><th>Age</th><th>Sex</th></tr></thead> <tbody><tr><td><xsl:value-of select="./name"/></td> <td><xsl:value-of select="./age"/></td> <td><xsl:value-of select="./sex"/></td></tr></tbody> </table> </xsl:template></xsl:transform>

Page 102: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Another example (contd.)• Suppose we wanted also wanted to embed a CSS

style-sheet in the XSLT style-sheet, as below

Page 103: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

Another example (contd.)

• Here is the XSLT stylesheet:<?xml version="1.0"?><xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/"> <html><head> <style> <![CDATA[ table {background-color:red} ]]> </style> </head> <body> <h1>Tabular Personnel Descriptions</h1> <p>Here are some tables, one for each person.</p> <xsl:apply-templates/></body></html> </xsl:template><xsl:template match="person"> <table rules="all"> <caption> Details of <xsl:value-of select="./name"/> </caption> <thead><tr><th>Name</th><th>Age</th><th>Sex</th></tr></thead> <tbody><tr><td><xsl:value-of select="./name"/></td> <td><xsl:value-of select="./age"/></td> <td><xsl:value-of select="./sex"/></td></tr></tbody> </table> </xsl:template></xsl:transform>

Page 104: Valid XML documents To be valid, an XML document –must be well-formed according to the general XML syntax rules –and, in addition, it must satisfy the

XSLT in full

• The full richness of XSLT is beyond the scope of the time available in this course

• However, you may wish to read the W3C specification and experiment

• The latest version of the XSLT specification can be found at: http://www.w3.org/TR/xslt