XML considerations
Here are the techniques you should try to avoid when working with XML.
Internet Explorer - XML Data Island
What is it? XML data islands are XML data embedded in HTML pages.
Why should you avoid using it? XML data islands are only available in the Internet Explorer browser.
What to replace it with? You should use JavaScript and XML DOM in HTML to parse and display XML.
For more information about JavaScript and XML DOM, visit our XML DOM tutorial.
XML Data Island Example
This example uses the XML document "cd_catalog.xml".
Bind the XML document to an <xml> tag in the HTML document. The id attribute defines the identifier of the data island, while the src attribute points to the XML file:
Instance
<html> <body> <xml id="cdcat" src="cd_catalog.xml"></xml> <table border="1" datasrc="#cdcat"> <tr> <td><span datafld="ARTIST"></span></td> <td><span datafld="TITLE"></span></td> </tr> </table> </body> </html>
Run Example»
Click the "Run Example" button to view the online example
The datasrc attribute of the<table> tag binds the HTML table to the XML data island. The
<span> tag allows the datafld attribute to reference the XML element to be displayed. In this example, the references are "ARTIST" and "TITLE". When the XML is read, a corresponding table row is created for each <CD> element.
Internet Explorer - Behavior
What is it? Internet Explorer 5 introduced behaviors. Behaviors are a way to add behavior to XML (or HTML) elements by using CSS styles.
Why should you avoid using it? Only Internet Explorer supports the behavior attribute.
What to use instead? Use JavaScript and XML DOM (or HTML DOM) instead.
Example 1 - Mouseover Highlight
The <style> element in the following HTML file defines a behavior for the <h1> element:
<head>
<style type="text/css">
h1 { behavior: url(behave.htc) }
</style>
</head>
<body>
<h1>Mouse over me!!!</h1>
</body>
< /html>
The following shows the XML document "behave.htc" (the file contains a JavaScript and event handlers for the elements):
Example
<html> <head> <style type="text/css"> h1 { behavior: url(behave.htc) } </style> </head> <body> <h1>Mouse over me!!!</h1> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
Example 2 - Typewriter Simulation
The <style> element in the following HTML file defines a behavior for the element with the id "typing":
<head>
<style type="text/css">
#typing
{
behavior:url(typing.htc);
font-family :'courier new';
}
</style>
</head>
<body>
<span id="typing" speed=" 100">IE5 introduced DHTML behaviors.
Behaviors are a way to add DHTML functionality to HTML elements
with the ease of CSS.<br /><br />How do behaviors work?< ;br />
By using XML we can link behaviors to any element in a web page
and manipulate that element.</p>v </span>
</body>
</html>
The following is the XML document "typing.htc":
Instance
<attach for="window" event="onload" handler="beginTyping" /> <method name="type" /> <script> var i,text1,text2,textLength,t; function beginTyping() { i=0; text1=element.innerText; textLength=text1.length; element.innerText=""; text2=""; t=window.setInterval(element.id+".type()",speed); } function type() { text2=text2+text1.substring(i,i+1); element.innerText=text2; i=i+1; if (i==textLength) { clearInterval(t); } } </script>
Run instance»
Click the "Run instance" button to view the online instance