Combining XML and XHTML

 <  Day Day Up  >  


In the previous example, which tried to render an XML document using CSS, it might have been useful to add a heading and use line breaks more liberally. You could go about inventing your <h1> and <br> tags but why do so when you have XHTML to serve you? You can use existing XHTML tags easily if you use the xmlns attribute. Consider the following:

 <directory xmlns:html="http://www.w3.org/1999/xhtml">  elements and text  </directory> 

Within the directory element, you can use XHTML tags freely as long as you prefix them with the namespace moniker html we assigned. For example,

 <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <?xml-stylesheet href="staff.css" type="text/css"?>     <directory xmlns:html="http://www.w3.org/1999/xhtml">  <html:h1>  Employee Directory  </html:h1>  <html:hr /> <employee> <name>Fred Brown</name> <title>Widget Washer</title> <phone>(543) 555-1212</phone> <email>fbrown@democompany.com</email> </employee>  <html:br /><html:br />  </directory> 

In this case, you could even attach CSS rules to our newly used XHTML elements and come up with a much nicer layout.

It should be obvious that namespaces are not just for including XHTML markup into an XML file. This facility allows you to include any type of markup within any XML document you like. Furthermore, making sure to prefix each tag with a namespace moniker is highly important especially when you consider how many people just might define their own <employee> tag!

To demonstrate namespaces, let's include XML in the form of MathML into an XHTML file. A rendering of the markup in a MathML-aware Mozilla variant browser is shown in Figure 18-7.

click to expand
Figure 18-7: XHTML with MathML and SVG under Mozilla
 <?xml version="1.0"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"  "http://www.w3.org/TR/MathML2/dtd/xhtml-math11-f.dtd">   <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">   <head>   <title>  MathML Demo  </title>   </head>   <body>   <h1 style="text-align:center;">MathML Below</h1>   <hr />  <math mode="display" xmlns="http://www.w3.org/1998/Math/MathML">   <mrow>     <mfrac>       <mrow>         <mi>x</mi>         <mo>+</mo>         <msup>           <mi>y</mi>           <mn>2</mn>         </msup>       </mrow>           <mrow>         <mi>k</mi>         <mo>+</mo>         <mn>1</mn>       </mrow>     </mfrac>   </mrow> </math>  <hr />   </body>   </html>  
Note  

This example requires the file to be named as .xml or .xhtml to invoke the strict XML parser on XHTML.

The previous example should suggest that XHTML may become host to a variety of languages in the future or that it will be hosted in a variety of other XML-based languages. The questions, then, are: Should the XML be within the XHTML/HTML or should the XHTML be inside the XML? While the W3C may lean toward XML hosting XHTML markup given the deployed base of HTML documents, markup authors may be more comfortable with just the opposite .

Internet Explorer XML Data Islands

Because of the common desire , or in many cases need , to embed XML data content into an HTML document, Microsoft introduced a special <xml> tag in Internet Explorer 4. The <xml> tag is used to create a so-called XML data island that can hold XML to be used within the document. Imagine running a query to a database and fetching more data than needed for the page and putting it in an XML data island. You may then allow the user to retrieve new information from the data island without going back to the server. To include XML in an HTML document, you can use the <xml> tag and either enclose the content directly within it, like so:

 HTML content     <xml id="myIsland">   <directory>   <employee>     <name>Fred Brown</name>     <title>Widget Washer</title>     <phone>(543) 555-1212</phone>     <email>fbrown@democompany.com</email>   </employee>   </directory> </xml>     HTML content 

or you can reference an external file by specifying its URL: <xml id="myIsland" src="staff.xml"></xml>.

Once the XML is included in the document, you can then bind the XML to HTML elements. In the example here, we bind XML data to a table. Notice that you must use fully standard table markup to avoid repeating the headings over and over:

  <!DOCTYPE html PUBLIC "-//W3C//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>  Employee Directory  </title>   <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />   <body>   <xml id="myIsland" src="staff.xml"></xml>   <h1 align="center">DemoCompany Directory</h1>   <hr/>   <table width="100%" datasrc="#myIsland">   <thead>   <tr>   <th>  Name  </th>   <th>  Title  </th>   <th>  Phone  </th>   <th>  Email  </th>   </tr>   </thead>   <tbody>   <tr>   <td><span datafld="name"></span></td>   <td><span datafld="title"></span></td>   <td><span datafld="phone"></span></td>   <td><span datafld="email"></span></td>   </tr>   </tbody>   </table>   </body>   </html>  
Note  

This example will not validate nor work in other browsers besides Internet Explorer as the <xml> tag is a proprietary tag.

The output of the example is as expected and is shown in Figure 18-8.

click to expand
Figure 18-8: With IE's data-binding you can output structured data easily

Once you bind data into a document, you can display it as we did in the previous example, or even use JavaScript and manipulate the contents. Imagine sending the full result of a query to a browser and then allowing the user to sort and page through the data without having to go back to the server. Embedded XML together with JavaScript can make this happen.

Now that you have seen some of the basics of XML, let's consider how the language is actually used on the Web today by discussing some of its application languages.

Note  

The previous discussion is by no means a complete discussion of XML and related technologies, but just enough for us to have the necessary background to present some use of XML and JavaScript together for those unfamiliar with the basics of XML. Readers looking for more detailed information on XML might consider sites like www.xml101.com and, of course, the W3 XML section (www.w3.org/XML).



 <  Day Day Up  >  


HTML & XHTML
HTML & XHTML: The Complete Reference (Osborne Complete Reference Series)
ISBN: 007222942X
EAN: 2147483647
Year: 2003
Pages: 252
Authors: Thomas Powell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net