domDoc(3tcl)
okay so bestie, welcome to the domDoc(3tcl) page! this is literally all about manipulating an instance of a DOM document object. no cap. π
β¨ NAME β¨
domDoc β Manipulates an instance of a DOM document object. that's the whole vibe. π¦
πΈ SYNOPSIS πΈ
domDocObjCmd method ?arg arg ...?
π¦ DESCRIPTION π¦
So this command manipulates one particular instance of a document object. The method argument tells it which specific method of the document class to use. These methods should closely conform to the W3C recommendation "Document Object Model (Core) Level 1" (http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html). Go read those docs if you want a deeper understanding of the functionality. iykyk. β¨
The valid methods are giving:
documentElement ?objVar?β Returns the top most element in the document (the root element). like the main character of the tree. πgetElementsByTagName nameβ Returns a list of all elements in the document matching (glob style)name. it's giving search party. πgetElementsByTagNameNS uri localnameβ Returns a list of all elements in the subtree matching (glob style)localnameand having the given namespaceuri. same energy but with namespaces. πcreateElement tagName ?objVar?β Creates (allocates) a new element node with node nametagName, append it to the hidden fragment list in the document object and returns the node object. IfobjVaris given the new node object is stored in this variable. cute little node. β¨createElementNS url tagName ?objVar?β Creates (allocates) a new element node within a namespace havingurias the URI and node nametagName, which could include the namespace prefix, append it to the hidden fragment list in the document object and returns the node object. IfobjVaris given the new node object is stored in this variable. namespace realness. πcreateTextNode text ?objVar?β Creates (allocates) a new text node with node valuetext, appends it to the hidden fragment list in the document object and returns the node object. IfobjVaris given, the new node object is stored in this variable. text go brrr. πcreateComment text ?objVar?β Creates (allocates) a new comment node with valuetext, appends it to the hidden fragment list in the document object and returns the node object. IfobjVaris given, the new comment node object is stored in this variable. comments are iconic. π¬createCDATASection data ?objVar?β Creates (allocates) a new CDATA node with node valuedata, appends it to the hidden fragment list in the document object and returns the node object. IfobjVaris given, the new node object is stored in this variable. CDATA slay. π₯createProcessingInstruction target data ?objVar?β Creates a process instruction, appends it to the hidden fragment list in the document object and returns the node object. IfobjVaris given, the new node object is stored in this variable. processing instruction vibes. βοΈdeleteβ Explicitly deletes the document, including the associated Tcl object commands (for nodes, fragment/new nodes, the document object itself) and the underlying DOM tree. bye bye everything. πgetDefaultOutputMethodβ Returns the default output method of the document. This is usually a result of a XSLT transformation. usually XSLT did this. β¨asXMLβ synopsis:
asXML ?-indent none/tabs/1..8? ?-channel channelId? ?-escapeNonASCII?
?-doctypeDeclaration <boolean>? -xmlDeclaration <boolean>? -encString
<string> ?-escapeAllQuot? ?-indentAttrs? ?-nogtescape?
?-noEmptyElementTag? ?-escapeCR? ?-escapeTab?
Returns the DOM tree as an (optional indented) XML string or sends the output directly to the given channelId. here's the tea on each option:
-indentβ requires"no","none","tabs"or a natural number between 0 and 8, both included, as value. With the values"no"or"none"no additional white space outside of markup will be added to the serialization. Otherwise, it's a "pretty-print" serialization, due to inserting white space between end and the next start tag according to the nesting level. The level indentation wide is given with the number. If the value is"tabs", then indentation is done with tabs, one tab per level.-channelβ sends the output directly to the givenchannelIdinstead of returning a string. (this is from the general description)-escapeNonASCIIβ if given, every non 7 bit ASCII character in attribute values or element PCDATA content will be escaped as character reference in decimal representation.-doctypeDeclarationβ determines whether there will be a DOCTYPE declaration emitted before the first node of the document. The default is not to emit it. The DOCTYPE name will always be the element name of the document element. An external entity declaration of the external subset is only emitted if the document has a system identifier.-xmlDeclarationβ determines whether there will be an XML Declaration and a newline emitted before anything else. The default is not to emit one. If this flag is given with a true argument then-encStringsets the encoding value in the XML Declaration.-encStringβ sets the encoding value in the XML Declaration. Otherwise this option is ignored. Please note that this option just enhances the string representation of the generated XML Declaration with an encoding information string, nothing more. It's up to the user to handle encoding in case of writing to a channel or reparsing.-escapeAllQuotβ if given, quotation marks will be escaped with"even in text content of elements.-indentAttrsβ if given, then attributes will each be separated with newlines and indented to the same level as the parent node plus the value given as argument to-indentAttrs(0..8).-nogtescapeβ if given then the character'>'won't get escaped in attribute values and text content of elements. The default is to escape this character.-noEmptyElementTagβ if given then no empty tag syntax will be used. Instead, if an element has empty content it will be serialized with an element start tag and an immediately following element end tag.-escapeCRβ if given then the character'\r'will be escaped as character reference in attribute values and text content of elements. The default is to not do this.-escapeTabβ if given then the character'\t'will be escaped as character reference in attribute values and text content of elements. The default is to not do this.
asCanonicalXMLβ synopsis:
asCanonicalXML ?-channel channelId? ?-comments <boolean>?
Returns the DOM tree as canonical XML string according to the "Canonical XML Version 1.0 W3C Recommendation 15 March 2001" or sends the output directly to the given channelId.
If the goal is to get a canonical XML serialization of the XML file from which the DOM tree was parsed there are a few prerequisites. The XML data must be parsed with the -keepEmpties option. If the XML data includes a DTD which defines attribute defaults or external parsed entity references it is necessary to use the expat parser (not the -simple one). For any supported Tcl version lesser then 9.0 if the XML data includes characters outside the BMP a Tcl build with TCL_UTF_MAX defined to 6 (and a tDOM build with this Tcl) is necessary.
-channelβ if given then the output is send directly to the Tcl channel given as argument. It is the up to the caller to ensure that the channel is correctly fconfigured. If this option is not given then the command returns the serialization as string.-commentsβ if given with a true value then the serialization includes comments according to the rules of the recommendation. If the value is false or this option is omitted then comments are removed from the serialization.
asHTMLβ synopsis:
asHTML ?-channel channelId? ?-escapeNonASCII? ?-htmlEntities?
?-doctypeDeclaration <boolean>? ?-breakLines? ?-onlyContents?
Returns the DOM tree serialized according to HTML rules (HTML elements are recognized regardless of case, without end tags for empty HTML elements etc.) as string or sends the output directly to the given channelId.
-escapeNonASCIIβ if given, every non 7 bit ASCII character in attribute values or element PCDATA content will be escaped as character reference in decimal representation.-htmlEntitiesβ if given, a character or a pair of characters is written using its HTML 5 character entity reference, if it has one. Some HTML 5 character entity references encode the same character or code points. From the possible entity names the shortest is choosen. If there are more than one shortest name and this names differ only in case then the lowercase alternative is choosen. Otherwise frist name in lexical ASCII order is choosen. There is one HTML5 entitiy (ThickSpace), which escapes two characters for which the first and the second character have an entity name by itself. If the two characters are to be serialized then the one two-characters entity ThickSpace will be choosen.-breakLinesβ if given the serialization outputs"\n>"instead of">"for the opening tags of elements.-onlyContentsβ if given only all child nodes are serialized. This option is ignored by document nodes.-doctypeDeclarationβ if given there will be a DOCTYPE declaration emitted before the first node of the document. The default is, to do not. The DOCTYPE name will always be the element name of the document element without case normalization. An external entity declaration of the external subset is only emitted, if the document has a system identifier. The doctype declaration will be written from the available information, without check, if this is a known (w3c) HTML version information or if the document confirms to the given HTML version. All nodes types other than document nodes ignore this option.-channelβ sends the output directly to the givenchannelId. (from general description)
asTextβ TheasTextmethod returns the tree by serializing the string-value of every text node in document order without any escaping. In effect, this is what the xslt output method "text" (XSLT 1.0 recommendation, section 16.3) does. just pure text, no escaping. πasJSONβ synopsis:
asJSON ?-indent none/0..8? ?-channel channelId?
π« asJSON π«
Okay bestie, so asJSON takes your tree and serializes it into a valid JSON data string β¨. And ngl? In general this may be a lossy serialization β that's just the deal. Here's what gets ghosted: all comment nodes, character data sections and processing instruction nodes, all attributes, and all XML namespaces. Gone, poof π. Only element and text nodes may be reflected in the generated JSON serialization. And the appropriate JSON data type information of a node will be respected, periodt.
If an element node has the JSON type OBJECT, then every element node child of that element gets serialized as a member of that object β the node name of the child is the member name, and the relevant children of that child are the value π«Ά. Every other child node? Ignored, sorry babe π.
If an element node has the JSON type ARRAY, then the text and element node children of that element node get serialized as the consecutive values of the array π. Element node children of an ARRAY element are container nodes for nested ARRAY or OBJECT values.
Text nodes with the JSON types TRUE, FALSE or NULL get serialized to the corresponding JSON token without looking at the value of the text node π. A text node without JSON type is always serialized as a JSON string token. And a text node with JSON type NUMBER gets serialized as a JSON number token if the text node value is in fact a valid JSON number β and as a JSON string if it isn't. No cap.
If an element node doesn't have a JSON type, then the serialization of its children is decided by these rules:
- Only text and element node children are relevant here. If the element node you're serializing is the member of a JSON object and there's no relevant child node, the value of that member will be an empty JSON string.
- If the only relevant child node of this element node is a text node, then the JSON value of that text node becomes the value of the object member.
- If the element has more than one relevant child node and the first one is a text node, then the relevant children get serialized as a JSON array.
- If the only relevant child node is an element node β or the first relevant child is an element node and the node name of that only-or-first relevant child isn't equal to the array container node name β then all element node children get serialized as the members of a JSON object (while ignoring any intermixed text nodes).
- If the only or first relevant child is an element node and the node name of this child is equal to the array container element name, then all relevant children get serialized as the values of a JSON array.
And for when the element you're serializing is a value of a JSON array:
- If the node name of this element isn't equal to the array container node name, that element is seen as a container node for a JSON object, and all element node children get serialized as the members of that array while ignoring any text node children.
- If the node name of this element is equal to the array container node name, all relevant children get serialized as a JSON array.
-channelβ if this option is given, the serialization isn't returned as a string; it's sent directly to the channel, given as the argument to the option. Direct delivery, iykyk π-indentβ if this option is given and the argument given to it isn't"none", then the returned JSON string is "pretty-printed" π. The numeric argument to this option defines the number of spaces for any indentation level. The default is to not emit any additional white space.
π¦ asTclValue π¦
asTclValue ?typevariable?
In case the DOM tree includes JSON type information, this method returns the JSON data as a nested Tcl data structure πΈ.
The returned value may be a Tcl dict, a Tcl list or a string. And if the optional argument typevariable is given, then the variable with that name is set to the value dict, list or string respectively β which is how it signals the type of the result to you.
- A JSON object is returned as a Tcl dict.
- A JSON array is returned as a list.
- JSON strings and numbers, as well as the symbolic JSON values
null,trueandfalse, are returned as a string (with the stringsnull,trueandfalsefor the respectively JSON symbol).
The value of a member of a JSON object may also be a Tcl dict, or a Tcl list, or a string β and the elements of a JSON array list may be a Tcl dict or a Tcl list or a string. Nested chaos, but make it organized π.
β asTypedList β
In case the DOM tree includes JSON type information, this method returns the JSON data as a nested Tcl list π.
The first element of every one of these lists describes the type of the value. The types are: OBJECT, ARRAY, STRING, NUMBER, TRUE, FALSE or NULL.
- If the type is
NUMBERorSTRING, then the second (and last) element is the value. - If the type is
NULL,TRUEorFALSE, the list doesn't have any other elements. - If the type is
OBJECT, the second value will be a Tcl list of property name and value pairs β which means the second element could be used as a dict. The value will be a Tcl list built by the rules of theasTypedListmethod. - If the type is
ARRAY, the second value will be a Tcl list of the JSON array values, each one built by the rules of theasTypedListmethod.
π publicId π
publicId ?publicId?
Returns the public identifier of the doctype declaration of the document, if there is one β otherwise the empty string. And if there's a value given to the method, the public identifier of the document gets set to that value. Read and write, iconic behavior π.
π systemId π
systemId ?systemId?
Returns the system identifier of the doctype declaration of the document, if there is one β otherwise the empty string. If there's a value given to the method, the system identifier of the document is set to this value. Same energy, different identifier π«Ά.
π internalSubset π
internalSubset ?internalSubset?
Returns the internal subset of the doctype declaration of the document, if there is one β otherwise the empty string. If there's a value given to the method, the internal subset of the document is set to this value.
Okay so important things bestie β οΈ β note that none of the parsing methods preserve the internal subset of a document, so a freshly parsed document will always have an empty internal subset. Also note that the method does no syntactical check at all on a given internal subset. You've been warned π
π cdataSectionElements π
cdataSectionElements (?URI:?localname|*) ?<boolean>?
This method lets you control for which element nodes the text node children will be serialized as CDATA sections π. Just so we're clear: this affects only serialization with the asXML method β no text node is altered in any way by this method, fr fr.
- If the method is called with an element name as first argument and a boolean with value
trueas second argument, every text node child of every element node in the document with the same name as the first argument will be serialized as a CDATA section. - If the second argument is a boolean with value
false, all text nodes of all elements with the same name as the first argument will be serialized as usual. - Namespaced element names have to be given in the form
namespace_URI:localnameβ not in the otherwise usualprefix:localnameform. Don't get it twisted π - With two arguments called, the method returns the used boolean value.
- If the method is called with only an element name, it returns a boolean value indicating that the text node children of all elements with that name in the document will be serialized as CDATA section elements (return value
1) or not (return value0). - If the method is called with only one argument and that argument is an asterisk (
'*'), then the method returns an unordered list of all element names of the document for which the text node children will be serialized as CDATA section nodes.
π§ selectNodesNamespaces π§
selectNodesNamespaces ?prefixUriList?
This method gives you control over a document-global prefix-to-namespace-URI mapping π, which will be used for selectNodes method calls β on the document as well as on all nodes that belong to the document β if it isn't overwritten by using the -namespaces option of the selectNodes method.
Any namespace prefix within an xpath expression will first be resolved against this list. If the list binds the same prefix to different namespaces, then the first binding wins. And if a prefix couldn't be resolved against the document-global prefix/namespaces list, then the namespace definitions in scope of the context node get used to resolve the prefix, as usual.
If the optional argument prefixUriList is given, then the global prefix/namespace list is set to this list β and it returns it. Without the optional argument, the method returns the current list. The default is the empty list πΈ.
π₯ xslt π₯
xslt ?-parameters parameterList? ?-ignoreUndeclaredParameters?
?-maxApplyDepth int? ?-xsltmessagecmd script? stylesheet ?outputVar?
Applies an XSLT transformation on the whole document of the node object, using the XSLT stylesheet (given as domDoc). It returns a document object containing the result document of the transformation, and it stores that document object in the optional outputVar, if that was given. Transformation glow-up, bestie β¨
-parametersparameterList β optional; sets top-level<xsl:param>to string values. The parameterList has to be a tcl list consisting of parameter name and value pairs.-ignoreUndeclaredParametersβ if this option is given, then parameter names in the parameterList given to the-parametersoption that aren't declared as top-level parameters in the stylesheet are silently ignored. Without this option, an error is raised if the user tries to set a top-level parameter that isn't declared in the stylesheet.-maxApplyDepthint β expects a positiv integer as argument. By default, the XSLT engine allows XSLT templates to nest up to 3000 levels (and raises an error if they nest deeper). This limit can be set by the-maxApplyDepthoption.-xsltmessagecmdscript β yep, this one's in the method's option list too, exactly as the synopsis shows it.
Okay bestie, so about that -xsltmessagecmd option? π
It sets a callback for xslt:message elements in the stylesheet. The actual command is literally the script you gave as the option's argument, then the XML Fragment from instantiating the xsl:message element content as a string (as if the XPath string() function had been applied to the XML Fragment), and then a flag that says whether the xsl:message has an attribute "terminate" with the value "yes". If the called script returns anything else then TCL_OK, the XSLT transformation will be aborted, returning error. If the called script returns -code break, the error message is empty; otherwise the result code is reported. And in case of a terminated transformation, the outputVar, if given, is set to the empty string. No cap. π«Ά
toXSLTcmd ?objVar?β If the DOM tree represents a valid XSLT stylesheet, this method transforms the DOM tree into an XSLT command, otherwise it returns error. The createdxsltCmdis returned and stored in theobjVar, if a var name was given. A successful transformation of the DOM tree to anxsltCmdremoves thedomDoccmd and allnodeCmdsof the document. β¨
The syntax of the created xsltCmd is:
xsltCmd method ?arg ...?
The valid methods are:
transform ?-parameters parameterList? ?-ignoreUndeclaredParameters? ?-maxApplyDepth int? ?-xsltmessagecmd script? domDoc ?outputVar?β Applies XSLT transformation on the documentdomDoc. Returns a document object containing the result document of that transformation and stores it in the optionaloutputVar.- The optional
-parametersoption sets top level<xsl:param>to string values. TheparameterListhas to be a tcl list consisting of parameter name and value pairs. - If the option
-ignoreUndeclaredParametersis given, then parameter names in theparameterListgiven to the-parametersoptions that are not declared as top-level parameters in the stylesheet are silently ignored. Without this option, an error is raised if the user tries to set a top-level parameter, which is not declared in the stylesheet. - The option
-maxApplyDepthexpects a positiv integer as argument. By default, the XSLT engine allows XSLT templates to nest up to 3000 levels (and raises error if they nest deeper). This limit can be set by the-maxApplyDepthoption. - The
-xsltmessagecmdoption sets a callback forxslt:messageelements in the stylesheet. The actual command consists of the script, given as argument to the option, appended with the XML Fragment from instantiating thexsl:messageelement content as string (as if the XPathstring()function would have been applied to the XML Fragment) and a flag, which indicates, if thexsl:messagehas an attribute"terminate"with the value"yes".
- The optional
deleteβ Deletes thexsltCmdand cleans up all used recourses. (yes, "recourses" fr fr π)
If the first argument to an xsltCmd is a domDoc or starts with a "-", then the command is processed in the same way as <xsltCmd> transform.
normalize ?-forXPath?β Puts all text nodes in the document into a"normal"form where only structure (e.g., elements, comments, processing instructions and CDATA sections) separates text nodes, i.e., there are neither adjacent text nodes nor empty text nodes. If the option-forXPathis given, all CDATA sections in the nodes are converted to text nodes, as a first step before the normalization. π§nodeTypeβ Returns the node type of the document node. This is alwaysDOCUMENT_NODE. πgetElementById idβ Returns the node having aidattribute with valueidor the empty string, if no node has anidattribute with that value. πfirstChild ?objVar?β Returns the first top level node of the document. πΈlastChild ?objVar?β Returns the last top level node of the document. πΈappendChild newChildβ AppendnewChildto the end of the list of top level nodes of the document. π«ΆremoveChild childβ Removeschildfrom the list of top level nodes of the document.childwill be part of the document fragment list after this operation. It is not physically deleted. πhasChildNodesβ Returns1if the document has any nodes in the tree. Otherwise0is returned. β¨childNodesβ Returns a list of the top level nodes of the document. πownerDocument ?domObjVar?β Returns the document itself. (main character energy π)insertBefore newChild refChildβ InsertnewChildbefore therefChildinto the list of top level nodes of the document. IfrefChildis the empty string, insertsnewChildat the end of the top level nodes. π¦replaceChild newChild oldChildβ ReplacesoldChildwithnewChildin the list of children of that node. TheoldChildnode will be part of the document fragment list after this operation. πappendFromList listβ Parseslist, creates an according DOM subtree and appends this subtree at the end of the current list of top level nodes of the document. πappendXML XMLstringβ ParsesXMLstring, creates an according DOM subtree and appends this subtree at the end of the current list of top level nodes of the document. πselectNodes ?-namespaces prefixUriList? ?-cache <boolean>? ?-list? xpathQuery ?typeVar?β Returns the result of applying the XPath queryxpathQueryto the document. The context node of the query is the root node in the sense of the XPath recommendation (not the document element). The result can be a string/value, a list of strings, a list of nodes or a list of attribute name / value pairs. IftypeVaris given the result type name is stored into that variable (empty, bool, number, string, nodes, attrnodes or mixed). See the documentation of the of thedomNodecommand methodselectNodesfor a detailed description of the arguments. π€baseURI ?URI?β Returns the presentbaseURIof the document. If the optional argumentURIis given, sets the base URI of the document to the givenURI. πappendFromScript tclScriptβ Appends the nodes created by thetclScriptby Tcl functions, which have been built usingdom createNodeCmd, at the end of the current list of top level nodes of the document. πinsertBeforeFromScript tclScript refChildβ Inserts the nodes created in thetclScriptby Tcl functions, which have been built usingdom createNodeCmd, before therefChildinto to the list of top level nodes of the document. IfrefChildis the empty string, the new nodes will be appended. π§©deleteXPathCache ?xpathQuery?β If called without the optional argument, all cached XPath expressions of the document are freed. If called with the optional argumentxpathQuery, this single XPath query will be removed from the cache, if it is there. The method always returns an empty string. ποΈ
Otherwise, if an unknown method name is given, the command with the same name as the given method within the namespace ::dom::domDoc is tried to be executed. This allows quick method additions on Tcl level. β¨
Newly created nodes are appended to a hidden fragment list. If they are not moved into the tree they are automatically deleted as soon as the whole document gets deleted. π
β¨ SEE ALSO β¨
domdomNode
β¨ KEYWORDS β¨
DOM node creationdocument element
Okay bestie, that's the whole vibe! This page is from Tcl, version domDoc(3tcl) β source: Tcl, date: not shown but timeless πβ¨π¦. Go slay those DOMs! ππ
π¦ where this page comes from
- debian trixie: tdom 0.9.5.1-3 Β· MPL-2.0
- ubuntu questing: tdom 0.9.5.1-3
- ubuntu resolute: tdom 0.9.6-2 Β· MPL-2.0
- ubuntu stonking: tdom 0.9.6-3 Β· MPL-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms