Sams Teach Yourself XML in 21 Days
-
Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- Day 2. Creating XML Documents
- Day 3. Creating Well-Formed XML Documents
- Day 4. Creating Valid XML Documents: DTDs
- Declaring Attributes in DTDs
- Day 6. Creating Valid XML Documents: XML Schemas
- Day 7. Creating Types in XML Schemas
- Part I. In Review
- Day 8. Formatting XML by Using Cascading Style Sheets
- Day 9. Formatting XML by Using XSLT
- Day 10. Working with XSL Formatting Objects
- Part II. In Review
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
-
Day 12. Putting XHTML to Work
- Creating Hyperlinks: <a>
- Linking to Other Documents: <link>
- Handling Images: <img>
- Creating Frame Documents: <frameset>
- Creating Frames: <frame>
- Creating Embedded Style Sheets: <style>
- Formatting Tables: <table>
- Creating Table Rows: <tr>
- Formatting Table Headers: <th>
- Formatting Table Data: <td>
- Extending XHTML
- Summary
- Q&A
- Workshop
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Day 14. Handling XLinks, XPointers, and XForms
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Day 16. Using Java and .NET: DOM
- Day 17. Using Java and .NET: SAX
- Day 18. Working with SOAP and RDF
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Day 20. Working with XML and Databases
- Day 21. Handling XML in .NET
- Part V. In Review
- Appendix A. Quiz Answers
Extending XHTML
The name Extensible Hypertext Markup Language might give you the impression that XHTML is designed to be extended. That's true. However, although you can technically extend XHTML, HTML browsers won't understand what you're doing. Let's take a look at an example.
In this example, you'll extend XHTML by adding to it a new element, <bold>, that will display its text in bold. Here's how you might declare this element in a DTD:
<!ELEMENT bold (#PCDATA)>
You also need the rest of the XHTML 1.0 Transitional DTD, so you start by creating a new parameter entity, which you can call XHTML1.0DTDEntity:
<!ELEMENT bold (#PCDATA)> <!ENTITY % XHTML1.0DTDEntity PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Now to include the entire XHTML 1.0 Transitional DTD in the new DTD, you just use a reference to this parameter entity. Listing 12.11 shows how this works.
Example 12.11. Extending the XHTML 1.0 Transitional DTD (ch12_11.dtd)
<!ELEMENT bold (#PCDATA)> <!ATTLIST bold boldattribute CDATA #IMPLIED > <!ENTITY % XHTML1.0DTDEntity PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> %XHTML1.0DTDEntity;
Now you have a new DTD that supports not only the <bold> element, but also the rest of XHTML 1.0 Transitional. So you've extended XHTML. You can see this new DTD at work in Listing 12.12.
Example 12.12. Extending XHTML 1.0 Transitional as HTML (ch12_12.html)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html SYSTEM "ch12_11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Extending XHTML
</title>
<link rel="stylesheet" href="ch12_13.css" />
</head>
<body>
<p>
This version of XHTML can make text <bold>bold</bold>.
</p>
</body>
</html>
To style the new <bold> element, you can use a CSS, such as the one in Listing 12.13.
Example 12.13. A Style Sheet for Extending XHTML 1.0 Transitional (ch12_13.css)
bold {font-weight: bold}
However, neither Internet Explorer nor Netscape Navigator will understand what to do with your extended XHTML. The reason for this is that you've given the XHTML document, ch12_12.html, the extension .html, which means that browsers will assume the fixed HTML element set and not even try to read in the DTD.
You can do a better job in this case if you treat ch12_12.html as XML instead of HTML. Listing 12.14 shows how that might work. In this version of the document, you're using an internal DTD (so as not to confuse Internet Explorer, which can't handle the DTD here as an external one) and the <?xml-stylesheet?> XML processing instruction to include a new version of the style sheet, ch12_15.css (to make sure the text in the <title> element will also be formatted for display), which is shown in Listing 12.15.
Example 12.14. Extending XHTML 1.0 Transitional as XML (ch12_14.xml)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="ch12_15.css"?>
<!DOCTYPE html [
<!ELEMENT bold (#PCDATA)>
<!ENTITY % XHTML1.0DTDEntity PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
%XHTML1.0DTDEntity;
]>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Extending XHTML
</title>
</head>
<body>
<p>
This version of XHTML can make text <bold>bold</bold>.
</p>
</body>
</html>
Example 12.15. A Style Sheet Used to Extend XHTML 1.0 Transitional as XML (ch12_15.css)
bold {font-weight: bold}
title {display:block; font-size: 24pt}
Figure 12.7 shows the results of Listings 12.14 and 12.15. in Internet Explorer. Internet Explorer indeed downloads the XHTML 1.0 Transitional DTD, extends it with the new <bold> element, and produces the results shown in Figure 12.7.
Figure 12.7 Extending XHTML.
By treating the markup as XML, you are able to extend XHTML. That's not too bad because the XHTML DTDs still contain all the XHTML rules, such as which elements can contain which other elements and which attributes are required. The drawback is that you need to define from scratch all the formatting you want to use; you do this by defining your own style sheets or using someone else's.
Summary | Next Section