tags:

views:

35

answers:

1

Hi,

How to use spaces in createElement while creating xml file

I have to use

$main = $doc->createElement("$cname"."Data" );

where

$cname="Company Name"

But due to spaces in $cname, I am getting the following error,

Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in file.php:50 Stack trace: #0 file.php(50): DOMDocument->createElement('geosoft company...') #1 {main} thrown in file.php on line 50

How to rectify this?

Regards, Rekha

+4  A: 

According to http://www.w3.org/TR/2008/REC-xml-20081126/#NT-NameChar an element name can only consists of the following characters

NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]

possible alternative:

$main = $doc->createElement("Data");
$main->setAttribute('origin', $cname);

you might also be interested in xml namespaces

VolkerK
Thank you very much
Rekha