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:
- White spaces are reduced into a single space even over begin and end tags within the same block
- 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.
- 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.
- 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
How to Prevent Text in a Table Cell from Wrapping
How to Center a Div Within Another Div
How to Add an Element After Another Element
Html/CSS How to Add Image Icon to Input Type="Button"
Vertical (Rotated) Text in HTML Table
CSS Single or Multiple Line Vertical Align
Z-Index Not Working with Position Absolute
Extra Space Under Textarea, Differs Along Browsers
How to Rotate a <Div> 90 Degrees
How to Properly Use H1 in HTML5
Specifying Content of an Iframe Instead of the Src Attribute to a Page
How to Get a Specific Number Child Using CSS
$Http Doesn't Send Cookie in Requests
How to Give the Background-Image Path in CSS
CSS - Relative Positioned Parent Div Not Stretching to Absolute Child Div Height