JohnRC
JohnRC

Reputation: 1371

XSLT copy-of seems to add line feeds between nodes in some circumstances

I am using xslt to transform xml into html.

Part of the incoming xml contains nodes that are copied directly to the output within pre tags using xslt:copy-of.

I have a problem in that part of the copied node has line feeds inserted where there are none in the original XML. The line feeds upset the formatting, because the pre tag is sensitive to line feeds in the html body.

I am using Microsoft Msxml2.FreeThreadedDOMDocument.6.0 to hold the XML and perform the transformation. I am using XSLT version 2.0 and have indent="no" method="xml" in the xsl:output node.

The specific circumstance in which a line feed is added seems to be when two html tags follow one another with intervening spaces and no other text. Here is the actual text of the XML node being transformed:

<span field='text'>
  <a href="aa">aa</a><a href="bb">bb</a>
  <a href="cc">cc</a> <a href="dd">dd</a>
  <a href="ee">ee</a>- -<a href="ff">ff</a>
</span>

For explanation, are three lines within the node. Each line contains two hyperlinks, the first with no intervening space, the second with an intervening space, and the third with intervening text that contains a space.

The relevant part of the transform is:

<pre style="margin-left: 2em" ><xsl:copy-of select=" span[@field='text'] "/></pre>

The output HTML text that is generated is:

<pre style="margin-left: 2em"><span field="text" xmlns=""><a href="aa">aa</a><a href="bb">bb</a>
<a href="cc">cc</a>
<a href="dd">dd</a>
<a href="ee">ee</a>- -<a href="ff">ff</a>
</span></pre>

The first line and the third line have been output as supplied, but the second line, in which the anchor tags have a single intervening space, has been split into two lines.

I have tried using the form <xsl:copy-of select="span[@field='text']/node() " /> in the transform, but this removes all the line feeds so none of the layout is preserved.

Am I doing something wrong? Is this behaviour specific to Microsoft XML?

Any assistance you can give would be much appreciated.

Upvotes: 0

Views: 535

Answers (2)

Martin Honnen
Martin Honnen

Reputation: 167571

I am able to reproduce the problem in http://home.arcor.de/martin.honnen/xslt/test2014101201.html but when I set preserveWhiteSpace = true on the input DOM document, as done in http://home.arcor.de/martin.honnen/xslt/test2014101203.html, the problem goes away. So a complete test case showing how to solve the problem with preserveWhiteSpace is

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Test</title>



<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
var xmlMarkup = "<span field='text'>\n  <a href=\"aa\">aa</a><a href=\"bb\">bb</a>\n  <a href=\"cc\">cc</a> <a href=\"dd\">dd</a>\n  <a href=\"ee\">ee</a>- -<a href=\"ff\">ff</a>\n</span>";
var xmlDoc = new ActiveXObject('Msxml2.DOMDocument.6.0');
xmlDoc.preserveWhiteSpace = true;
xmlDoc.loadXML(xmlMarkup);

var xslDoc = new ActiveXObject('Msxml2.DOMDocument.6.0');
xslDoc.loadXML([
    '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">',
    '<xsl:output method="xml" indent="no"/>',
    '<xsl:template match="/">',
    '<pre>',
    '<xsl:copy-of select="span[@field = \'text\']"/>',
    '</pre>',
    '</xsl:template>',
    '</xsl:stylesheet>'].join('\n'));

document.getElementById('output').value = xmlDoc.transformNode(xslDoc);
}//]]>  

</script>


</head>
  <body>
  <textarea id="output" rows="10" cols="80"></textarea>

</body>


</html>

and then the output, as tested in IE 11 on Windows 8.1, is

<?xml version="1.0"?><pre><span field="text">
  <a href="aa">aa</a><a href="bb">bb</a>
  <a href="cc">cc</a> <a href="dd">dd</a>
  <a href="ee">ee</a>- -<a href="ff">ff</a>
</span></pre>

Upvotes: 2

michael.hor257k
michael.hor257k

Reputation: 116993

Is this behaviour specific to Microsoft XML?

That's easy to find out: compare your results to say Saxon 6.5.5 here: http://xsltransform.net/bFDb2BX

Note the effect of adding the <xsl:strip-space elements="*"/> instruction: http://xsltransform.net/bFDb2BX/1

--
P.S. I am not sure what exactly you're trying to accomplish here with the <pre> tags, since the rendered result of the first version above is:

enter image description here

Upvotes: 2

Related Questions