04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11: DOM Levels 2 and 3<br />

Here, the namespace for XHTML is defined with a prefix of xhtml , requiring all XHTML elements to<br />

begin with that prefix. Attributes may also be namespaced to avoid confusion between languages, as<br />

shown in the following example:<br />

< xhtml:html xmlns:xhtml=”http://www.w3.org/1999/xhtml” ><br />

< xhtml:head ><br />

< xhtml:title > Example XHTML page < /xhtml:title ><br />

< /xhtml:head ><br />

< xhtml:body xhtml:class=”home” ><br />

Hello world!<br />

< /xhtml:body ><br />

< /xhtml:html ><br />

The class attribute in this example is prefixed with xhtml . Namespacing isn ’ t really necessary when<br />

only one XML - based language is being used in a document; it is, however, very useful when mixing two<br />

languages together. Consider the following document containing both XHTML and SVG:<br />

< html xmlns=”http://www.w3.org/1999/xhtml” ><br />

< head ><br />

< title > Example XHTML page < /title ><br />

< /head ><br />

< body ><br />

< svg xmlns=”http://www.w3.org/2000/svg” version=”1.1”<br />

viewBox=”0 0 100 100” style=”width:100%; height:100%” ><br />

< rect x=”0” y=”0” width=”100” height=”100” style=”fill:red” / ><br />

< /svg ><br />

< /body ><br />

< /html ><br />

In this example, the < svg > element is indicated as foreign to the containing document by setting its own<br />

namespace. All children of the < svg > element, as well as all attributes of the elements, are considered to<br />

be in the http://www.w3.org/2000/svg namespace. Even though the document is technically an<br />

XHTML document, the SVG code is considered valid because of the use of namespaces.<br />

The interesting problem with a document such as this is what happens when a method is called on the<br />

document to interact with nodes in the document. When a new element is created, which namespace<br />

does it belong to? When querying for a specific tag name, what namespaces should be included in the<br />

results? DOM Level 2 Core answers these questions by providing namespace - specific versions of most<br />

DOM Level 1 methods.<br />

Changes to Node<br />

The Node type evolves in DOM Level 2 to include the following namespace - specific properties:<br />

❑<br />

❑<br />

❑<br />

localName — The node name without the namespace prefix<br />

namespaceURI — The namespace URI of the node or null if not specified<br />

prefix — The namespace prefix or null if not specified<br />

319

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!