Re: [xml] xsl:copy and line breaks between attributes



On 01.06.2004 13:06, Oliver Kiddle wrote:
I'm using a stylesheet to make a very small number of changes to some
files. So The vast majority of things go through a template which
matches "@*|node()" and uses xsl:copy. The files are edited by humans so
they are nicely indented and I don't want any incidental changes in the
whitespace.

The problem is that where there are line breaks between attributes in an
XML element, xsl:copy puts the whole XML element onto one line. So
  <foo
    val="bar>
becomes
  <foo val="bar">

I appreciate that this isn't a bug but it makes xsltproc all but useless
in this situation.  I don't suppose it would be possible to make xsl:copy
preserve whitespace within elements?

No, sorry. The space between attributes within a tag is insignificant and I believe that none of this world's processors preserve it. It is lost allready during parsing, before a XSLT processor even sees the thing.

Here is a document with inter-attribute spaces, just like yours:

  C:\Temp>type attispace.xml
  <?xml version="1.0"?>
  <doc>
    <node attone="valone"
          atttwo="valtwo">
      text
    </node>
  </doc>

Here is what xsltproc makes out of it using a classic identity stylesheet:

  C:\Temp>xsltproc identity.xsl attispace.xml
  <?xml version="1.0"?>
  <doc>
    <node attone="valone" atttwo="valtwo">
      text
    </node>
  </doc>

That is exactly what bothers you, no? Most unfortunate, I see Xalan doing the same:

  C:\Temp>xalan -xsl identity.xsl -in attispace.xml
  <?xml version="1.0" encoding="UTF-8"?>
  <doc>
    <node attone="valone" atttwo="valtwo">
      text
    </node>
  </doc>

I have no other processors here I could try this with, but I believe, without claim, all of them will give the same output. I admit, what these two processors above did to a hand-crafted file isn't very nice, but the fact remains that they did exactly what they should and broke no rules. In particular, they lost no XML-relevant data.

You can use some stream editing tool (sed, or its like) and try to edit a file with that. It may or may not be possible to do, depends on the nature of your edits. This isn't beautiful, but if the editing you want to perform is minor enough, it could save you manual labour with a text editor.

Ciao,
Igor



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]