Upload
ella-robertson
View
216
Download
2
Embed Size (px)
Citation preview
1The MITRE Corporation @JICPAC
Using XSL to Generate XHTML Documents
Roger L. Costello
XML Technologies
2The MITRE Corporation @JICPAC
What is XHTML?
• With HTML you can – omit end tags, – use upper case for the start tag and then lower case
for the end tag (i.e., HTML if case insensitive– leave off the quotes around attribute values
• Clearly, these all violate XML• XHTML is HTML that follows all the rules of
XML
3The MITRE Corporation @JICPAC
<br>
<br class="empty"/>
HTML:
XHTML:
Most browsers today do not understand empty elements,e.g., <br/>. Consequently, we must "trick" the browser.You can put a class attribute on just about any HTMLelement. The class attribute can have any value. So, weuse the class attribute simply to get the browser to "see"this element as a break element.
4The MITRE Corporation @JICPAC
<table> … </TABLE>
<table> … </table>
HTML:
XHTML:
XHTML specifies that all tags be in lower case.
5The MITRE Corporation @JICPAC
<body bgcolor=lightgrey>
<body bgcolor="lightgrey">
HTML:
XHTML:
Quote the attribute value!
6The MITRE Corporation @JICPAC
<html>
<!DOCTYPE html PUBLIC "-//W3//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
HTML:
XHTML:
1. Add a DOCTYPE declaration with a PUBLIC identifier2. Add a namespace declaration to the root element.
7The MITRE Corporation @JICPAC
Advantages of formatting your HTML as XHTML
• Ever notice how some web sites are broken when viewed in Netscape, but work when viewed in IE (or vice-versa)?
– That's oftentimes because the HTML has missing end tags and one browser is able to "fix it" while the other isn't
• If it were written in XHTML then there would never be a problem with missing end tags
• Many web masters will check their HTML by testing it against as many browsers as they can
– There are many, many browsers, and many versions of browsers, so exhaustive testing is very difficult
• If it were written in XHTML then it could be tested simply by validating against the XHTML DTD
• Due to the relative randomness of HTML it is very difficult for applications such as screen scrapers to make use of it.
– With XHTML the document is much more strictly organized and hence it is easier to parse and make use of it.
8The MITRE Corporation @JICPAC
Generating XHTML with your stylesheet
<?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml" version="1.0"> <xsl:output method="html" doctype-public="-//W3//DTD XHTML 1.0 Transitional//EN" doctype-system=" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/"> <html> <head> <title>Welcome</title> </head> <body> Welcome! </body> </html> </xsl:template>
</xsl:stylesheet>
This default namespace declarationwill generate an identical defaultnamespace declaration in the output
These instruct the XSL Processor tocreate a DOCTYPE declaration inthe output, with a PUBLIC identifieras well as a SYSTEM identifier.
(see html-example25)
9The MITRE Corporation @JICPAC
Here's what will be generated
<!DOCTYPE html PUBLIC "-//W3//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Welcome</title> </head> <body> Welcome! </body></html>
Notes:1. There is no XML declaration2. There is a DOCTYPE declaration containing a PUBLIC identifier as well as a SYSTEM identifier3. There is a default namespace declaration4. We can validate this XHTML document