90 likes | 203 Views
Using XSL to Generate XHTML Documents. Roger L. Costello XML Technologies. 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
E N D
Using XSL to Generate XHTML Documents Roger L. Costello XML Technologies
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
HTML: <br> XHTML: <br class="empty"/> 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 HTML element. The class attribute can have any value. So, we use the class attribute simply to get the browser to "see" this element as a break element.
HTML: <table> … </TABLE> XHTML: <table> … </table> XHTML specifies that all tags be in lower case.
HTML: <body bgcolor=lightgrey> XHTML: <body bgcolor="lightgrey"> Quote the attribute value!
HTML: <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"> XHTML: 1. Add a DOCTYPE declaration with a PUBLIC identifier 2. Add a namespace declaration to the root element.
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.
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 declaration will generate an identical default namespace declaration in the output These instruct the XSL Processor to create a DOCTYPE declaration in the output, with a PUBLIC identifier as well as a SYSTEM identifier. (see html-example25)
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 declaration 2. There is a DOCTYPE declaration containing a PUBLIC identifier as well as a SYSTEM identifier 3. There is a default namespace declaration 4. We can validate this XHTML document