xsl-list
[Top] [All Lists]

Re: Need help rendering the HTML residing within the XML

2004-08-12 09:20:29
Hi Jeremy,

Welcome to XSLT.

At 10:28 PM 8/11/2004, you wrote:
First timer problem (couldn't find an exact answer in the archives): I have an XML file with a hundred plus nodes of which any number could at any time contain HTML tags such as the <b></b> tag in line 2.

1 <book>
2 <name><b>Count of Monte Cristo</b></name>
3 <author>Alexander Dumas</author>
4 <content />
5 </book>

I need my XSL file to parse the XML so that the browser (IE 6+, NN 7+) will take these <b></b> tags and apply the appropiate HTML formatting rather than treating them as XML nodes.

Right. First lesson: forget "tags". Tags in XML are taken, in XSLT, to represent the organization of "nodes" in the tree. So we will call these "elements" or "element nodes", the element being the thing that is tagged (both start- and end-tags, if it's not empty) and everything inside it. So element nodes have content, which generally consists of text nodes and other elements (and sometimes other things like comments).

So if you had:

<author>Alexandre Dumas, <i>père</i></author>

this would be an element node, an 'author', with two children, a text node ("Alexandre Dumas, ") and another element, an 'i' with a text node child.

The browser seems to like character entity equivalents such as in line 2 below and renders the <name> node in bold when I have disable-output-escaping set to "yes".

1 <book>
2 <name>&lt;b&gt;Count of Monte Cristo&lt;/b&gt;</name>
3 <author>Alexander Dumas</author>
4 <content />
5 </page>

I can tell you're using IE because the other major XML-capable browser, Mozilla, won't work with this.

This is why you're liable to get scolded for using disable-output-escaping. Used like this it's a terrible hack that only happens to work in a single readily-available tool.

but the chances of getting character entitiy equivalents into my XML file vs. brackets are slim to none.

Good: you want XML. To kluge XML by representing "tags" using character entities is a practice often maligned by us purists.

Does this mean I'd have to write a query function within the XSL that goes through every node in my XML file looking for regular HTML tags? Wouldn't this take forever to parse considering I could have upwards of a hundred nodes in my XML? Is there something I'm missing? Does anybody know of an article or link that address my problem?

Answers:
1. Goodness, no.
2. Yes, which is why there's a better way. (And many XML documents contain thousands of nodes.) 3. Yes: you are missing the XSLT processing model, which depends on the traversal of the node tree using templates, and can gracefully handle situations like the above (and much worse). 4. Google for "XSLT processing model". Or quite likely we'll get another little rehearsal of it here on the list. It's fundamental, but non-obvious, so we keep coming back to it.

Please feel free to follow up with further questions.

Cheers,
Wendell


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================



<Prev in Thread] Current Thread [Next in Thread>