xml and xslt

39
XML and XSL Institutional Web Management 2001: Organising Chaos

Upload: andrew-savory

Post on 19-May-2015

1.783 views

Category:

Technology


1 download

DESCRIPTION

This workshop introduced the power of XML and XSLT to delegates. It used an innovative solution of Apache Cocoon on a single server and form-based file upload to allow delegates to quickly and simply see the effect of applying XSL transformations on their markup.

TRANSCRIPT

Page 1: XML and XSLT

XML and XSL

Institutional Web Management 2001:Organising Chaos

Page 2: XML and XSLT

Running order

Background All about XML All about XSL Hands-on XML and XSL Publishing engines Example applications Crystal balls

Page 3: XML and XSLT

Background

Mark Ray and Andrew Savory Using XML since 1996 Used a variety of tools and methods

Page 4: XML and XSLT

XML and XSL

W3C specifications Separation of Concerns

Reuse of data Multiple output formats Styles tailored to reader / Standardised styles

Page 5: XML and XSLT

Applications of XML and XSL

Static information Institutional web sites Prospectuses Course catalogues

Dynamic information News or event information services Course catalogues Staff directories

Page 6: XML and XSLT

Benefits of XML and XSL

Standards-based, format-independent Serve sites and formats based on user need

Text-based (text readers, slow links, low-tech) Netscape / Internet Explorer specifc TV, Phone, PDA PDF, SVG, VRML, ...

Simplification of web site management...

Page 7: XML and XSLT

The management bit

Management Decide what the site

should contain, how it should behave and how it should appear

Content Responsible for

writing, owning, managing the site content

Page 8: XML and XSLT

The management bit

Logic Responsible for

integration with dynamic content generation

Style Responsible for

information presentation, look & feel, site graphics and maintenance

Page 9: XML and XSLT

What is XML?

eXtensible Markup Language Not a fixed format 'Metalanguage' For describing information

Page 10: XML and XSLT

XML Design Goals

1 XML shall be straightforwardly usable over the Internet.

2 XML shall support a wide variety of applications.

3 XML shall be compatible with SGML.

4 It shall be easy to write programs which process XML documents.

5 The number of optional features in XML is to be kept to the absolute minimum, ideally zero.

Page 11: XML and XSLT

XML Design Goals

6 XML documents should be human-legible and reasonably clear.

7 The XML design should be prepared quickly.

8 The design of XML shall be formal and concise.

9 XML documents shall be easy to create.

10Terseness is of minimal importance.

Page 12: XML and XSLT

Structure of an XML document

<?xml version="1.0"?>

<PARENT>

<CHILD>

This is content.

</CHILD>

<EMPTY/>

</PARENT>

Page 13: XML and XSLT

A first XML document

Construct a well-formed XML document using the following tags: xml opening tag page title content para

Don't forget to add the closing tags!

Page 14: XML and XSLT

Differences to HTML

Order:<tag1><tag2></tag1></tag2> WRONG

<tag1><tag2></tag2></tag1> RIGHT

Balance<tag1><tag2></tag2> WRONG

<tag1><tag2></tag2></tag1> RIGHT

Case<tag1><tag2></TAG2></TAG1> WRONG

<tag1><tag2></tag2></tag1> RIGHT

Page 15: XML and XSLT

Differences to HTML

Attributes<tag1 class=wrong> WRONG

<tag1 class="right"> RIGHT

Empty Tags<tag1 class="wrong"> WRONG

<tag1 class="wrong" /> RIGHT

XML is stricter

...and therefore better!

Page 16: XML and XSLT

Well-formed vs. Valid

Exactly what they say: Well-formed means it's written correctly Valid means we can validate it

Page 17: XML and XSLT

A well-formed example

<?xml version="1.0"?>

<PARENT><CHILD>

<MARK NUMBER="1" LISTED="yes" TYPE="natural"/>

<NAME>

<LASTNAME>child</LASTNAME>

<FIRSTNAME>second</FIRSTNAME>

</NAME>

</CHILD>

</PARENT>

Page 18: XML and XSLT

A valid example

