From: Steve B. <Ste...@ex...> - 2006-11-26 20:39:54
|
Cameron, et al, (I can't find the c.l.t. thread for this message; Cameron, would you please forward to the newsgroup?) The subcommand is "selectNode". This is a misnomer since one is usually selecting multiple nodes. Since it is a non-standard feature it is at the DOMImplementation level, rather than at the Document level. Its use is like so: set nodes [dom::selectNode $doc //via] selectNode returns a static node list, which in terms of TclDOM means a normal Tcl list. DOM Level 3 has an official method for using XPath with a DOM tree, but I haven't had time to implement that yet. Bear in mind that the DOM and XPath data models are not entirely compatible, so this is not trivial. This doesn't seem to be quite what the original message was asking. To get an element's attributes as a list you can use TclXML (ie. the SAX-like interface). This delivers the attributes of an element as a Tcl list, as desired. But which element? You have to write your own code to figure that out. This is where XPath is better. //@* would give you every attribute of every element in the document. //via/@* would give you every attribute of every "via" element in the document (not just of the document/root element). The variations are almost endless. However, you need a DOM tree to do this. HTHs, Steve Ball On 26/11/2006, at 5:16 AM, Cameron Laird wrote: > In article <jij...@la...>, I recommended: >> In article <116...@h5...>, >> comp.lang.tcl <phi...@gm...> wrote: >> . >> . >> . >>>> What exactly are you attempting to do? >>> >>> It's so easy. All I want to do is convert an XML file into a TCL >>> list, >>> that's it, just a TCL list: >>> >>> attr1 {val1} attr2 {val2} >> . >> . >> . >> Should we recommend a SAX or XQuery approach for this? >> A SAX accumulator used to be canonical for something >> this simple; I haven't worked with Rolf's XPath, but I >> suspect it affords a one-liner that satisfies the >> requirements. > > Oh, my; I *do* perceive deficiencies in TclXML documentation. > > I know that TclXML has had XPath since 2001. In particular, by 2004, > Steve wrote about selectNodes in 3.0 <URL: > http://talkaboutprogramming.com/group/comp.lang.tcl/messages/ > 253140.html >. > When I run > > puts [package require dom] > > set xml_image {<?xml version = "1.0" encoding = "UTF-8" ?> > <via id="1" trivia_id="255" question="How much wood would a > woodchuck chuck?" answer_id="1" answer="A lot" > expDate="116494926"></via> > } > > set doc [dom::parse $xml_image] > set nodes [$doc selectNodes //via] > > on a recent Mac OS, though, I see > > 3.1 > bad method "selectNodes": must be cget, configure, createElement, > createDocumentFragment, createTextNode, createComment, > createCDATASection, createProcessingInstruction, > createAttribute, > createEntity, createEntityReference, createDocTypeDecl, > importNode, > createElementNS, createAttributeNS, getElementsByTagNameNS, > getElementsById, createEvent, getElementsByTagName, dtd, or > schema > > So: what's it take to get TclXML to do XPath? > |