FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Adding a CDATA section
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Adding a CDATA section [message #173555] Mon, 18 April 2011 19:33 Go to next message
JustWondering is currently offline  JustWondering
Messages: 9
Registered: April 2011
Karma: 0
Junior Member
I am trying to add a CDATA section to a DOMdocument.

Code:

<?php
// Create a DOMDocument object and set nice formatting
$doc = new DOMDocument( "1.0", "UTF-8" );
$doc->formatOutput = true;

// Create the root "stockList" element
$stockList = $doc->createElement( "stockList" );
$doc->appendChild( $stockList );

// Create the first "item" element (apple)
$item = $doc->createElement( "item" );
$item->setAttribute( "type", "fruit" );
$stockList->appendChild( $item );

// Create the item's "description" child element
$description = $doc->createElement( "description" );
$item->appendChild( $description );
$cdata = $doc->createCDATASection( "Apples are <b>yummy</b>" );
$description->appendChild( $cdata );

// Create the second "item" element (beetroot)
$item = $doc->createElement( "item" );
$item->setAttribute( "type", "vegetable" );
$stockList->appendChild( $item );

echo $doc->saveXML();
?>

What I'm expecting to see is Apples are yummy, with yummy in bold.
Here's what I get:

yummy]]>

Firebug / Edit gives me this:

<stocklist>
<item type="fruit">
<description><!--[CDATA[Apples are <b-->yummy]]&gt;</description>
</item>
<item type="vegetable">
</item></stocklist>

What I'm expecting is:

<stocklist>
<item type="fruit">
<description><![CDATA[Apples are <b->yummy</b>]]></description>
</item>
<item type="vegetable">
</item></stocklist>

Any ideas?
Re: Adding a CDATA section [message #173566 is a reply to message #173555] Tue, 19 April 2011 02:37 Go to previous messageGo to next message
Adam Harvey is currently offline  Adam Harvey
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
JustWondering wrote:
> What I'm expecting is:
>
> <stocklist>
> <item type="fruit">
> <description><![CDATA[Apples are <b->yummy</b>]]></description>
> </item>
> <item type="vegetable">
> </item></stocklist>

I'm not really sure why you'd expect that dash in <b->, but at any rate,
running your code on a current PHP 5.3 build results in what I'd expect,
which is:

<?xml version="1.0" encoding="UTF-8"?>
<stockList>
<item type="fruit">
<description><![CDATA[Apples are <b>yummy</b>]]></description>
</item>
<item type="vegetable"/>
</stockList>

What version of PHP are you running?

Adam
Re: Adding a CDATA section [message #173573 is a reply to message #173555] Tue, 19 April 2011 07:40 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
Senior Member
El 18/04/2011 21:33, JustWondering escribió/wrote:
> I am trying to add a CDATA section to a DOMdocument.
>
> Code:
>
> <?php
> // Create a DOMDocument object and set nice formatting
> $doc = new DOMDocument( "1.0", "UTF-8" );
> $doc->formatOutput = true;
>
> // Create the root "stockList" element
> $stockList = $doc->createElement( "stockList" );
> $doc->appendChild( $stockList );
>
> // Create the first "item" element (apple)
> $item = $doc->createElement( "item" );
> $item->setAttribute( "type", "fruit" );
> $stockList->appendChild( $item );
>
> // Create the item's "description" child element
> $description = $doc->createElement( "description" );
> $item->appendChild( $description );
> $cdata = $doc->createCDATASection( "Apples are<b>yummy</b>" );
> $description->appendChild( $cdata );
>
> // Create the second "item" element (beetroot)
> $item = $doc->createElement( "item" );
> $item->setAttribute( "type", "vegetable" );
> $stockList->appendChild( $item );
>
> echo $doc->saveXML();
> ?>
>
> What I'm expecting to see is Apples are yummy, with yummy in bold.
> Here's what I get:
>
> yummy]]>

You are getting the expected output, you are just not viewing it
properly. You need to add the appropriate Content-Type header so the
browser knows it is an XML document:

header('Content-Type: text/xml; charset=UTF-8');

Otherwise, the server will send the default type, which is probably
text/html.


> Firebug / Edit gives me this:
>
> <stocklist>
> <item type="fruit">
> <description><!--[CDATA[Apples are<b-->yummy]]&gt;</description>
> </item>
> <item type="vegetable">
> </item></stocklist>

That panel does not display the original source code but and an
interpretation of it. In this case, the code misinterpreted as HTML and
fixed to fit.

