Re: [xml] Bug or expected behaviour with xmlNewChild



On Wed, Jul 31, 2013 at 08:32:01PM +0100, Neil Barnes wrote:
This one is really confusing me... I'm the first to admit I'm not at
all sure what I'm doing, but:

I'm trying to create an xhtml file as part of an epub. (I've already
successfully exported an html and imported an xml file into the
program, and I'm getting no error messages.)

My google-fu is weak on this one; I can find nothing relevant in
either the archive or generally on the net.

What I want:

<head>

<title>Unknown</title>

<link href="../Styles/stylesheet.css" rel="stylesheet" type="text/css" />

<link href="../Styles/page_styles.css" rel="stylesheet" type="text/css" />

</head>


  ouch ...

and I cannot find a way to make the link elements terminate.


Code:
doc = htmlNewDoc (NULL, NULL);

  No.
http://www.w3.org/TR/xhtml1/#strict

First XHTML is *XML* , so xmlNewDoc() must be called.

You need the doctype declaration,
Libxml2 serializer will check for those and use XHTML1 serialization
mode if you set up the DOCTYPE as expected, assuming transitional:

  xmlNewDtd(doc, "-//W3C//DTD XHTML 1.0 Transitional//EN",
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";);


root_node = xmlNewNode (NULL, BAD_CAST "html");
xmlNewProp (root_node, BAD_CAST "xmlns", BAD_CAST
"http://www.w3.org/1999/xhtml";);

  No, a anamespace node declaration is not an attribute in libxml2
  xmlNsPtr ns = xmlNewNs(root_node, "http://www.w3.org/1999/xhtml";, NULL);
  xmlSetNs(root_node, ns);

xmlDocSetRootElement (doc, root_node);

node = xmlNewChild (root_node, NULL, BAD_CAST "head", NULL);

  no, you want the new node to be in the same default namespace as the
html parent so 

  node = xmlNewChild (root_node, ns, BAD_CAST "head", NULL);
   
etc ...

node1 = xmlNewChild (node, NULL, BAD_CAST "title", NULL);

xmlNodeAddContent (node1, BAD_CAST "Title");

node1 = xmlNewChild (node, NULL, BAD_CAST "plink", NULL);
xmlNewProp (node1, BAD_CAST "href", BAD_CAST "../Styles/stylesheet.css");
xmlNewProp (node1, BAD_CAST "rel", BAD_CAST "stylesheet");
xmlNewProp (node1, BAD_CAST "type", BAD_CAST "text/.css");

node1 = xmlNewChild (root_node, NULL, BAD_CAST "body", NULL);
// and so on...


This produces:

  how ? I don't know what routine you called ?

Daniel


-- 
Daniel Veillard      | Open Source and Standards, Red Hat
veillard redhat com  | libxml Gnome XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | virtualization library  http://libvirt.org/


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