Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #1174 > unrolled thread

Adding a CDATA section

Started byJustWondering <eastsidedev@gmail.com>
First post2011-04-18 12:33 -0700
Last post2011-04-19 09:40 +0200
Articles 8 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Adding a CDATA section JustWondering <eastsidedev@gmail.com> - 2011-04-18 12:33 -0700
    Re: Adding a CDATA section Adam Harvey <usenet@adamharvey.name> - 2011-04-19 10:37 +0800
      Re: Adding a CDATA section Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-04-19 11:13 +0200
        Re: Adding a CDATA section JustWondering <eastsidedev@gmail.com> - 2011-04-19 08:13 -0700
          Re: Adding a CDATA section Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-04-20 10:51 +0200
            Re: Adding a CDATA section JustWondering <eastsidedev@gmail.com> - 2011-04-27 14:23 -0700
      Re: Adding a CDATA section JustWondering <eastsidedev@gmail.com> - 2011-04-19 07:50 -0700
    Re: Adding a CDATA section "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2011-04-19 09:40 +0200

#1174 — Adding a CDATA section

FromJustWondering <eastsidedev@gmail.com>
Date2011-04-18 12:33 -0700
SubjectAdding a CDATA section
Message-ID<f836e19a-0e5f-4962-8d3d-7b281f6ef629@o15g2000prn.googlegroups.com>
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?

[toc] | [next] | [standalone]


#1185

FromAdam Harvey <usenet@adamharvey.name>
Date2011-04-19 10:37 +0800
Message-ID<ioish6$hj6$1@lawngnome.eternal-september.org>
In reply to#1174
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

[toc] | [prev] | [next] | [standalone]


#1193

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-04-19 11:13 +0200
Message-ID<1854315.SyKVeVyVuy@PointedEars.de>
In reply to#1185
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.)

[toc] | [prev] | [next] | [standalone]


#1202

FromJustWondering <eastsidedev@gmail.com>
Date2011-04-19 08:13 -0700
Message-ID<83f1ea86-c11d-4242-b0f8-29cb7cdc8bf1@j35g2000prb.googlegroups.com>
In reply to#1193
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?

[toc] | [prev] | [next] | [standalone]


#1221

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2011-04-20 10:51 +0200
Message-ID<2034437.qChMirdbgy@PointedEars.de>
In reply to#1202
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.)

[toc] | [prev] | [next] | [standalone]


#1301

FromJustWondering <eastsidedev@gmail.com>
Date2011-04-27 14:23 -0700
Message-ID<c1c972d2-e790-43a6-9b44-2613dff37241@r33g2000prh.googlegroups.com>
In reply to#1221
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.

[toc] | [prev] | [next] | [standalone]


#1201

FromJustWondering <eastsidedev@gmail.com>
Date2011-04-19 07:50 -0700
Message-ID<383aa1e3-c3df-460a-9d17-360367dabe34@g7g2000pro.googlegroups.com>
In reply to#1185
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

[toc] | [prev] | [next] | [standalone]


#1192

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2011-04-19 09:40 +0200
Message-ID<ioje8u$m66$1@dont-email.me>
In reply to#1174
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
--

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web