White Space Inside Xml/HTML Tags

White space inside XML/HTML tags

The specification (section 3.1 Start-tags, end-tags, and empty-element tags) says that there is no white space between the '<' and the tag name, between '</' and the tag name, or inside '/>'. You can add white space after the tag name, though:

<foo            >
</foo >
<bar
/>

Spaces in XML element to show in HTML control

Add

xml:space="preserve"

to your xsl stylesheet or your input doc.

Here's a thorough guide to whitespace handling in XSLT.

EDIT:

To retain white space in the rendered HTML, use css style white-space:pre on the element in which you want to preserve white-space.

White space handling in XHTML

It seems that there is no real documentation on how white spaces are rendered in XHTML. Here is what I found out by experiment:

  1. White spaces are reduced into a single space even over begin and end tags within the same block
  2. The space will be put into the formatting scope of the containing tag. If it spans two tags it will be added to the first tag.
  3. Spaces at the begin and end of block elements or span elements which are the first child element/ the last child element in their block are ignored.
  4. White spaces outside of block elements are ignored.

This is all I could figure out. It is kind of sad that the XHTML specifiaction does not contain information about rendering of white spaces.

Remove white spaces between tag values in xml with php

You can force SimpleXML to trim all whitespace when it reads the file, by passing the LIBXML_NOBLANKS option to simplexml_load_file:

$sXML = simplexml_load_file('file.xml', null, LIBXML_NOBLANKS);

Then when you call ->asXML(), all the whitespace will be removed, and you'll get XML all on one line, like this:

<?xml version="1.0" encoding="utf-8"?>
<products><item><reference>00003</reference><other_string>PRODUCT 3</other_string><brand>BRAND 3</brand></item><item><reference>00004</reference><other_string>PRODUCT 4</other_string><brand>BRAND 4</brand></item></products>

To re-generate whitespace based on the remaining structure, you'll need to use DOM rather than SimpleXML - but that's easy to do without changing any of your existing code, because dom_import_simplexml simply "rewraps" the XML without reparsing it.

Then you can use the DOMDocument formatOutput property and save() method to "pretty-print" the document:

$sXML = simplexml_load_file('file.xml', null, LIBXML_NOBLANKS);
// ...
// process $sXML as before
// ...
$domDocument = dom_import_simplexml($sXML)->ownerDocument;
$domDocument->formatOutput = true;
echo $domDocument->save('filtered.xml');

Adding white space in XML file tag

If you want artificial whitespace, you'll simple needed to add it:

sub topicref_processing {
my($twig, $ppttext) = @_;
print $ppttext->text(), " ";
}


Related Topics



Leave a reply



Submit