Discussion:
[XOM-interest] Preserve white space in text nodes
Stevenson, Todd (GE Healthcare)
2012-01-18 19:06:59 UTC
Permalink
I can't figure out how to preserve whitespace in a text node.

I use the serializer to write my output and it strips the spaces and newline characters.

Is there a configuration option to make this work correctly?

Thanks.
Elliotte Rusty Harold
2012-01-18 20:57:08 UTC
Permalink
On Wed, Jan 18, 2012 at 2:06 PM, Stevenson, Todd (GE Healthcare)
Post by Stevenson, Todd (GE Healthcare)
I can't figure out how to preserve whitespace in a text node.
I use the serializer to write my output and it strips the spaces and newline characters.
Is there a configuration option to make this work correctly?
Do you have code that demonstrates what's happening?
--
Elliotte Rusty Harold
elharo at ibiblio.org
Stevenson, Todd (GE Healthcare)
2012-01-18 22:33:29 UTC
Permalink
This method:

import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;

void createXML() throws IOException {

Element root = new Element("Root");
Element child = new Element("Child");

child.appendChild("\n first line \n second line\n");
root.appendChild(child);

Document doc = new Document(root);

Serializer serializer = new Serializer(System.out, "ISO-8859-1");
serializer.setIndent(4);
serializer.setMaxLength(100);
serializer.write(doc);
}

Generates this output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Root>
<Child> first line second line </Child>
</Root>


Note that it discards the extra white space and newline characters. I want to be able to preserve this whitespace to the output XML document.

Thanks

-----Original Message-----
From: xom-interest-bounces at lists.ibiblio.org [mailto:xom-interest-bounces at lists.ibiblio.org] On Behalf Of Elliotte Rusty Harold
Sent: Wednesday, January 18, 2012 1:57 PM
To: XOM API for Processing XML with Java
Subject: Re: [XOM-interest] Preserve white space in text nodes
Post by Stevenson, Todd (GE Healthcare)
I can't figure out how to preserve whitespace in a text node.
I use the serializer to write my output and it strips the spaces and newline characters.
Is there a configuration option to make this work correctly?
Do you have code that demonstrates what's happening?


--
Elliotte Rusty Harold
elharo at ibiblio.org
Christophe Marchand
2012-01-19 07:35:57 UTC
Permalink
This works correctly for me.