<?xml version="1.0"?><!DOCTYPE PARENT [

<!ELEMENT PARENT (CHILD*)><!ELEMENT CHILD (MARK?,NAME+)><!ELEMENT MARK EMPTY><!ELEMENT NAME (LASTNAME+,FIRSTNAME+)*><!ELEMENT LASTNAME (#PCDATA)><!ELEMENT FIRSTNAME (#PCDATA)><!ATTLIST MARK

NUMBER ID #REQUIREDLISTED CDATA #FIXED "yes"TYPE (natural|adopted) "natural">

<!ENTITY STATEMENT "This is well-formed XML">]><PARENT>

&STATEMENT;<CHILD>

<MARK NUMBER="1" LISTED="yes" TYPE="natural"/> <NAME>

<LASTNAME>child</LASTNAME><FIRSTNAME>second</FIRSTNAME>

</NAME></CHILD>

</PARENT>

Page 19: XML and XSLT

Document Type Definitions

First standard mechanism for XML validation Define the role and structure of XML

elements Sufficient for simple XML schemas Don't support namespaces Use non-XML syntax

Page 20: XML and XSLT

XSchema

XML structural definition language of choice Defines a class of XML document Supports name spaces More powerful

Page 21: XML and XSLT

Xschema example

<?xml version="1.0"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:annotation> <xsd:documentation xml:lang="en"> Example Schema for IWM workshop </xsd:documentation> </xsd:annotation>

<xsd:element name="page" type="page"/>

<xsd:complexType name="page"> <xsd:sequence> <xsd:element name="title" type="xsd:string"/> <xsd:element name="content" type="pageContent"/> </xsd:sequence> </xsd:complexType>

<xsd:complexType name="pageContent"> <xsd:sequence> <xsd:element name="para" type="xsd:string" minOccurs="0"/> </xsd:sequence> </xsd:complexType>

</xsd:schema>

Page 22: XML and XSLT

What is XSL?

Preferred style sheet language of XML a method for transforming XML documents a method for defining XML parts and patterns a method for formatting XML documents

An application of XML (same formatting rules)

Page 23: XML and XSLT

Hands-on XML

Create the following XML document:

<?xml version="1.0"?>

<page>

<title>Hello</title><content><para>This is my first XML page!</para></content>

</page>

Save it as hello_world.xml

Page 24: XML and XSLT

Uploading the file

Navigate to the site provided Click on "upload.html" Beside the "file to upload" box, click on

"choose" Select the file you want to upload Click on "upload"

Page 25: XML and XSLT

Viewing the file

If you see the file you wanted to upload and receive a "File written successfully" message...

Click on "Content", then the name of the file

Page 26: XML and XSLT

Structure of an XSL stylesheet

Most templates have the following form:<xsl:template match="emphasis">

<i><xsl:apply-templates/></i>

</xsl:template>

The whole <xsl:template> element is a template

The match pattern determines where this template applies

Page 27: XML and XSLT

Structure of an XSL stylesheet

Literal result element(s) come from non-XSL namespace(s)

XSLT elements come from the XSL namespace

Page 28: XML and XSLT

Hands-on XSL

Create the following XSL stylesheet:<?xml version="1.0"?>

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

<xsl:template match="page"><html>

<head><title><xsl:value-of select="title"/></title>

</head><body bgcolor="white" alink="red" link="blue" vlink="blue">

<xsl:apply-templates/></body>

</html></xsl:template>

</xsl:stylesheet>

Page 29: XML and XSLT

Uploading the file

Navigate to the site provided Click on "upload.html" Beside the "file to upload" box, click on

"choose" Select the file you want to upload Click on "upload"

Page 30: XML and XSLT

Publishing engines

Cocoon Java-based

AxKit Perl-based

4Suite Python

Others or roll your own

Page 31: XML and XSLT

Viewing the file

If you see the file you wanted to upload and receive a "File written successfully" message...

Click on "View site in cocoon" Select the file you uploaded and the

stylesheet you want to view it in

Page 32: XML and XSLT

Hands-on XSL

Add the following to the XSL stylesheet:

<xsl:template match="title"><h2 style="color: navy; text-align: center">

<xsl:apply-templates/></h2>

</xsl:template>

<xsl:template match="para"><p align="left">

<i><xsl:apply-templates/></i></p>

</xsl:template>

Page 33: XML and XSLT

New platforms, new beginnings?

The old way of publishing Repurposing

The new way of publishing Target multiple platforms Multiple stylesheets Multiple formats

Page 34: XML and XSLT

Browser-specific content

<map:select>

<map:when test="lynx">

<map:transform src="stylesheets/simple-page2html-lynx.xsl"/>

</map:when>

<map:otherwise>

<map:transform src="stylesheets/simple-page2html.xsl"/>

</map:otherwise>

</map:select>

Page 35: XML and XSLT

Lynx-friendly XSL stylesheet

Add the following to your XSL stylesheet:

<h2>This site is lynx-friendly</h2>

Save the stylesheet as

simple-page2html-lynx.xsl

Page 36: XML and XSLT

Uploading the file

Navigate to the site provided Click on "upload.html" Beside the "file to upload" box, click on

"choose" Select the file you want to upload Click on "upload"

Page 37: XML and XSLT

Viewing the file

If you see the file you wanted to upload and receive a "File written successfully" message...

Click on "View site in cocoon" Select the file you uploaded and the

stylesheet you want to view it in

Page 38: XML and XSLT

Where next?

The semantic web