Use the browser's "View source" feature instead (or run the script from
the command line).


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
Re: Adding a CDATA section [message #173574 is a reply to message #173566] Tue, 19 April 2011 09:13 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Adam Harvey wrote:

> JustWondering wrote:
>> What I'm expecting is:
>>
>> <stocklist>
>> <item type="fruit">
>> <description><![CDATA[Apples are <b->yummy</b>]]></description>
>> </item>
>> <item type="vegetable">
>> </item></stocklist>
>
> I'm not really sure why you'd expect that dash in <b->, but at any rate,
> running your code on a current PHP 5.3 build results in what I'd expect,
> which is:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <stockList>
> <item type="fruit">
> <description><![CDATA[Apples are <b>yummy</b>]]></description>
> </item>
> <item type="vegetable"/>
> </stockList>
>
> What version of PHP are you running?

PHP is not the issue here, but that the OP has not understood what XML is
(and how it is to be used), what a CDATA section is (that its content is
*not* parsed except for `]]>', and so cannot possibly display bold because
of <b>…</b> in it), and that Firebug's innerHTML-based Edit feature is shaky
at best with XML documents served as text/html.

Something along

header('Content-Type: text/xml; charset=UTF-8');

is missing after

<?php

but that would still not display anything of the markup in bold. (And that
is good so.)


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
Re: Adding a CDATA section [message #173582 is a reply to message #173566] Tue, 19 April 2011 14:50 Go to previous messageGo to next message
JustWondering is currently offline  JustWondering
Messages: 9
Registered: April 2011
Karma: 0
Junior Member
On Apr 18, 7:37 pm, Adam Harvey <use...@adamharvey.name> wrote:
> JustWondering wrote:
>> What I'm expecting is:
>
>> <stocklist>
>>   <item type="fruit">
>>     <description><![CDATA[Apples are <b->yummy</b>]]></description>
>>   </item>
>>   <item type="vegetable">
>> </item></stocklist>
>
> I'm not really sure why you'd expect that dash in <b->, but at any rate,
> running your code on a current PHP 5.3 build results in what I'd expect,
> which is:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <stockList>
>   <item type="fruit">
>     <description><![CDATA[Apples are <b>yummy</b>]]></description>
>   </item>
>   <item type="vegetable"/>
> </stockList>
>
> What version of PHP are you running?
>
> Adam

That was a a typo when I copied it. I am expecting <b>. When I run
phpinfo() it tells me that the PHP version is 5.3.4
Re: Adding a CDATA section [message #173583 is a reply to message #173574] Tue, 19 April 2011 15:13 Go to previous messageGo to next message
JustWondering is currently offline  JustWondering
Messages: 9
Registered: April 2011
Karma: 0
Junior Member
On Apr 19, 2:13 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> Adam Harvey wrote:
>> JustWondering wrote:
>>> What I'm expecting is:
>
>>> <stocklist>
>>>   <item type="fruit">
>>>     <description><![CDATA[Apples are <b->yummy</b>]]></description>
>>>   </item>
>>>   <item type="vegetable">
>>> </item></stocklist>
>
>> I'm not really sure why you'd expect that dash in <b->, but at any rate,
>> running your code on a current PHP 5.3 build results in what I'd expect,
>> which is:
>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <stockList>
>>   <item type="fruit">
>>     <description><![CDATA[Apples are <b>yummy</b>]]></description>
>>   </item>
>>   <item type="vegetable"/>
>> </stockList>
>
>> What version of PHP are you running?
>
> PHP is not the issue here, but that the OP has not understood what XML is
> (and how it is to be used), what a CDATA section is (that its content is
> *not* parsed except for `]]>', and so cannot possibly display bold because
> of <b>…</b> in it), and that Firebug's innerHTML-based Edit feature is shaky
> at best with XML documents served as text/html.
>
> Something along
>
>   header('Content-Type: text/xml; charset=UTF-8');
>
> is missing after
>
>   <?php
>
> but that would still not display anything of the markup in bold.  (And that
> is good so.)
>
> PointedEars
> --
> Use any version of Microsoft Frontpage to create your site.
> (This won't prevent people from viewing your source, but no one
> will want to steal it.)
>   -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

