Re: [xml] Bug or expected behaviour with xmlNewChild



On 06/08/13 09:28, Daniel Veillard wrote:
Ah, okay ! Just that it would be good to have something with setting a default namespace and how to express XHTML as people seems often confused by that :-)
There will probably be one or two errors remaining (or at least, not
best practice) which you can put down to my lack of knowledge of the
format. Naturally, *both* my O'Reilly 'XML' books are currently
hiding from me...
  hehe !

Daniel


Daniel - try this. It compiles clean (except with -Wall which complains 'warning: variable ‘dtd’ set but not used [-Wunused-but-set-variable]'

As above - it's bound to be missing something, but if it helps, feel free.

Neil

----------------------------------------------------------
Output is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml";>
<head>
<title>Title</title>
<link href="../Styles/stylesheet.css" rel="stylesheet" type="text/.css"/>
<link href="../Styles/page_styles.css" rel="stylesheet" type="text/.css"/>
</head>
<body class="ocrpr">
<h1 class="title">Richard III</h1>
<h2 class="act">Act I</h2>
<h3 class="scene">Scene 1: London; a street</h3>
<p class="stage_direction">Enter GLOUCESTER, <i>solus</i></p>
<h4 class="actor">GLOUCESTER</h4>
<p class="body_text">Now is the winter of our discontent</p>
<p class="body_text">Made glorious summer by this sun of York;</p>
<p class="body_text">...</p>
</body>
</html>


----------------------------------------------------------

// output a document in xhtml format, with the necessary structures for epub
// a single file is generated for temporary storage; for export as epub it is // split into separate files for each page break (i.e. chapter and subtitles)
// it includes the default styles
//
// compile with: gcc -Wall -g -o xhtmlwrite xhtmlwrite.c `xml2-config --cflags` `xml2-config --libs` -export-dynamic
//
// Author: Neil Barnes - neil nailed-barnacle co uk
//
// based on tree2.c (and a little Shakespeare)


#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>

int main (int argc, char * * argv)
{

xmlNodePtr root_node = NULL, node = NULL, node1 = NULL, node2 = NULL, node3 = NULL;
xmlDocPtr doc;
xmlDtdPtr dtd = NULL; /* DTD pointer */

// create the initial doc
doc = xmlNewDoc(BAD_CAST "1.0");
root_node = xmlNewNode(NULL, BAD_CAST "html");
xmlDocSetRootElement(doc, root_node);
// we need the default dtd
dtd = xmlCreateIntSubset(doc, BAD_CAST "html", BAD_CAST "-//W3C//DTD XHTML 1.1//EN", BAD_CAST "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";); xmlNewProp (root_node, BAD_CAST "xmlns", BAD_CAST "http://www.w3.org/1999/xhtml";);

// there's a head and a body section
// build the head
// <html>
// <head>
// <title>Title<title>
// <link href="../Styles/stylesheet.css" rel="stylesheet" type="text/css" />
// <link href="../Styles/page_styles.css" rel="stylesheet" type="text/css" />
// </head>
// <body>
// stuff...
// </body>
// </html>

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

// add a 'title' section and some default data
node1 = xmlNewChild (node, NULL, BAD_CAST "title", NULL);
xmlNodeAddContent (node1, BAD_CAST "Title");

node1 = xmlNewChild (node, NULL, BAD_CAST "link", 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 (node, NULL, BAD_CAST "link", NULL);
xmlNewProp (node1, BAD_CAST "href", BAD_CAST "../Styles/page_styles.css");
xmlNewProp (node1, BAD_CAST "rel", BAD_CAST "stylesheet");
xmlNewProp (node1, BAD_CAST "type", BAD_CAST "text/.css");

// now the body, and a little metadata
node1 = xmlNewChild (root_node, NULL, BAD_CAST "body", NULL);
xmlNewProp (node1, BAD_CAST "class", BAD_CAST "ocrpr");

// a header/title; the classes are chosen to suit your css
node2 = xmlNewChild(node1, NULL, BAD_CAST "h1", BAD_CAST "Richard III");
xmlNewProp(node2, BAD_CAST "class", BAD_CAST "title");

// another
node2 = xmlNewChild(node1, NULL, BAD_CAST "h2", BAD_CAST "Act I");
xmlNewProp(node2, BAD_CAST "class", BAD_CAST "act");

// and another
node2 = xmlNewChild(node1, NULL, BAD_CAST "h3", BAD_CAST "Scene 1: London; a street");
xmlNewProp(node2, BAD_CAST "class", BAD_CAST "scene");

node2 = xmlNewChild(node1, NULL, BAD_CAST "p", BAD_CAST "Enter GLOUCESTER, ");
xmlNewProp(node2, BAD_CAST "class", BAD_CAST "stage_direction");

node3 = xmlNewChild(node2, NULL, BAD_CAST "i", BAD_CAST "solus");

node2 = xmlNewChild(node1, NULL, BAD_CAST "h4", BAD_CAST "GLOUCESTER");
xmlNewProp(node2, BAD_CAST "class", BAD_CAST "actor");

node2 = xmlNewChild(node1, NULL, BAD_CAST "p", BAD_CAST "Now is the winter of our discontent");
xmlNewProp(node2, BAD_CAST "class", BAD_CAST "body_text");

node2 = xmlNewChild(node1, NULL, BAD_CAST "p", BAD_CAST "Made glorious summer by this sun of York;");
xmlNewProp(node2, BAD_CAST "class", BAD_CAST "body_text");

node2 = xmlNewChild(node1, NULL, BAD_CAST "p", BAD_CAST "...");
xmlNewProp(node2, BAD_CAST "class", BAD_CAST "body_text");

xmlSaveFormatFileEnc("shakespeare.xhtml", doc, "UTF-8", 1);
xmlFreeDoc(doc);
xmlCleanupParser();

return (0);
}



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