ITworld.com
  Search  
Menu Changing the way you view IT
HTML Just Passing Through
Sign up for XML IN PRACTICE
More Newsletters
 
 

XML IN PRACTICE --- 03/01/2001



Mark Johnson

XML's many applications include one traditionally handled by HTML, document source. Sometimes you want to include some HTML in your XML source; particularly when styling your XML with XSL to HTML, you may want some literal HTML in the output. In some cases, you'd just like to include some well-formed mixed-content HTML in your XML, and not worry too much about controlling its structure.
Advertisement
On this topic




Writing explicit XSL rules for each HTML tag would be tedious and hard to maintain. This week, I'll present a quick shortcut that allows you to include arbitrary mixed-content HTML in your XSL stylesheet's output.

The XSL stylesheet's input includes some XML document, constrained by (that is, validated against) a particular DTD. To define the element containing the arbitrary HTML's content model, simply define content models for each of the HTML elements you want to use and their attribute lists. For example, define the <.a> tag for hyperlinks like this:

<.!ELEMENT a (#PCDATA)*>

<.!ATTLIST a
href CDATA #IMPLIED target CDATA #IMPLIED name CDATA #IMPLIED>

(This example is simplified, since it allows only unformatted text inside the hyperlink text node.) Create a definition like this for each HTML tag you wish to use. After you've defined the tags, define a parameter entity that includes all of the HTML "pass-through" tags you've defined:

<.!ENTITY % htmlpassthru
"a|i|b|code|br|tr|td|th|img|font|em">

Anywhere in the DTD you want to include mixed-content HTML, use this parameter entity in the DTD. For example:

<.!ELEMENT HtmlHelpText (#PCDATA|%htmlpassthru;)*>

Now for the stylesheet. Defining a separate rule for each possible HTML element within a HtmlHelpText element would be extremely tedious. Fortunately, a single rule can handle all such elements. Define an XSL rule that matches the tag name AND uses <.xsl:copy-of> to copy the entire node (including attributes) to the output.

<.!-- EXAMPLE XSL stylesheet -->

<.xsl:template
match="a|i|b|code|br|tr|td|th|img|font|em"> <.xsl:copy-of select="."/> <./xsl:template>

<.!-- End EXAMPLE -->

Any nodes in the XML input tree with these tags will be copied to the output structure unmodified, attributes and all.

This technique is somewhat "quick-and-dirty". Choosing XHTML, which reformulates HTML as XML and brings XML's extensibility and structure control into the world of HTML, would be a better solution. But that's a topic for another newsletter.

 

Mark Johnson is president of Elucify Technical Communications, a Colorado-based training and consulting company dedicated to clarifying novel or complex ideas through clear explanation and examples.

Sponsored links
Top 5 Reasons to Combine App Performance and Security
KODAK i1400 Series Scanners stand up to the challenge
Locate Hidden Software on business PCs with this free tool
Bring harmony to your mix of UNIX-Linux-Windows computing environments
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Industry Standard   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.