tags:

views:

204

answers:

2

I want to print my output xml in a single line[when viewed in notepad or other simple text-editor], so as to remove the redundant white-space in my xml file. So which is the better method to follow for that ??

I think there are two options,
1) To use

  <xsl:output method="xml" indent="no"/>

2) or to use

  <xsl:strip-space elements="*"/>

Which is more efficient, and why?
some people[in my bay] say that .. <strip-space/> applies, so they suggest me to use indent="no",

I believed that strip-space is best suited, but not sure because of suggestions given by others.

To be more elaborated let me take an example:
Input XML:

<root>
 <node>
   <child1/>
   <child2/>
 </node>
</root>

and the output required is:

<root><node><child1/><child2/></node></root>
+2  A: 

Perfomance differences are best measured. XSLT processor implementations differ, and you should make the test for yourself (though I suspect that worrying over the performance of the one or the other might fall into the "premature optimization" category in this case).

<xsl:output indent="no" /> might not have the effect you want unless accompanied by

<xsl:template match="text(normalize-space()='')" />

because if whitespace nodes (the ones between your tags) are not removed, then they will appear in the output at some point, regardless of the "output" setting.

Tomalak
Sorry for the previous comment -- I was still sleepy -- please, delete it.Why would you code the template, when it means the same as `<xsl:strip-space element="*"/>` ?
Dimitre Novatchev
@Dimitre: I meant it solely in conjunction with `<xsl:output indent="no" />`, because `indent="no"` would not be sufficient without any explicit whitespace handling. P.S.: The only one who can delete your comments is yourself. :-) (I know, this has been different in an early version of the site)
Tomalak
+2  A: 

In order to eliminate anything that looks like "indentation" it may be necessary (that means there are cases when you need) to use both <xsl:strip-space> and `indent="no".

Take the simplest example: you have the identity transformation. Without any of the two methods specified, the transformation will reproduce the white-space-only text nodes from the source XML document. That is, if the source XML document is indented, the transformation will produce indented result, too.

Now, add to this transformation <xsl:output indent="no" />. This instructs the XSLT processor not to perform "pretty-printing" of its own. However, the whitespace-only nodes from the source XML document are still copied to the output and the result document looks still indented (because the source document is indented).

Now, as a last step, add <xsl:strip-space elements="*"/>. You have specified both methods of preventing white-space-only nodes in the output. What happens? No white-space-only nodes are processed at all by the XSLT processor, and it does not indent the output -- you get your desired one-line dense output.

Finally, make a regression, change the <xsl:output indent="no" /> to <xsl:output indent="yes" />. The <xsl:strip-space elements="*"/> is still there, so no whitespace-only nodes are reproduced in the output. But the XSLT processor obeys the <xsl:output indent="yes" /> directive and adds whitespace-only text nodes of its own.

So, from the four possible combinations, only specifying both <xsl:strip-space elements="*"/> and <xsl:output indent="no" /> guarantees that no indentation will be caused either from whitespace-only nodes from the source XML document or from the XSLT processors initiative.

Even this last case, of course, doesn't completely guarantee that the output won't be indented -- if the XSLT programmer intentionally puts there indentation code such as

<xsl:text>

</xsl:text>

the output will contain this indentation.

Dimitre Novatchev
Thank you for the detailed explanation :-)
infant programmer