xhtml (2) – advanced
General Terms
Example
Example: body {font-size:12pt; color:red}
Document =
Element = body { }
Attributes = font-size, color
Values = 12pt, red
Attribute: An attribute is a parameter to an element declared in the DTD. An attribute's type and value range, including a possible default value, are defined in the DTD.
DTD: A DTD, or document type definition, is a collection of XML markup declarations that, as a collection, defines the legal structure, elements, and attributes that are available for use in a document that complies to the DTD.
Document: A document is a stream of data that, after being combined with any other streams it references, is structured such that it holds information contained within elements that are organized as defined in the associated DTD.
Element: An element is a document structuring unit declared in the DTD. The element's content model is defined in the DTD, and additional semantics may be defined in the prose description of the element. Example: body{ }
Facilities: Facilities are elements, attributes, and the semantics associated with those elements and attributes.
Implementation: See User Agent.
Parsing: Parsing is the act whereby a document is scanned, and the information contained within the document is filtered into the context of the elements in which the information is structured.
Rendering: Rendering is the act whereby the information in a document is presented. This presentation is done in the form most appropriate to the environment (e.g. aurally, visually, in print).
User Agent: A user agent is a system that processes XHTML documents in accordance with this specification. Example: a browser (Firefox, IE, Opera)
Validation: Validation is a process whereby documents are verified against the associated DTD, ensuring that the structure, use of elements, and use of attributes are consistent with the definitions in the DTD.
Well-formed: A document is well-formed when it is structured according to the rules defined in Section 2.1 of the XML 1.0 Recommendation [XML]. (If it is validated, it is well-formed.)
Normative Definition of XHTML 1.0
This version of XHTML provides a definition of strictly conforming XHTML 1.0 documents, which are restricted to elements and attributes from the XML and XHTML 1.0 namespaces.
A Strictly Conforming XHTML Document is an XML document that requires only the facilities described as mandatory in this specification.
Such a document must meet all of the following criteria:
- It must conform to the constraints expressed in one of the three DTDs found in DTDs and in Appendix B.
- The root element of the document must be html.
- The root element of the document must contain an xmlns declaration for the XHTML namespace [XMLNS]. The namespace for XHTML is defined to be
http://www.w3.org/1999/xhtml.
An example root element might look like:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- There must be a DOCTYPE declaration in the document prior to the root element. The public identifier included in the DOCTYPE declaration must reference one of the three DTDs found in DTDs using the respective Formal Public Identifier. The system identifier may be changed to reflect local system conventions:
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
- The DTD subset must not be used to override any parameter entities in the DTD.
An XML declaration is NOT required in an XML document:
- IF encoding was determined by a higher-level protocol (HTTP header).
- OR IF encoding of the document is in the default UTF-8 or UTF-16.
Here is an example of an XHTML document. In this utf-8 example, the XML declaration is optional but has been included.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Virtual Library</title>
</head>
<body>
<p>Moved to <a href="http://example.org/">example.org</a>.</p>
</body>
</html>
The first line in the above, <?xml version="1.0" encoding="UTF-8"?>, caused problems with browsers so I dropped it, relying on a meta tag between the head tags, instead:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
Using XHTML with an other namespace HTML4
Due to the fact that XHTML is an XML application, certain practices that were perfectly legal in SGML-based HTML 4 must be changed.
Documents must be well-formed
XML introduced Well-formedness:
- all elements must either have closing tags or be written in a special form
- all the elements must nest properly.
Although overlapping of elements is illegal in SGML, it is widely tolerated in existing browsers.
CORRECT: nested elements: <p>here is an emphasized <em>paragraph</em>.</p>
INCORRECT: overlapping elements: <p>here is an emphasized <em>paragraph.</p></em>
Element and attribute names must be in lower case
XHTML documents must use lower case for all HTML element and attribute names. This difference is necessary because XML is case-sensitive e.g. <li> and <LI> are different tags.
For non-empty elements, end tags are required
Ordinarily an opening and a closing tag contain some text between, as in:
<p>This is a paragraph.</p>
Such opening tags must have closing tags, like </p>.
But some opening tags do not need closing tags, such as:
<img src="wot.jpg" alt="Wot" /> This image tag pulls in an image to be displayed at this point on the page.
<hr /> This tag draws a line.
<br />This causes the text line to break.
<link rel="stylesheet" href="K2.css" type="text/css" /> This goes into the head of the xhtml file to bring in another file.
All these tags stand alone without a closing tag. They are called "empty tags". XML requires that empty tags must end "space+/+>".
Attribute values must always be quoted
All attribute values must be quoted, even those which appear to be numeric.
CORRECT: quoted attribute values
<td rowspan="3">
INCORRECT: unquoted attribute values
<td rowspan=3>
Dimensional values should include the units of meazurement, such as font-size="12pt", width="600px", padding="2em". Do not let the browsers choose for you. I prefer to use em (determined by font-size) instead of px (pixels, determined by the dpi setting of monitor).
Attribute Minimization
XML does not support attribute minimization. Attribute-value pairs must be written in full. Attribute names such as compact and checked cannot occur in elements without their value being specified, as in <dl compact="compact">, NOT <dl compact>
White Space handling in attribute values
When user agents process attributes, they strip leading and trailing white space, so that a sequences of more than one white space characters (including line breaks) is reduced to a single inter-word space.
Script and Style elements
In XHTML, the script and style elements are declared as having #PCDATA content. As a result, <and & will be treated as the start of markup, and entities such as < and & will be recognized as entity references by the XML processor to < and & respectively.
Wrapping the content of the script or style element within a CDATA marked section avoids the expansion of these entities.
<script type="text/javascript">
<![CDATA[
... unescaped script content ...
]]>
</script>
CDATA sections are recognized by the XML processor and appear as nodes in the Document Object Model, see Section 1.3 of the DOM Level 1 Recommendation [DOM].
An alternative is to use external script and style documents.
Element Prohibitions
The following elements have prohibitions on which elements they can contain.. This prohibition applies to all depths of nesting, i.e. it contains all the descendant elements:
- a: must not contain other a elements. (Hyperlinks cannot contain hyperlinks.)
- pre: must not contain the img, object, big, small, sub, or sup elements.
- button: must not contain the input, select, textarea, label, button, form, fieldset, iframe or isindex elements.
- label: must not contain other label elements.
- form: must not contain other form elements.
Embedded Style Sheets and Scripts
Use external style sheets if your stylesheet uses < or & or ]]> or --.
Use external scripts if your script uses < or & or ]]> or --.
Note that XML parsers are permitted to silently remove the contents of comments. Therefore, the historical practice of "hiding" scripts and style sheets within "comments" to make the documents backward compatible is likely to not work as expected in XML-based user agents.
The lang and xml:lang Attributes
Use both the lang and xml:lang attributes when specifying the language of an element. The value of the xml:lang attribute takes precedence.
Fragment Identifiers
In XML, URI-references [RFC2396] that end with fragment identifiers of the form "#foo" do not refer to elements with an attribute name="foo"; rather, they refer to elements with an attribute defined to be of type ID, e.g., the id attribute in HTML 4.
Many existing HTML clients don't support the use of ID-type attributes in this way, so identical values may be supplied for both of these attributes to ensure maximum forward and backward compatibility (e.g., ...).
Note that XHTML 1.0 has deprecated the name attribute of the a, applet, form, frame, iframe, img, and map elements, and it will be removed from XHTML in subsequent versions.
Character Encoding
Historically, the character encoding of an HTML document is either specified by a web server via the charset parameter of the HTTP Content-Type header, or via a meta element in the document itself.
In an XML document, the character encoding of the document is specified on the XML declaration (e.g., <?xml version="1.0" encoding="EUC-JP"?>). In order to portably present documents with specific character encodings, the best approach is to ensure that the web server provides the correct headers. If this is not possible, a document that wants to set its character encoding explicitly must include both the XML declaration an encoding declaration and a meta http-equiv statement (e.g., <meta http-equiv="Content-type" content="text/html; charset=EUC-JP" />). In XHTML-conforming user agents, the value of the encoding declaration of the XML declaration takes precedence.
If you encode in utf-8 you do not need the xml declaration (which troubles some browsers).
Using Ampersands in Attribute Values (and Elsewhere)
In both SGML and XML, the ampersand character ("&") declares the beginning of an entity reference (e.g., ® for the registered trademark symbol "®"). Unfortunately, many HTML user agents have silently ignored incorrect usage of the ampersand character in HTML documents - treating ampersands that do not look like entity references as literal ampersands.
XML-based user agents will not tolerate this incorrect usage, and any document that uses an ampersand incorrectly will not be "valid", and consequently will not conform to this specification.
In order to ensure that documents are compatible with historical HTML user agents and XML-based user agents, ampersands used in a document that are to be treated as literal characters must be expressed themselves as an entity reference (e.g. "&").
For example, when the href attribute of the a element refers to a CGI script that takes parameters, it must be expressed as http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user rather than as http://my.site.dom/cgi-bin/myscript.pl?class=guest&name=user.
Cascading Style Sheets (CSS) and XHTML
The Cascading Style Sheets level 2 Recommendation [CSS2] defines style properties which are applied to the parse tree of the HTML or XML documents. Differences in parsing will produce different visual or aural results, depending on the selectors used. The following hints will reduce this effect for documents which are served without modification as both media types:
- CSS style sheets for XHTML should use lower case element and attribute names
- In tables, the tbody element will be inferred by the parser of an HTML user agent, but not by the parser of an XML user agent. Therefore you should always explicitly add a tbody element if it is referred to in a CSS selector.
- Within the XHTML namespace, user agents are expected to recognize the "id" attribute as an attribute of type ID. Therefore, style sheets should be able to continue using the shorthand "#" selector syntax even if the user agent does not read the DTD.
- Within the XHTML namespace, user agents are expected to recognize the "class" attribute. Therefore, style sheets should be able to continue using the shorthand "." selector syntax.
- CSS defines different conformance rules for HTML and XML documents; be aware that the HTML rules apply to XHTML documents delivered as HTML and the XML rules apply to XHTML documents delivered as XML.
Referencing Style Elements when serving as XML
In HTML 4 and XHTML, the style element can be used to define document-internal style rules. In XML, an XML stylesheet declaration is used to define style rules. In order to be compatible with this convention, style elements should have their fragment identifier set using the id attribute, and an XML stylesheet declaration should reference this fragment. For example:
<?xml-stylesheet href="http://www.w3.org/StyleSheets/TR/W3C-REC.css" type="text/css"?>
<?xml-stylesheet href="#internalStyle" type="text/css"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>An internal stylesheet example</title>
<style type="text/css" id="internalStyle">
code {
color: green;
font-family: monospace;
font-weight: bold;
}
</style>
</head>
<body>
<p>
This is text that uses our
<code>internal stylesheet</code>.
</p>
</body>
</html>