Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to write ascii control characters (line feed) in the xml document using tclxml/libxml2 parser

945 views
Skip to first unread message

Divyesh Patel

unread,
Oct 3, 2013, 12:13:50 AM10/3/13
to


Hi everyone,

I want to insert a line feed character in the xml document and it is an ASCII control character having value:


But when I tried to write it using createTextNode API it always writes & #10; instead of 

I think it is considering & as entity reference.

using following code
set data [ ::dom::document createElement $cell "Data" ]
::dom::document createTextNode $data "testing
line feed"
e.g.
i want following output
<Cell>
<Data>testing&#10;line feed</Data>
</Cell>

but it gives as
<Cell>
<Data>testing&amp;#10;line feed</Data>
</Cell>

I want to write it as the value of the text element and not in the CDATA scope.

Please help me how to do that.

I am using TCLXML/Libxml2 and version: 3.2

Thanks
Divyesh

Rich

unread,
Oct 3, 2013, 6:31:26 AM10/3/13
to
Divyesh Patel <divyes...@gmail.com> wrote:


: Hi everyone,

: I want to insert a line feed character in the xml document and it is an ASCII control character having value:&#10;

: ...
: ::dom::document createTextNode $data "testing&#10;line feed"

Did you try ::dom::document createTextNode $data "testing\nline feed"?

You are not writing a literal linefeed, you are asking it to insert the 5
characters & # 1 0 ;.

And when a proper XML node sees a &, it is supposed to convert that the
&amp;. Which is why you get the extra &amp;.

Divyesh Patel

unread,
Oct 3, 2013, 11:42:33 PM10/3/13
to
Hi Rich,
I have done that exercise.

For \r it is putting &#13; but for \n it simply puts new line in xml document instead of putting &#10;

Thanks
Divyesh

Andreas Leitgeb

unread,
Oct 4, 2013, 5:31:03 AM10/4/13
to
Divyesh Patel <divyes...@gmail.com> wrote:
> On Thursday, 3 October 2013 16:01:26 UTC+5:30, Rich wrote:
>> Divyesh Patel <divyes...@gmail.com> wrote:
>> : Hi everyone,
>> : I want to insert a line feed character in the xml document and it is an ASCII control character having value:&#10;
>> : ...
>> : ::dom::document createTextNode $data "testing&#10;line feed"
>> Did you try ::dom::document createTextNode $data "testing\nline feed"?
> Hi Rich,
> I have done that exercise.
> For \r it is putting &#13; but for \n it simply puts new line in xml document instead of putting &#10;
> Thanks
> Divyesh

What Rich and Donal seem to be trying to tell you is, that a literal linefeed
character in the resulting xml is really the correct thing.

Why exactly do you even want the &#10; sequence? Is your XML going to
be parsed by something else than a proper xml parser? In that case you
might indeed need "something else than a proper xml generator." :)

Rich

unread,
Oct 4, 2013, 7:33:27 AM10/4/13
to
Divyesh Patel <divyes...@gmail.com> wrote:
Please edit out the extra blank lines that Google Groups adds to your
posting before you submit your reply (note how the formatting is above).

You are being very unclear in exactly what it is you want.

When you say "I want to insert a line feed character" that means you want a
single byte to be inserted, and that byte to have the value 10 (decimal).

Your reply above indicates that when you use "\n" you get "a single byte ...
inserted, and that byte ... has the value 10 (decimal)". Which means you
are currently receiving just exactly what you are asking for.

But the end of your sentence: "instead of putting &#10;" implies that you
want something other than "a single byte with the value 10 (decimal)" to be
inserted.

It implies that what you actually want to ask is:

"How to I get newlines inserted into an XML document to actually be encoded
as an XML character escape?"

Which is a completely different question.

Likely the reason why \n is not encoded is that a byte with a value of 10
(decimal) is a legal (and recommended by the standard) way to insert
newlines into an XML documents. So that means if you really do want the
newline bytes encoded as xml character escapes, you will have to do that
encoding yourself.

One way to do it is to filter your whole xml document through [string map]
when you save/send it somewhere, and ask [string map] to change all the
"bytes with value 10" into "strings with value '&#10;'".

I.e., like so:

set encoded_xml [ string map [ list \n "&#10;" ] $raw_xml ]

0 new messages