Perhaps you should read the post before making up assumptions about
the OP. Where did I say that I'm expecting to see bold? I am pointing
to the generated XML code, not how the generated code is displayed.
Why would I expect anything in a CDATA section to be displayed?
Re: Adding a CDATA section [message #173600 is a reply to message #173583] Wed, 20 April 2011 08:51 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
JustWondering wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Adam Harvey wrote:
>>> JustWondering wrote:
>>>> What I'm expecting is:
>>>> <stocklist>
>>>> <item type="fruit">
>>>> <description><![CDATA[Apples are <b->yummy</b>]]></description>
>>>> </item>
>>>> <item type="vegetable">
>>>> </item></stocklist>
>>
>>> I'm not really sure why you'd expect that dash in <b->, but at any
>>> rate, running your code on a current PHP 5.3 build results in what I'd
>>> expect, which is:
>>
>>> <?xml version="1.0" encoding="UTF-8"?>
>>> <stockList>
>>> <item type="fruit">
>>> <description><![CDATA[Apples are <b>yummy</b>]]></description>
>>> </item>
>>> <item type="vegetable"/>
>>> </stockList>
>>
>>> What version of PHP are you running?
>>
>> PHP is not the issue here, but that the OP has not understood what XML is
>> (and how it is to be used), what a CDATA section is (that its content is
>> *not* parsed except for `]]>', and so cannot possibly display bold
>> because of <b>…</b> in it), and that Firebug's innerHTML-based Edit
>> feature is shaky at best with XML documents served as text/html.
>>
>> Something along
>>
>> header('Content-Type: text/xml; charset=UTF-8');
>>
>> is missing after
>>
>> <?php
>>
>> but that would still not display anything of the markup in bold. (And
>> that is good so.)
>
> Perhaps you should read the post before making up assumptions about
> the OP.

I really do not have to make assumptions in this case. It is very obvious
that you do not know what you are doing.

> Where did I say that I'm expecting to see bold?

In your OP. Precisely:

>>> What I'm expecting to see is Apples are yummy, with yummy in bold.

> I am pointing to the generated XML code, not how the generated code is
> displayed.

Then you should have said so. XML is not (X)HTML; a `b' element has no
inherent meaning there. And when `<b>…</b>' is in a CDATA section, it does
not constitute an element at all.

> Why would I expect anything in a CDATA section to be displayed?

You should expect the content of a CDATA section to be displayed verbatim
because that is how it works. You are not expecting it because evidentially
you have not understood what a CDATA section is, and how XML works.

Please trim your quotes to the relevant minimum.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
Re: Adding a CDATA section [message #173673 is a reply to message #173600] Wed, 27 April 2011 21:23 Go to previous message
JustWondering is currently offline  JustWondering
Messages: 9
Registered: April 2011
Karma: 0
Junior Member
On Apr 20, 1:51 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> JustWondering wrote:
>> Thomas 'PointedEars' Lahn wrote:
>>> Adam Harvey wrote:
>>>> JustWondering wrote:
>>>> > What I'm expecting is:
>>>> > <stocklist>
>>>> > <item type="fruit">
>>>> > <description><![CDATA[Apples are <b->yummy</b>]]></description>
>>>> > </item>
>>>> > <item type="vegetable">
>>>> > </item></stocklist>
>
>>>> I'm not really sure why you'd expect that dash in <b->, but at any
>>>> rate, running your code on a current PHP 5.3 build results in what I'd
>>>> expect, which is:
>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <stockList>
>>>> <item type="fruit">
>>>> <description><![CDATA[Apples are <b>yummy</b>]]></description>
>>>> </item>
>>>> <item type="vegetable"/>
>>>> </stockList>
>
>>>> What version of PHP are you running?
>
>>> PHP is not the issue here, but that the OP has not understood what XML is
>>> (and how it is to be used), what a CDATA section is (that its content is
>>> *not* parsed except for `]]>', and so cannot possibly display bold
>>> because of <b>…</b> in it), and that Firebug's innerHTML-based Edit
>>> feature is shaky at best with XML documents served as text/html.
>
>>> Something along
>
>>> header('Content-Type: text/xml; charset=UTF-8');
>
>>> is missing after
>
>>> <?php
>
>>> but that would still not display anything of the markup in bold.  (And
>>> that is good so.)
>
>> Perhaps you should read the post before making up assumptions about
>> the OP.
>
> I really do not have to make assumptions in this case.  It is very obvious
> that you do not know what you are doing.
>
>> Where did I say that I'm expecting to see bold?
>
> In your OP.  Precisely:
>
>>>> What I'm expecting to see is Apples are yummy, with yummy in bold.
>> I am pointing to the generated XML code, not how the generated code is
>> displayed.
>
> Then you should have said so.  XML is not (X)HTML; a `b' element has no
> inherent meaning there.  And when `<b>…</b>' is in a CDATA section, it does
> not constitute an element at all.
>
>> Why would I expect anything in a CDATA section to be displayed?
>
> You should expect the content of a CDATA section to be displayed verbatim
> because that is how it works.  You are not expecting it because evidentially
> you have not understood what a CDATA section is, and how XML works.
>
> Please trim your quotes to the relevant minimum.
>
> PointedEars
> --
> Use any version of Microsoft Frontpage to create your site.
> (This won't prevent people from viewing your source, but no one
> will want to steal it.)
>   -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)

So, you've decided that the only legitimate use of CDATA is what your
blinders point you to. If it has not occurred to you yet, there are
possible uses for an XML/XHTML structure other than being viewed
through a web browser. Please trim your ego and insults to the
relevant minimum.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Using exec with & - what might I get back?
Next Topic: php sessions and css file?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 05:22:18 GMT 2024

Total time taken to generate the page: 0.04741 seconds