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

XML Document Type Declaration

0 views
Skip to first unread message

Charles Heizer

unread,
Feb 18, 2006, 1:26:40 PM2/18/06
to
Hello,
I'm just starting to create and work with xml using tdom and I wanted
to know, do I need to specify document type when creating my xml docs?
If I do, I don't see a tdom arg to create it, or when I write my xml
out to a file should I make it my first line?

thanks,
- Charles

Rolf Ade

unread,
Feb 19, 2006, 3:56:12 PM2/19/06
to
In article <1140287200....@g44g2000cwa.googlegroups.com>,

Charles Heizer wrote:
>I'm just starting to create and work with xml using tdom and I wanted
>to know, do I need to specify document type when creating my xml docs?

The XML recommendation is clear about that: The document type
declaration is optional. tDOM doesn't need one; if the document is
well-formed, it will parse your data, even without document type
declaration.

If you create XML, to deliver that XML to an external
system/application, then that system may require one.

>If I do, I don't see a tdom arg to create it, or when I write my xml
>out to a file should I make it my first line?

If you want (or need) one, just [puts] it to your output channel
before you do the [puts .. [$doc asXML ..]].

rolf


Donal K. Fellows

unread,
Feb 19, 2006, 5:03:48 PM2/19/06
to
Rolf Ade wrote:
> The XML recommendation is clear about that: The document type
> declaration is optional. tDOM doesn't need one; if the document is
> well-formed, it will parse your data, even without document type
> declaration.

The main thing the declaration helps with is determining the encoding of
a stream of bytes. But if you already know the encoding (or have a
unicode string, such as Tcl's strings) then the XML declaration has next
to no practical value. Assuming you're not using XML 1.1 or later. ;-)

> If you create XML, to deliver that XML to an external
> system/application, then that system may require one.

If you're writing an XML _document_, it's a good idea to put the XML
declaration in (though I'd suggest omitting the <!DOCTYPE if you can;
DTDs are seriously out of fashion[*]). If you're just writing an element
fragment, leave the declaration out.

Donal.
[* Both XML-Schema and RELAX-NG are better than a DTD, especially for
XML with namespaces... ]

Rolf Ade

unread,
Feb 19, 2006, 6:39:17 PM2/19/06
to
Donal K. Fellows wrote:
>Rolf Ade wrote:
>> The XML recommendation is clear about that: The document type
>> declaration is optional. tDOM doesn't need one; if the document is
>> well-formed, it will parse your data, even without document type
>> declaration.
>
>The main thing the declaration helps with is determining the encoding of
>a stream of bytes. But if you already know the encoding (or have a
>unicode string, such as Tcl's strings) then the XML declaration has next
>to no practical value. Assuming you're not using XML 1.1 or later. ;-)

Don't confuse the XML declaration with the document type
declaration. You're right, the main thing about the XML declaration is
to help with determining the encoding of a stream of bytes, while
reading. The original poster asked about writing the document type
declartion.

>> If you create XML, to deliver that XML to an external
>> system/application, then that system may require one.
>
>If you're writing an XML _document_, it's a good idea to put the XML
>declaration in (though I'd suggest omitting the <!DOCTYPE if you can;
>DTDs are seriously out of fashion[*]). If you're just writing an element
>fragment, leave the declaration out.

To refine that a bit: if you're writing an XML document in UTF-8
encoding, then the XML declaration is optional. If you write your
document in another encoding, it is not only a good idea but strongly
recommended (and often (depending on the document content) absolutely
necessary) that you write an XML declaration.

rolf

Charles Heizer

unread,
Feb 20, 2006, 6:25:06 PM2/20/06
to
Thanks, for the info. I think it sounds like good practice to just put
it in.

Thanks again,
charles

Donal K. Fellows

unread,
Feb 20, 2006, 6:26:01 PM2/20/06
to
Rolf Ade wrote:
> Don't confuse the XML declaration with the document type
> declaration. You're right, the main thing about the XML declaration is
> to help with determining the encoding of a stream of bytes, while
> reading. The original poster asked about writing the document type
> declartion.

Fair point, except I'd go as far as saying that a large majority of XML
documents don't have DTDs and never will have. This is because of the
way that everyone is switching to using namespaces, which really don't
work with DTDs at all well. Only put a DTD in if some important document
consumer absolutely requires it.

> To refine that a bit: if you're writing an XML document in UTF-8
> encoding, then the XML declaration is optional. If you write your
> document in another encoding, it is not only a good idea but strongly
> recommended (and often (depending on the document content) absolutely
> necessary) that you write an XML declaration.

Well, that depends purely on whether you have a byte-oriented or a
character-oriented transport. If you're lucky enough to have the latter
(e.g. a Tcl string or something with accurate MIME types) then the only
thing that the XML decl buys you is more waste. :-)

Donal.

0 new messages