Jump to content

question in spaces and CDATA


BsM

Recommended Posts

How can i fix the spaces problem in XML elements contentas i know from XML tutorials in the site XML remove spaces and leave only one space ex:<forum><topic>Test topic for(4spaces here)spaces</topic></forum>when i try to get the topic element content it will be"Test topic for spaces" not"Test topic for(4spaces here)spaces"how to get it as the user write it "with it's spaces" so that my program will be very delicate :) (i have notice that "IPBoard" forum from invision board remove spaces automatically so that i write "4spaces here")--------------------------------------------------------------------------------------------------------What's the use of CDATA as i know from XML tutorials CDATA section will be ignored from the parserso that why i use it as XML allow a method to write Comments :)

Link to comment
Share on other sites

Its not the XML that removes the spaces, its the rendering agent. In your case you are probably viewing the XML in a browser. IE, I know, will automatically normalize white-space. If you need to preserve white space while viewing XML output, you need to use <PRE> tags, where white-space is preservedYou could substitute spaces with their equilivant non-breaking space character ( ) but this is making assumptions that you know what the rendering agent will be. <test> <item>Here there be four spaces . Period follows four spaces</item> <item>Here there be four spaces    . Period follows four spaces</item></test>This will show up as a single space in IE for the first item, four spaces for the second.note: the rendering agent here is changing the code for the space into a space, The code is & # 1 6 0 ; (remove the spaces between the & and :)

Link to comment
Share on other sites

thank's aloti have found the solution that i make a code to counting spaces and then replace it with the code & # 1 6 0 ;but i want to know where can i found a table of this codes such as & # 1 6 0 ;

Link to comment
Share on other sites

I think you're missing a very good point aalbetski is making. It's not the XML that's missing the spaces. It's (X)HTML. To verify that, view the actual (X)HTML source in any text editor (like Notepad) and see if there is more then one space there. If so, you need to readjust the CSS of the (X)HTML or the (X)HTML itself. CSS properties like "white-space" and the <pre/> element help a lot in theese cases.If it turns out to be the XML, you can force preservation by adding the attribute xml:space="preserve" wherever you need to preserve space, like so:

<forum><topic xml:space="preserve">Test topic for	spaces</topic></forum>

and the output code will be sure to have those spacesHowever, if the output is (X)HTML, you should apply the above tricks instead of relying on numeric entities. Not only will it be more efficient, but also more compact. I mean & # 1 6 0 ; or & n b s p ; (the other way to use non-breaking space when using (X)HTML) are 6 characters for each space. If a character is a byte and you have 100 extra spaces inside a document, this means 600 extra bytes (0,6KB)... only for spaces? It's not worth it. At the same time

white-space:pre;

are 15 characters. When you have 100 spaces, you keep only those spaces plus those 15 characters, meaning 115 bytes (0,1KB). And that could be even more dramatic improvement in bigger pages.If you still insist on the less efficient and not good entity way, use the HTML entities reference. However, note that in XML, the only allowed named entities are the ones in the first table. Other entities (including & n b s p ; ) are available only by their numeric equivalent.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...