asXML -indent
). Defaults to use tdom's default
# @param -indentAttrs the optional attribute indentation (see tdom's asXML -indentAttrs
). Defaults to use tdom's default
# @return the inner XML string
proc ::dom::domNode::innerXML {node args} {
# re-use parameter checks from asXML
set result [$node asXML {*}$args]
if {[$node hasChildNodes]} {
# strip element tag
set result [string replace $result 0 [string first ">" $result]]
set result [string replace $result [string last "<" $result] end]
# strip whitespace, but not the indentation
set indent ""
regexp {^\s*?\n??([ \t]*?)\S} $result -> indent
set result $indent[string trim $result]
return $result
} else {
return [$node text]
}
# UNREACHABLE
}
% % % % % %
% dom parse {Played on
Result:
Played on 10-Jun-1998
Result: Brazil2, Scotland1
Played on 23-Jun-1998
Result: Scotland0, Morocco3
====== ** Loading XML from file with the right encoding settings ** tDOM provides some helpers for this in the tdom.tcl lib distributed with the tDOM. Cited from [Rolf Ade]'s answer in [http://groups.google.com/forum/#!topic/comp.lang.tcl/ilbpBNs5KDU%|%tdom again - BOM and/or encoding problems & how to create node?], [comp.lang.tcl], 2008-05-22 : tDOM::xmlOpenFile expects a filename and returns a file channel handle, which : is readily fconfigure'd and seek'ed to get feeded into a dom parse `-channel` ... : Please note, that the proc opens a channel and returns that. That channel will : not magically go away, if you're done with it. It's your responsibility to : close that channel, if you don't need them anymore. So, a typical use pattern : (sure, not the only) is ====== set xmlfd [tDOM::xmlOpenFile $filename] set doc [dom parse -channel $xmlfd] close $xmlfd ====== `tDOM::xmlReadFile` is just a wrapper around `tDOM::xmlOpenFile`. The pattern is ====== set doc [dom parse [tDOM::xmlReadFile $filename]] ====== and you're done. No leaking file channels, filename in, DOM tree out. ** Preserving Whitespace ** [PYK] 2014-07-26: I was looking for a way to parse HTML while preserving newline characters in `` elements. In my case, `-keepEmpties` did the trick. See Also: [https://stackoverflow.com/questions/7908627/how-to-apply-an-xslt-transformation-that-includes-spaces-to-an-xml-doc-using-tdo/7908668#7908668%|%How to apply an XSLT transformation that includes spaces to an XML doc using tDOM?], 2011-10-26.: [https://groups.google.com/d/msg/comp.lang.tcl/ulu5tbGfrvs/O-iNWFvVc_EJ%|%asymmetry in tDOM XML parsing and rendering], [comp.lang.tcl], 2008-04-11: ** Discussion ** Uhm, this "full compliant" XSLT support is a little bit to much said - Jochen would have done better, if he had used my "almost compliant". Don't get this wrong. It is true, that tDOM's XSLT support was greatly improved over the last releases. I'm pretty sure, you could use every 'real live' XSLT stylesheet with it, with correct result. I won't confuse you with outlying nifty difty XSLT details, therefore I omit the list of things that are not quite right. There are (prominent) tools out there, that don't do it better than tDOM's XSLT engine, due to my extensive testing, but nevertheless claim since a couple of months 100% XSLT compliance, which is simply not completely true, and nobody bothers. So just use tDOM's XSLT and you will be happy with it. It's only, that I know my business and "full compliant" is not 100% true. Even the missing outlying details will be added, in the next months, for sure. I wonder, what Jochen then will write, in the announcement ;-). The XPath support is now indeed really very compliant and complete. de. ---- [skm] 2005-03-11 Many of the links here in [http://www.tdom.org/index.html#SECTid80ac968%|%Further Readings%|%] are stale. Does anyone have up-to-date locations for these papers? thanks. ---- [LV]: Note that even though the tdom.org web site shows no release since 2004, email on the tdom mail list encourages people to use the CVS to fetch the latest version of code, which developers assure continues to be updated. Note that 64 bit users need to get the cvs version of tdom to get it working properly, according to the developer. Also note that http://www.tdom.org/files/ appears to have the tdom ''releases'' that are being performed. ---- [LV] 2007-09-11: Just a note - if you build tdom from source, do a "make test" and see a crash, try rebuilding tdom with the '''--disable-tdomalloc''' flag; on my SPARC Solaris 9 system, this resolved the crash. ---- '''Getting the Current Namespace Mapping for a Node''' [DKF]: It came up in [comp.lang.tcl] recently that [Gerald Lester] needed access to the current namespace mapping for an arbitrary DOM node (parsing [XML Schema] and [WSDL] requires this sort of thing). [Rolf Ade] told us how: ======none Given that the variable node2 holds the node command, for which you want to know all prefix-URI mappings in scope, do ====== ====== $node2 selectNodes namespace::* ====== This returns a list of two-element lists (i.e. pairs). Of each pair, the first element is either the literal "xmlns" (stating that this is the description of the namespace of unqualified elements) or "xmlns:" followed by a string local namespace name (stating that this is the description of a named namespace and that elements and attributes in that namespace will be using qualified names). The second element of the pair is the URI that characterises the namespace (which need not resolve to anything). Note (for XML neophytes) that unqualified attributes are always in ''no'' namespace at all (unlike unqualified elements). ---- [LV] 2007-10-09: Anyone have an example of using tDOM to validate XML using a DTD? See the page [tnc] for that. ---- [LV]: from comp.lang.tcl, we read: ======none > 2. I evidently don't understand the domNode man page. For the > "getAttribute" method it says: > getAttribute attributeName ?defaultValue? > Returns the value of the attribute attributeName. If > attribute is not available defaultValue is returned. > It also doesn't give any example(s). > Can someone point me to some sample code? ====== If I have a Dom Document $doc Which has an element somewhere: ======noneStuff ====== ====== set $node [$doc getElementsByTagName "Foo"] puts [$node getAttribute "Bar"] ====== will print: ======none baz ====== ---- [male] 2010-02-24 - [Sorting nodes in tdom] ---- [CMcC] 2010-04-13 21:05:27: I was having some confusion with [[$node attributes]] and xml namespaces. [evilotto] helped me decode what it returns, and I record the findings for posterity. attributes may return a singleton. In that case, the attribute name is just that. attributes may return a three-element list. In that case it may be approximated as [[lassign $a name namespace uri]] ... however: the uri may be empty and the name and namespace equal. In that case, the attribute appears to be a definition of the uri for the namespace given by $name, although the uri thus defined is not returned in the uri field, the uri-defining attribute is named as if it were $ns:$ns. Finally, the {xmlns {} {}} form appears to be special, and to indicate that the xmlns namespace's uri is being defined. There. Clear as mud. No wonder XML is so popular (?) ---- [aricb] 2010-06-17: An '''XML declaration''' is a processing instruction along the lines of `` at the beginning of an XML document. As nearly as I can determine, the recommended way to put this line in a tDOM-generated XML file is something along the lines of: ====== puts $xmlfile "" ====== In other words, tDOM doesn't provide any facilities for outputting XML declarations (see [http://groups.google.com/group/comp.lang.tcl/browse_frm/thread/8a56e904db392835?tvc=1] and [http://groups.google.com/group/comp.lang.tcl/browse_frm/thread/865beb80e5837e2f/650d922399d811a6]). However, when parsing an XML file with tDOM, you can capture the value of the XML declaration's encoding attribute using: ====== dom parse [tDOM::xmlReadFile $filename encStr] ====== the value will be stored in `$encStr` (see [http://groups.google.com/group/comp.lang.tcl/browse_frm/thread/8a56e904db392835?tvc=1%|%this google thread%|%]). [DKF]: Note that the XML declaration is not formally a processing instruction, though it uses the same basic syntax. ** Historical ** www.tdom.org was terminated around 2010-11 [bovine] set up the github project with a mirror of the old website and a fork from the repository. [makr] kept as mirror of the old CVS repository. ---- [snichols]: I recently compiled tdom 0.8.0 on Windows XP successfully with threads enabled, but when doing a package require from within Tcl 8.4.7 I get the following error, "too many nested evaluations (infinite loop?)" when doing the package require tdom command. Any ideas? Thanks in advance. [RS]: Oh yes, a silly bug - in the file tdom.tcl, comment out the line ====== package require tdom ====== a file can't require a package during providing it :) [snichols]: Thank you very much. That fixed the issue. [AK] 2006-08-02: When was '''recently''' ? According to Rolf Ade this problem was fixed '''Sep 29, 2004'''. Both the package index generated by the TEA Makefile, and the one found in the 'win' directory load the shared library (i.e. DLL) first, then source tdom.tcl. As the DLL runs the C equivalent of 'package provide tdom' the 'package require tdom' executed by 'tdom.tcl' is satisfied and will not loop. ** Bugs ** [escargo] 2008-02-38: The "official home" does not provide any way to submit bug reports. I did report a problem to the Yahoo group (now defunct), since seemed to be the only resort available. [HaO] 2014-09-16: Rolf Ade said in a private conversation on the 2014 European TCL User meeting that he maintains tdom actively (thank you) and asked if there is something missing... The fossil repository [https://46.163.78.80/cgi-bin/repros/tdom/timeline] offers a ticket system which might be on his radar. ** Description ** This tightly-coded extension emphasizes speed and memory economy. In contrast to the "Pure Tcl" [TclDOM], it bests leading [Java] [DOM] implementations by an order of magnitude (!) in both processing speed and memory demand. Jochen also exploits tDOM's expressivity to offer a nice [HTML] reader, XML validator--called [tnc]--and [XSLT] engine. tDOM is in production at several commercial installations. 0.8.3 doesn't compile for Tcl version 8.6. Use trunk or a later version instead. [MSW]: now understands why tDOM crashed for him ... obviously you shouldn't be generating variables more than once (like two consecutive [[documentElement root]] in different functions make tDOM trip). He's still intrigued by appended nodes not being addressable (no localName, no URI, not reachable via [[selectNodes]]). Hmm? ====== set doc [dom createDocument foo] set root [$doc documentElement] # append a node set node [$root appendChild [$doc createElement bar]] # all of the following return a value == $node $root firstChild lindex [$root selectNodes /foo/bar] 0 lindex [$root selectNodes //bar] 0 lindex [$root selectNodes /*/*] 0 lindex [$root getElementsByTagName bar] 0 # do it again set node1 [$root appendChild [$doc createElement bar]] # all of the following return a value == $node1 [$root firstChild] nextSibling lindex [$root selectNodes /foo/bar] 1 lindex [$root selectNodes {/foo/bar[2]}] 0 lindex [$root selectNodes //bar] 1 lindex [$root selectNodes /*/*] 1 lindex [$root getElementsByTagName bar] 1 ====== < > Package | Mailing List | XML | Parsing