RE: [xml] Inserting a DTD when creating a XML file



I might do something wrong so here is the code (using libxml2 - 2.4.5 on
IRIX 6.5):

If I do the following, the DTD is NOT inserted:

        doc = xmlNewDoc("1.0");
        xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "settingsfile",
NULL));
        xmlDtdPtr dtd = xmlParseDTD(NULL, "settings.dtd");
        dtd->name = xmlStrdup((xmlChar*)"settingsfile");
        doc->intSubset = dtd;
        if (doc->children == NULL) 
                xmlAddChild((xmlNodePtr)doc, (xmlNodePtr)dtd);
        else 
                xmlAddPrevSibling(doc->children, (xmlNodePtr)dtd);
        xmlSaveFormatFile(fname, doc, 1);

but if I do what Greg suggested, it works:

        doc = xmlNewDoc("1.0");
        xmlDocSetRootElement(doc, xmlNewDocNode(doc, NULL, "settingsfile",
NULL));
        xmlDtdPtr dtd = xmlParseDTD(NULL, "settings.dtd");
        dtd->name = xmlStrdup((xmlChar*)"settingsfile");
        doc->intSubset = dtd;   
        dtd->doc = doc;
        dtd->parent = doc;
        doc->children = (xmlNodePtr)dtd;
        dtd->next = xmlNewDocNode(doc, NULL, "settingsfile", NULL);
        xmlSaveFormatFile(fname, doc, 1);

although I get both the definition in the xml file and also the link to the
file:

<?xml version="1.0"?>             v  how to remove this?
<!DOCTYPE settingsfile SYSTEM "settings.dtd" [
<!ELEMENT settingsfile (date-created, ...

Yvan

-----Original Message-----
From: Greg Sjaardema [mailto:gdsjaar sandia gov]
Sent: Friday, January 25, 2002 8:28 AM
To: Yvan Paitel
Cc: 'veillard redhat com'; xml gnome org
Subject: Re: [xml] Inserting a DTD when creating a XML file


Yvan Paitel wrote:

If I save the document (using xmlSaveFile or xmlSaveFileFormat), the DTD
is
neither included nor referenced in the xml file. Any idea?

Thanks for your help,
Yvan

I do the following (C++):

  dtd = xmlParseDTD(NULL, dtd_path);
  if (dtd->name != NULL)
     xmlFree((char*)dtd->name);
  dtd->name = xmlStrdup(root_name);
  doc->intSubset = dtd;

  // Get rid of system id -- internal references only
  if (dtd->SystemID != NULL)
     xmlFree((char*)dtd->SystemID);
  dtd->SystemID = NULL;

  dtd->doc = doc;
  dtd->parent = doc;
  doc->children  = (xmlNodePtr)dtd;

  // Define and insert the root of the output document
  root = dtd->next = xmlNewDocNode(doc, NULL, root_name, NULL);

When the file is saved, it has the DTD embedded at the beginning of the
file.

--Greg



-----Original Message-----
From: Daniel Veillard [mailto:veillard redhat com]
Sent: Thursday, January 24, 2002 7:36 AM
To: darrell
Cc: ypaitel sofamordanek com; xml gnome org
Subject: Re: [xml] Inserting a DTD when creating a XML file

On Tue, Jan 22, 2002 at 09:17:44PM -0700, darrell wrote:
i am unsure if anyone is alowed to reply,

  Of course !

but this is an exerpt from the
faq, perhaps it is not what you are looking for?

It is also possible to simply add a Dtd to an existing document:

xmlDocPtr doc; /* your existing document */
        xmlDtdPtr dtd = xmlParseDTD(NULL, filename_of_dtd); /* parse the
DTD */
        dtd->name = xmlStrDup((xmlChar*)"root_name"); /* use the given
root */

        doc->intSubset = dtd;

  It can be a good idea to link the DTD as the first node in the doc
children list:
     if (doc->children == NULL)
         xmlAddChild((xmlNodePtr) doc, (xmlNodePtr) dtd);
     else
         xmlAddPrevSibling(doc->children, (xmlNodePtr) dtd);

Daniel

--
Daniel Veillard      | Red Hat Network https://rhn.redhat.com/
veillard redhat com  | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]