From: <dg...@mo...> - 2004-09-15 08:47:25
|
Hello, Ok I modified the the file dom.tcl inside TclDom3.0 in order to (get|set|remove)Attributes: Here is the diff: $ diff /d/home/dgroth/cvs/tclhttpd/customlib/tcldom3.0/dom.tcl /d/tcl-lib/tcldo m3.0/dom.tcl 1211,1222d1210 < *Attribute { < switch $node(node:nodeType) { < textNode {} < default { < if {[llength $args] == 1} { < set result [dom::element $method $token [lindex $args 0]] < } else { < set result [dom::element $method $token [lindex $args 0] [lindex $args 1]] < } < } < } < } Might add something for (get|set|remove)AttributeNS too. Now the following works for TclDom/tcl: % lappend auto_path d:/home/dgroth/cvs/tclhttpd/customlib D:/tcl-programme/tclkit/tclkit8.4.7-mingw.exe/lib/tcl8.4 D:/tcl-programme/tclkit/tclkit8.4.7-mingw.exe/lib D:/tcl-programme/tclkit/tclkit8.4.7-mingw.exe/lib/tk8.4 d:/home/dgroth/cvs/tclhttpd/customlib (tclkit) 2 % package require dom 3.0 (tclkit) 3 % set doc [dom::DOMImplementation create] ::dom::tcl::document1::Document (tclkit) 4 % set top [$doc createElement html] ::dom::tcl::document1::node2 (tclkit) 5 % set head [$doc createElement head] ::dom::tcl::document1::node6 (tclkit) 6 % $top appendChild $head ::dom::tcl::document1::node6 (tclkit) 7 % set title [$doc createElement title] ::dom::tcl::document1::node13 (tclkit) 8 % $head appendChild $title ::dom::tcl::document1::node13 (tclkit) 9 % $title appendChild [$doc createTextNode "MPIMG Berlin - Mouse Database"] ::dom::tcl::document1::node20 (tclkit) 10 % set body [$doc createElement body] ::dom::tcl::document1::node30 (tclkit) 11 % $top appendChild $body ::dom::tcl::document1::node30 (tclkit) 12 % $body setAttribute bgcolor black black (tclkit) 13 % $body getAttribute bgcolor black (tclkit) 14 % dom::serialize $doc <?xml version='1.0'?> <!DOCTYPE html> <html><head><title>MPIMG Berlin - Mouse Database</title></head><body bgcolor="black"/></html> Looks good. Should I add this to the feature requests? regards, Detlef > >On 15/09/2004, at 12:37 AM, Detlef Groth wrote: > >> I like the itcl/tk- and Tdom-like calling conventions for tdom: > >TclDOM version 3.0 has introduced that style of API, but not uniformly >throughout the package (yet). > >At the time that I was implementing this feature I had an issue with >overloading the document/node commands with methods for multiple node >classes. For example, an element node is both a Node and an Element >class object, so should have the methods for both the ::dom::node and >::dom::element commands. However, a TextNode or a Document node will >have a different set a methods. > >> (tclkit) 82 % set doc [dom::DOMImplementation create] >> ::dom::tcl::document7::Document >> (tclkit) 83 % set top [dom::document createElement $doc html] >> ::dom::tcl::document7::node2 >> (tclkit) 84 % set title [$doc createElement title] >> ::dom::tcl::document7::node6 >> (tclkit) 85 % $top appendChild $title >> ::dom::tcl::document7::node6 >> (tclkit) 86 % set body [$doc createElement body] >> ::dom::tcl::document7::node13 >> (tclkit) 87 % $top appendChild $body >> ::dom::tcl::document7::node13 >> (tclkit) 88 % $body setAttribute bgcolor black >> unknown method "setAttribute" >> >> This does not work, however this: >> >> (tclkit) 90 % dom::element setAttribute $body bgcolor black >> black > >Yes, that exactly the issue raised above. "setAttribute" is a method >of the ::dom::element command. You'd like to be able to do: > >$body setAttribute bgcolor black > >What happens if you do: > >$doc setAttribute bgcolor black > >The latter should probably throw an exception. > >Making the former work would mean either defining the node command as a >separate function and implementing every method from every node class >command (for both Tcl and libxml2), or modifying the node command to >call the class-specific function if the method is not defined >(actually, the latter would be the best approach). > >> which is for me a little bit awkward, and I always forget to add the >> nodename after the (get|set)Attribute-command. I am used to javascript >> where we say something like: >> >> var input = window.document.createElement("input"); >> input.setAttribute("name", "inputvalue"); > >Having to remember to add the node token does make the API a bit >awkward, I agree. > >> It should be not to difficult to write some simple wrapper classes >> with Snit or something similar. But using snit may slow down the code. >> Did >> anyone have a better suggestion? > >Do it in the TclDOM package itself; please submit a feature request to >SF. > >At the moment I'm flat-out with other (paid-for) projects so I won't be >able to do anything about it in the short-term. However, maybe someone >else will step up to the plate and give it a go. > >> As a side effect the same code might run with tdom also. > >Ah... that's a sore point. You go talk to the tDOM people about >compatibility :-( > >Cheers, >Steve Ball > >--- > >Steve Ball | XSLT Standard Library | Training & Seminars >Zveno Pty Ltd | Web Tcl Complete | XML XSL Schemas >http://www.zveno.com/ | TclXML TclDOM | Tcl, Web Development >Ste...@zv... +---------------------------+--------------------- >Ph. +61 2 6242 4099 | Mobile (0413) 594 462 | Fax +61 2 6242 4099 > -- Dr. Detlef Groth Max-Planck-Institut fuer Molekulare Genetik Ihnestr. 63/73 D-14195 Berlin Tel.: + 49 30 - 8413 1235 |