Best regards,
Christophe
Post by Stevenson, Todd (GE Healthcare)
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
void createXML() throws IOException {
Element root = new Element("Root");
Element child = new Element("Child");
child.addAttribute(new Attribute("xml:space", XMLConstants.XML_NS_URI,
"preserve"));
Post by Stevenson, Todd (GE Healthcare)
child.appendChild("\n first line \n second line\n");
root.appendChild(child);
Document doc = new Document(root);
Serializer serializer = new Serializer(System.out, "ISO-8859-1");
serializer.setIndent(4);
serializer.setMaxLength(100);
serializer.write(doc);
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<Root>
<Child> first line second line</Child>
</Root>
Note that it discards the extra white space and newline characters. I want to be able to preserve this whitespace to the output XML document.
Thanks
-----Original Message-----
From: xom-interest-bounces at lists.ibiblio.org [mailto:xom-interest-bounces at lists.ibiblio.org] On Behalf Of Elliotte Rusty Harold
Sent: Wednesday, January 18, 2012 1:57 PM
To: XOM API for Processing XML with Java
Subject: Re: [XOM-interest] Preserve white space in text nodes
Post by Stevenson, Todd (GE Healthcare)
I can't figure out how to preserve whitespace in a text node.
I use the serializer to write my output and it strips the spaces and newline characters.
Is there a configuration option to make this work correctly?
Do you have code that demonstrates what's happening?
--
Elliotte Rusty Harold
elharo at ibiblio.org
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
Christophe Marchand
2012-01-19 08:03:03 UTC
Permalink
hum... colors are not preserved...
See code added in yours.
Christophe
Post by Christophe Marchand
This works correctly for me.
Best regards,
Christophe
Post by Stevenson, Todd (GE Healthcare)
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
void createXML() throws IOException {
Element root = new Element("Root");
Element child = new Element("Child");
child.addAttribute(new Attribute("xml:space", XMLConstants.XML_NS_URI,
"preserve"));
Post by Stevenson, Todd (GE Healthcare)
child.appendChild("\n first line \n second line\n");
root.appendChild(child);
Document doc = new Document(root);
Serializer serializer = new Serializer(System.out, "ISO-8859-1");
serializer.setIndent(4);
serializer.setMaxLength(100);
serializer.write(doc);
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<Root>
<Child> first line second line</Child>
</Root>
Note that it discards the extra white space and newline characters. I want to be able to preserve this whitespace to the output XML document.
Thanks
-----Original Message-----
From: xom-interest-bounces at lists.ibiblio.org [mailto:xom-interest-bounces at lists.ibiblio.org] On Behalf Of Elliotte Rusty Harold
Sent: Wednesday, January 18, 2012 1:57 PM
To: XOM API for Processing XML with Java
Subject: Re: [XOM-interest] Preserve white space in text nodes
Post by Stevenson, Todd (GE Healthcare)
I can't figure out how to preserve whitespace in a text node.
I use the serializer to write my output and it strips the spaces and newline characters.
Is there a configuration option to make this work correctly?
Do you have code that demonstrates what's happening?
--
Elliotte Rusty Harold
elharo at ibiblio.org
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
_______________________________________________
XOM-interest mailing list
XOM-interest at lists.ibiblio.org
http://lists.ibiblio.org/mailman/listinfo/xom-interest
Elliotte Rusty Harold
2012-01-19 11:21:39 UTC
Permalink
On Wed, Jan 18, 2012 at 5:33 PM, Stevenson, Todd (GE Healthcare)
Post by Stevenson, Todd (GE Healthcare)
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Serializer;
? ?void createXML() throws IOException {
? ? ? ?Element root = new Element("Root");
? ? ? ?Element child = new Element("Child");
? ? ? ?child.appendChild("\n ? ? ? ? ?first line ? \n ? ? ? ? ?second line\n");
? ? ? ?root.appendChild(child);
? ? ? ?Document doc = new Document(root);
? ? ? ?Serializer serializer = new Serializer(System.out, "ISO-8859-1");
? ? ? ?serializer.setIndent(4);
? ? ? ?serializer.setMaxLength(100);
This is your problem. When you use setIndent and/or setMaxLength you
are telling the serializer to pretty print. I.e. you are telling it
that it should modify the white space so you have a certain line
length and indenting. If you want to preserve white space, don't do
that. Absent instructions to the contrary, XOM will preserve white
space in text nodes.
--
Elliotte Rusty Harold
elharo at ibiblio.org
Michael Kay
2012-01-19 12:27:26 UTC
Permalink
Post by Elliotte Rusty Harold
This is your problem. When you use setIndent and/or setMaxLength you
are telling the serializer to pretty print. I.e. you are telling it that
it should modify the white space so you have a certain line length and
indenting. If you want to preserve white space, don't do that. Absent
instructions to the contrary, XOM will preserve white space in text nodes.

The message here seems to be that XOM indentation is more aggressive
than XSLT indentation, which only adds whitespace before or after start
and end tags.

Michael Kay
Saxonica
Stevenson, Todd (GE Healthcare)
2012-01-19 15:24:09 UTC
Permalink
Thanks all. Problem fixed.

-----Original Message-----
From: xom-interest-bounces at lists.ibiblio.org [mailto:xom-interest-bounces at lists.ibiblio.org] On Behalf Of Michael Kay
Sent: Thursday, January 19, 2012 5:27 AM
To: xom-interest at lists.ibiblio.org
Subject: Re: [XOM-interest] Preserve white space in text nodes
This is your problem. When you use setIndent and/or setMaxLength you are telling the serializer to pretty print. I.e. you are telling it that it should modify the white space so you have a certain line length and indenting. If you want to preserve white space, don't do that. Absent instructions to the contrary, XOM will preserve white space in text nodes.
The message here seems to be that XOM indentation is more aggressive than XSLT indentation, which only adds whitespace before or after start and end tags.

Michael Kay
Saxonica
Olivier Lefevre
2012-01-21 14:09:15 UTC
Permalink
Post by Michael Kay
The message here seems to be that XOM indentation is more aggressive
than XSLT indentation, which only adds whitespace before or after start
and end tags.
Yes but I would argue that it's more in line with common expectations of
a pretty printer: you don't want odd and weird indents in the output, no
matter where they came from, and respecting white space in text nodes
might cause just that.

-- O.L.

Loading...