Format and Indent options, preserving newlines in XML

Having trouble installing Oxygen? Got a bug to report? Post it all here.
chrisbw
Posts: 2
Joined: Tue Jan 27, 2009 8:40 pm

Format and Indent options, preserving newlines in XML

Post 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
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Format and Indent options, preserving newlines in XML

Post 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
chrisbw
Posts: 2
Joined: Tue Jan 27, 2009 8:40 pm

Re: Format and Indent options, preserving newlines in XML

Post by chrisbw »

Thanks for the reply.
Could you recommend a tutorial or guide for doing that?
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Format and Indent options, preserving newlines in XML

Post by sorin_ristache »

Hello,

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


Regards,
Sorin
Post Reply