I have a source XHTML document with elements in multiple namespaces that I am transforming into an HTML document (obviously with no namespaces). In my XSL templates I only match elements in the XHTML namespace to remove non-HTML-compatible elements from the result tree. However, in the output, while those elements are gone, the whitespace I used to indent them remains—i.e., lines of irrelevant CR/LFs and tabs.
For example, if this is my input:
<div id="container">
<svg:svg>
<svg:foreignObject>
<img />
</svg:foreignObject>
</svg:svg>
</div>
After applying the transformation, this will be the output:
<div id="container">
<img />
</div>
While my desired output is this:
<div id="container">
<img />
</div>
This happens using both TransforMiiX (attaching the stylesheet locally in Firefox) and libxslt (attaching the stylesheet server-side with PHP), so I know it’s probably the result of some XSL parameter not getting set, but I’ve tried playing with <xsl:output indent="yes|no" />, xml:space="default|preserve", <xsl:strip-space elements="foo bar|*" />, all to no avail.
This will be implemented server-side so if there’s no way to do it in raw XSL but there is a way to do it in PHP I’ll accept that.
I know this is not a namespace issue since I get the same result if I remove ANY element.
The white space you see is from the source document. XSLT default rules say that text nodes should be copied, it does not matter if they are empty or not. To override the default rule, include:
Alternatively: Spot any
<xsl:apply-templates />(or<xsl:apply-templates select="node()" />) and explicitly specify which children you want to apply templates to. This method might be necessary if your transformation partly relies on the identity template (in which case the empty template for text nodes would be counter-productive).I have marked up the “insignificant” white space in your snippet the way Word would do it:
EDIT: You can also modify your identity template like this:
This would remove any blank-only text node (attribute values remain untouched, they are not text nodes). Use
<xsl:output indent="yes" />to pretty-print the result.