Re: [xml-bindings]Difficulties with Python bindings



On Sat, May 04, 2002 at 11:42:14PM +0100, Gary Benson wrote:
> 
> Hi,
> 
> I'm having a couple problems with the Python bindings, and wondered if 
> anyone could help.
> 
> Firstly, I'm trying to write something which works a little like a 
> webserver; all content starts as XML files which have xml-stylesheet PIs 
> to locate the stylesheets (with relative paths).
> 
> Some of the documents are served as static files, loaded via 
> libxml2.parseFile. When I load the stylesheet, with 
> libxslt.loadStylesheetPI, the relative path gets resolved relative to 
> wherever the original document was loaded from, which is what I wanted.
> 
> Some of the documents, however, are generated (with libxml2.newDoc, etc) 
> and these have no filename, so the stylesheet's location gets resolved 
> relative to the current directory, which breaks stuff. Is there any way I 
> can 'set the filename' of a generated document? I can get around it by 
> changing directory, but that strikes me as a bit of a hack.

  Hum, if it wasn't for a stylesheet PI found before the root element
then doing a setBase() on the document root element would do it.
  In that case the only way is to find a accessors for the URL field of
the xmlDoc structure.

  I see only 2 ways to do this:
   1/ change the behaviour of libxml2 xmlNodeSetBase() to change the
      URL field when the argument is a DOCUMENT
   2/ add r/w accessors to the URL fileds aocciated to the underlying structure.
      actually doc.name returns the value of that URL already

patch for 1/ enclosed

> Another problem is that I can't find an equivalent of xmlDocDumpMemory(), 

  In recent versions doc.serialize() will do this, check serialize.py
in recent versions

> so the only way to serialise a document is to save it to a file. I'm 
> currently hacking around it by redirecting stdout to a temporary file, 
> saving the document to it and then reading it back in, which is icky.

I recognize there is still a problem w.r.t. reusing the Python file
layer when they are not FILE * based. I would need to fix the I/O layer
to accept any kind of python file (probably by using callbacks to the
python level triggered by the C routine to be sure it works in all cases).

But your immediate need should be fullfilled by serialize() (it actually
work on any libxml2 node type).

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/
Index: tree.c
===================================================================
RCS file: /cvs/gnome/gnome-xml/tree.c,v
retrieving revision 1.205
diff -c -r1.205 tree.c
*** tree.c	20 Apr 2002 06:41:39 -0000	1.205
--- tree.c	5 May 2002 06:49:43 -0000
***************
*** 3670,3680 ****
          case XML_TEXT_NODE:
          case XML_CDATA_SECTION_NODE:
          case XML_COMMENT_NODE:
-         case XML_DOCUMENT_NODE:
          case XML_DOCUMENT_TYPE_NODE:
          case XML_DOCUMENT_FRAG_NODE:
          case XML_NOTATION_NODE:
-         case XML_HTML_DOCUMENT_NODE:
          case XML_DTD_NODE:
          case XML_ELEMENT_DECL:
          case XML_ATTRIBUTE_DECL:
--- 3670,3678 ----
***************
*** 3685,3697 ****
  	case XML_NAMESPACE_DECL:
  	case XML_XINCLUDE_START:
  	case XML_XINCLUDE_END:
- #ifdef LIBXML_DOCB_ENABLED
- 	case XML_DOCB_DOCUMENT_NODE:
- #endif
  	    return;
          case XML_ELEMENT_NODE:
          case XML_ATTRIBUTE_NODE:
  	    break;
      }
      
      ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);
--- 3683,3707 ----
  	case XML_NAMESPACE_DECL:
  	case XML_XINCLUDE_START:
  	case XML_XINCLUDE_END:
  	    return;
          case XML_ELEMENT_NODE:
          case XML_ATTRIBUTE_NODE:
  	    break;
+         case XML_DOCUMENT_NODE:
+ #ifdef LIBXML_DOCB_ENABLED
+ 	case XML_DOCB_DOCUMENT_NODE:
+ #endif
+         case XML_HTML_DOCUMENT_NODE: {
+ 	    xmlDocPtr doc = (xmlDocPtr) cur;
+ 
+ 	    if (doc->URL != NULL)
+ 		xmlFree((xmlChar *) doc->URL);
+ 	    if (uri == NULL)
+ 		doc->URL = NULL;
+ 	    else
+ 		doc->URL = xmlStrdup(uri);
+ 	    return;
+ 	}
      }
      
      ns = xmlSearchNsByHref(cur->doc, cur, XML_XML_NAMESPACE);


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