XML::LibXML, newlines in nodes, and entities...

J

Jay McGavren

I'm currently using XML::LibXML for a project. I need to include
newlines in an XML node, but my target user's parser just ignores them.
So I'm trying to use an entity instead, but the entity is just being
treated as plain text.

Here's some simplified sample code:

use XML::LibXML;
$document = XML::LibXML::Document->new('1.0', 'ISO-8859-1');
$document->setDocumentElement($document->createElement('Data'));
$document->documentElement->appendTextChild('test', 'line 1
line
2');
print $document->toString();

And here's my output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Data><test>line 1&amp;#xA;line 2</test></Data>

....The ampersand that is supposed to start the entity winds up becoming
an entity itself.

I've perused the XML::LibXML documentation looking for an appropriate
method, but it's a like seeking a needle in a haystack. Can anyone
give me a pointer in the right direction?
 
J

John Bokma

Jay McGavren said:
I'm currently using XML::LibXML for a project. I need to include
newlines in an XML node, but my target user's parser just ignores them.
So I'm trying to use an entity instead, but the entity is just being
treated as plain text.

Here's some simplified sample code:

use XML::LibXML;
$document = XML::LibXML::Document->new('1.0', 'ISO-8859-1');
$document->setDocumentElement($document->createElement('Data'));
$document->documentElement->appendTextChild('test', 'line 1
line
2');
print $document->toString();

And here's my output:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Data><test>line 1&amp;#xA;line 2</test></Data>

...The ampersand that is supposed to start the entity winds up becoming
an entity itself.

you use &amp;#10; not
?
 
A

A. Sinan Unur

you use &amp;#10; not
?

But that results in

D:\Home\asu1> ttt
<?xml version="1.0" encoding="ISO-8859-1"?>
<Data><test>line 1&amp;amp#10;line2</test></Data>

I have never used LibXML, so I am not sure if this is intended behavior,
but it sure is unexpected.

Sinan
 
J

John Bokma

A. Sinan Unur said:
But that results in

D:\Home\asu1> ttt
<?xml version="1.0" encoding="ISO-8859-1"?>
<Data><test>line 1&amp;amp#10;line2</test></Data>

I have never used LibXML, so I am not sure if this is intended behavior,
but it sure is unexpected.

I didn't read it right (the example), and thought the OP used &amp;#xA;
in the first place, apologies.

Found this though:
<http://search.cpan.org/~phish/XML-LibXML-
1.58/lib/XML/LibXML/Document.pod>

createEntityReference

my $entref = $doc->createEntityReference($refname);

If a document has a DTD specified, one can create entity references
by using this function. If one wants to add a entity reference to the
document, this reference has to be created by this function.

An entity reference is unique to a document and cannot be passed to
other documents as other nodes can be passed.

NOTE: A text content containing something that looks like an entity
reference, will not be expanded to a real entity reference unless it is
a predefined entity

my $string = "&foo;";
$some_element->appendText( $string );
print $some_element->textContent; # prints "&amp;foo;"
^^^^^^^^^
so my best guess is (I don't have libxml installed), to create an entity
reference.
 
A

A. Sinan Unur

I didn't read it right (the example), and thought the OP used
&amp;#xA; in the first place, apologies.
....

NOTE: A text content containing something that looks like an
entity reference, will not be expanded to a real entity
reference unless it is a predefined entity

That makes a lot of sense actually. My lack of knowledge of XML is
showing. Thanks for the information.

Sinan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top