Page 1 of 1

Format and Indent options, preserving newlines in XML

Posted: Tue Jan 27, 2009 8:57 pm
by chrisbw
Hi,
I'm having trouble figuring out which Editor / Format / XML options I need to set/unset to preserve new line characters between tags.
Say I start with this code:

Code: Select all

<tag>
a1
b2
c3
</tag>
Ideally I'd like the result to look like this:

Code: Select all

<tag>
a1
b2
c3
</tag>
But what ends up happening is that the formatting tool gives me this:

Code: Select all

<tag> a1 b2 c3 </tag>
This is still valid XML but these changes actually make things difficult to edit and parse with non-XML aware tools like grep. So I'd like the formatting tool to do something, I don't want to preserve the text, but I don't want it to get rid of the new lines.
Is there an option for this?

Thanks

Re: Format and Indent options, preserving newlines in XML

Posted: Wed Jan 28, 2009 11:34 am
by sorin_ristache
Hello,

The formatting options available in Preferences / Editor / Format / XML cannot help you to preserve only some whitespaces (the newlines) inside a text content. There is an option for preserving the text content as is but that option either preserves the text of some XML elements in the same form or normalizes the whitespaces. The XML normalization process transforms a sequence of whitespace characters to only one space and the result is what you obtained:

Code: Select all

<tag> a1 b2 c3 </tag>
For preserving only some whitespace characters (in your case the newline characters) you need custom formatting with an XSLT stylesheet. You can develop an XSLT stylesheet that preserves the input XML document with the exception of the text nodes which are normalized according with your custom rules.


Regards,
Sorin

Re: Format and Indent options, preserving newlines in XML

Posted: Wed Jan 28, 2009 4:55 pm
by chrisbw
Thanks for the reply.
Could you recommend a tutorial or guide for doing that?

Re: Format and Indent options, preserving newlines in XML

Posted: Wed Jan 28, 2009 5:19 pm
by sorin_ristache
Hello,

You should look at the string manipulation functions. You can start here or here.


Regards,
Sorin