Re: [xsl] preserve structure of input XML file

Subject: Re: [xsl] preserve structure of input XML file
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 17 May 2011 11:35:32 -0400
Hi,

On 5/17/2011 11:04 AM, a kusa wrote:
I need to preserve all the whitespace and structure of an input XML
file. How do I do that in XSLT? I tried using fixed width fonts like
monospace but that does not work.

Hm. Unfortunately -- assuming you're talking about whitespace in a transformation result, not in how it's displayed -- trying to address this issue using fixed width fonts is like trying to ride a bike by wearing a red shirt. Riding a bike involves momentum, balance, and control of the bike, not color coordination.


As for the answer to your question, that depends on information you haven't given us. All other things being equal, an XSLT transformation will always preserve whitespace from the input XML, and can preserve exactly as much of the structure from it as you like.

But other things are rarely equal. Whitespace can be affected by how your input is parsed and processed (for example, if a schema is present and how it is being used) and how your output is being serialized (since sometimes people want whitespace added then), as well as by details of what the XSLT does.

Some basic rules of thumb if you want whitespace preserved:
  * Don't use MSXML or another processor known to discard whitespace
  * Don't use xsl:strip-space (which strips whitespace from the source)
    or xsl:output/@indent='yes' (which adds it in serialization)
  * Don't use a schema for your input, or if you do (in XSLT 2.0) make
    sure your processor is set to leave whitespace alone
  * Don't add whitespace in your templates

(Others might have things to add to this.)

As for the structure of your XML, how much of it is preserved depends entirely on which elements in the input you are matching and what you are doing with them.

So it's not really a simple question. It is probably best addressed here by giving a small demonstration example, with a specification of the desired result.

Cheers,
Wendell

======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread