Anyone know of a good scala library to do whitespace removal/compaction from XML?
<foo> <bar>hello world</bar> <baz> xxx </baz> </foo>
to:
<foo><bar>hello world</bar><baz>xxx</baz></foo>
Attach a Filler node to the XML source node. Open the Filler node and use the field chooser to select the field with the unwanted spaces. Set Replace to Based on condition and set Condition to true.
In XML documents, there are two types of whitespace: Significant whitespace is part of the document content and should be preserved. Insignificant whitespace is used when editing XML documents for readability. These whitespaces are typically not intended for inclusion in the delivery of the document.
When xml:space is used on an element with a value of preserve , the whitespace in that element's content must be preserved as is by the application that processes it. The whitespace is always passed on to the processing application, but xml:space provides the application with a hint regarding how to process it.
As XML is case sensitive, xml:space must be declared using all lowercase letters. The value of xml:space applies to the element and any elements it contains unless overridden by the xml:space value of the contained elements.
scala.xml.Utility.trim() should do what you want:
scala> val x = <foo> | <bar>hello world</bar> | <baz> xxx </baz> | </foo> x: scala.xml.Elem = <foo> <bar>hello world</bar> <baz> xxx </baz> </foo> scala> scala.xml.Utility.trim(x) res0: scala.xml.Node = <foo><bar>hello world</bar><baz>xxx</baz></foo>
For whatever it's worth, this is what I've got going on now in the "roll my own" strategy:
def compactXml(xml: Node): Node = { (xml map { case Elem(prefix, label, attributes, scope, children @ _*) => { Elem(prefix, label, attributes, scope, children.map(compactXml(_)) :_*) } case Text(data) => Text(data.trim) case x => x }).first }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With