XML? Does all node contents require HTML special chars (ie. > = >)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am currently writing a simple PHP program that uses an XML file to
output rows for a 'Whats New' page. Once written, I will only require
updating the XML file and any pages that use the XML file will get
their row content from there. The rows may look some thing similar to

- Added <a>mailing list</a> functionality to <a>homepage</a>
- Competition winners announced, click <a>here</a>
- Guidance documents now available - <a>French</a>, <a>German</a> and

(obviously, the link tags (<a>) would be in full with href (<a
href="...">) attribute but for the sake of space and simplicity I have
presented them as they are above)

As you can see, they all differ in where links may be for each row. As
a result, I figured that the best solution is to contain the HTML
within the XML nodes:

<?xml version="1.0" encoding="iso-8859-1"?>
    <item date="2005-08-08">Added <a>mailing list</a> functionality to
    <item date="2005-08-05">Competition winners announced, click
    <item date="2005-08-05">Guidance documents now available -
<a>French</a>, <a>German</a> and <a>Spanish</a></item>

The PHP for outputing this is:

$xmldoc = domxml_open_file('whatsnew.xml',
$node = $xmldoc->document_element();
if ($xmldoc->has_child_nodes()) {
  $node = $node->first_child();
  $endwhile = false;
  while ($endwhile != true) {
    echo $node->get_content() . "<br />";
    if ($node->next_sibling()) {
      $node = $node->next_sibling();
      $endwhile = true;

This seemed perfect until I realised that when I output them, it didnt
display the HTML tags inside the <item> nodes. So instead I would get
something like:

- Added mailing list functionality to homepage
- Competition winners announced, click here
- Guidance documents now available - French, German and Spanish

(note the links missing)

I understood why this so converted the node content to:

<item date="2005-08-08">Added &lt;a&gt;mailing list&lt;/a&gt;
functionality to &lt;a&gt;homepage&lt;/a&gt;<item>

This worked a treat but I was kinda hoping that I could keep the html
tags intact when in the <item> nodes as it is easier, at a glance, to
recognise the HTML code within the nodes. XML validator doesnt seem to
mind when there is raw HTML (<a> not &lt;a&gt;) inside nodes so is it
possible to change the PHP '$node->get_contents' to something that will
preserve the HTML tags as they are and display them on screen?

If not, I will have to change all the HTML tags to HTML special
characters but this will be quite a job.

Any help would be great, thanks


Re: XML? Does all node contents require HTML special chars (ie. > = >)

Quoted text here. Click to load it

Things to look up:  

<attribute name='content'>
    <a href="http://example.com ">there you go</a>  

<xsl:value-of select="$content" disable-output-escaping="yes"/>

Met vriendelijke groeten,
Tim Van Wassenhove <http://timvw.madoka.be

Re: XML? Does all node contents require HTML special chars (ie. > = >)

Quoted text here. Click to load it

Would I be right in saying that this is XSLT? I dont really have a
great deal of knowledge in this. Is it possible to use this with PHP
XMLDOM doing the output? If so, how would I embed this into my code?
Ideally, can it be done with XMLDOM alone? Cheers


Site Timeline