<manpage id="domDoc" cat="cmd" title="domDoc">
  <namesection>
    <name>domDoc</name>
    <desc>Manipulates an instance of a DOM document object</desc>
  </namesection>
  <!--
  
  Copyright (c) 1999 Jochen Loewer (loewerj@hotmail.com)
  Copyright (c) 2002-2005 Rolf Ade (rolf@pointsman.de)

  See the file "LICENSE" for information on usage and redistribution
  of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  
  -->
  <synopsis>
    <syntax><cmd>domDocObjCmd</cmd> <m>method</m> ?<m>arg arg ...</m>?</syntax>
  </synopsis>

  <section>
    <title>DESCRIPTION </title>

    <p>This command manipulates one particular instance of a document
object. <m>method</m> indicates a specific method of the document class. These
methods should closely conform to the W3C recommendation "Document Object Model
(Core) Level 1" (<ref
href="http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html">http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html</ref>). Look
at these documents for a deeper understanding of the functionality.</p>

      <p>The valid methods are:</p>

      <commandlist>
        <commanddef>
          <command><method>documentElement</method> ?<m>objVar</m>?</command>
          <desc>Returns the top most element in the document (the root
element).</desc>
        </commanddef>

        <commanddef>
          <command><method>getElementsByTagName</method> <m>name</m></command>
          <desc>Returns a list of all elements in the document matching
(glob style) <m>name</m>.</desc>
        </commanddef>

      <commanddef>
        <command><method>getElementsByTagNameNS</method> <m>uri</m> <m>localname</m></command>
        <desc> Returns a list of all elements in the subtree
matching (glob style) <m>localname</m> and having the given namespace
<m>uri</m>.</desc>
      </commanddef>

        <commanddef>
          <command><method>createElement</method> <m>tagName</m> ?<m>objVar</m>?</command>
          <desc>Creates (allocates) a new element node with node name
<m>tagName</m>, append it to the hidden fragment list in the document
object and returns the node object.  If <m>objVar</m> is given the new
node object is stored in this variable.</desc>
        </commanddef>

        <commanddef>
          <command><method>createElementNS</method> <m>url</m> <m>tagName</m> ?<m>objVar</m>?</command>
          <desc>Creates (allocates) a new element node within a namespace
having <m>uri</m> as the URI and node name <m>tagName</m>, which
could include the namespace prefix, append it to the hidden fragment list in
the document object and returns the node object.  If <m>objVar</m> is
given the new node object is stored in this variable.</desc>
        </commanddef>

        <commanddef>
          <command><method>createTextNode</method> <m>text</m> ?<m>objVar</m>?</command>
          <desc>Creates (allocates) a new text node with node value
<m>text</m>, appends it to the hidden fragment list in the document
object and returns the node object.  If <m>objVar</m> is given, the new
node object is stored in this variable.</desc>
        </commanddef>

        <commanddef>
          <command><method>createComment</method> <m>text</m> ?<m>objVar</m>?</command>
          <desc>Creates (allocates) a new comment node with value
<m>text</m>, appends it to the hidden fragment list in the document
object and returns the node object.  If <m>objVar</m> is given, the new
comment node object is stored in this variable.</desc>
        </commanddef>

        <commanddef>
          <command><method>createCDATASection</method> <m>data</m> ?<m>objVar</m>?</command>
          <desc>Creates (allocates) a new CDATA node with node value
<m>data</m>, appends it to the hidden fragment list in the document
object and returns the node object.  If <m>objVar</m> is given, the new
node object is stored in this variable.</desc>
        </commanddef>

        <commanddef>
          <command><method>createProcessingInstruction</method> <m>target</m> <m>data</m> ?<m>objVar</m>?</command>
          <desc>Creates a process instruction, appends it to the hidden
fragment list in the document object and returns the node object.  If
<m>objVar</m> is given, the new node object is stored in this variable.</desc>
        </commanddef>

        <commanddef>
          <command><method>delete</method></command>
          <desc>Explicitly deletes the document, including the associated
Tcl object commands (for nodes, fragment/new nodes, the document object itself)
and the underlying DOM tree.</desc>
        </commanddef>

        <commanddef>
          <command><method>getDefaultOutputMethod</method></command>
          <desc>Returns the default output method of the document. This is
usually a result of a XSLT transformation.</desc>
        </commanddef>

      <commanddef>
        <command><method>asXML</method> <option>?-indent none/1..8?</option> <option>?-channel channelId?</option> <option>?-escapeNonASCII?</option> <option>?-doctypeDeclaration &lt;boolean&gt;?</option> <option>?-escapeAllQuot?</option></command>
        <desc>Returns the DOM tree as an (optional indented) XML
string or sends the output directly to the given channelId. If the
option <m>-escapeNonASCII</m> is given, every non 7 bit ASCII
character in attribute values or element PCDATA content will be
escaped as character reference in decimal representation. The flag
<m>-doctypeDeclaration</m> determines, whether 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. An external entity declaration of the external
subset is only emitted, if the document has a system identifier. If
the option <m>-escapeAllQuot</m> is given, quotation marks will be
escaped with &amp;quot; even in text content of elements.</desc>

      </commanddef>

      <commanddef>
        <command><method>asHTML</method> <option>?-channel
channelId?</option> <option>?-escapeNonASCII?</option> <option>?-htmlEntities?</option> <option>?-doctypeDeclaration &lt;boolean&gt;?</option></command> 
        <desc>Returns the DOM tree serialized acording to HTML rules (HTML
elements are recognized regardless of case, without end tags for emtpy HTML
elements etc.), as string or sends the output directly to the given
channelId. If the option <m>-escapeNonASCII</m> is given, every non 7 bit ASCII
character in attribute values or element PCDATA content will be escaped as
character reference in decimal representation. If the option
<m>-htmlEntities</m> is given, a character is outputed using a HTML 4.01
character entity reference, if one is defined for it. The flag
<m>-doctypeDeclaration</m> determines, whether 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 avaliable informations, without
check, if this is a known (w3c) HTML version information or if the document
confirms to the given HTML version.</desc>
      </commanddef>

      <commanddef>
        <command><method>asText</method></command>
          <desc>The asText method outputs the result tree by outputting
the string-value of every text node in the result tree in document
order without any escaping. In effect, this is what the xslt output method
"text" (XSLT 1.0 recommendation, section 16.3) does.</desc>
      </commanddef>

      <commanddef>
        <command><method>publicId</method> <m>?publicId?</m></command>
        <desc>Returns the public identifier of the doctype declaration of the
document, if there is one, otherwise the empty string. If there is a value
given to the method, the public identifier of the document is set to this
value.</desc>
      </commanddef>

      <commanddef>
        <command><method>systemId</method> <m>?systemId?</m></command>
        <desc>Returns the system identifier of the doctype declaration of the
document, if there is one, otherwise the empty string. If there is a value
given to the method, the system identifier of the document is set to this
value.</desc>
      </commanddef>

      <commanddef>
        <command><method>internalSubset</method> <m>?internalSubset?</m></command>
        <desc>Returns the internal subset of the doctype declaration of the
document, if there is one, otherwise the empty string. If there is a value
given to the method, the internal subset of the document is set to this
value. Note, that none of the parsing methods preserve the internal subset
of a document; a freshly parsed document will always have an empty internal
subset. Also note, that the method doesen't do any syntactical check on a
given internal subset.</desc>
      </commanddef>

      <commanddef>
        <command><method>cdataSectionElements</method> <m>(?URI:?localname|*) ?&lt;boolean&gt;?</m></command>
        <desc>This method allows to control, for which element nodes
the text node childs will be serialized as CDATA sections (this affects only
serialization with the asXML method, no text node is altered in any
way by this method). IF the method is called with an element name as
first argument and a boolean with value true as 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 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 given in the form
namespace_URI:localname, not in the otherwise usual prefix:localname
form. With two arguments called, the method returns the used boolean
value. If the method is called with only an element name, it will
return a boolean value, indicating, if the text nodes childs of all
elements with that name in the document will be serialized as CDATA
section elements (return value 1) or not (return value 0). 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 childs will be
serialized as CDATA section nodes.</desc>
      </commanddef>

      <commanddef>
        <command><method>selectNodesNamespaces</method> <option>?prefixUriList?</option></command>
        <desc>This method allows to control a document global prefix
to namespace URI mapping, which will be used for selectNodes method
calls (on document as well as on all nodes, which belongs to the
document), if it is not overwritten by using the -namespaces option of
the selectNodes method. Any namespace prefix within an xpath
expression will be first resolved against this list. If the list bind
the same prefix to different namespaces, then the first binding will
win. If a prefix could not resolved against the document global prefix
/ namespaces list, then the namespace definitions in scope of the
context node will be used to resolve the prefix, as usual. If the
optional argument <m>prefixUriList</m> is given, then the global prefix /
namespace list is set to this list and returns it. Without
the optional argument the method returns the current list. The
default is the empty list.</desc>
      </commanddef>

      <commanddef>
        <command><method>xslt</method> <option>?-parameters
parameterList?</option> <option>?-ignoreUndeclaredParameters?</option>
<option>?-xsltmessagecmd script?</option> <m>stylesheet</m> <m>?outputVar?</m></command>
        <desc>Applies an XSLT transformation on the whole document of the node
object using the XSLT <m>stylesheet</m> (given as domDoc). Returns a document
object containing the result document of the transformation and stores that
document object in the optional <m>outputVar</m>, if that was given.

<p>The optional <m>-parameters</m> option sets top level
&lt;xsl:param&gt; to string values. The <m>parameterList</m> has to be a tcl
list consisting of parameter name and value pairs.</p>

<p>If the option <m>-ignoreUndeclaredParameters</m> is given, then parameter
names in the <m>parameterList</m> given to the <m>-parameters</m> options 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.</p>

<p>The <m>-xsltmessagecmd</m> option sets a callback for xslt:message elements
in the stylesheet. The actual command consists of the script, given as argument
to the option, appended with the XML Fragment from instantiating the
xsl:message element content as string (as if the XPath string() function would
have been applied to the XML Fragment) and a flag, which indicates, if the
xsl:message has an attribute "terminate" with the value "yes".</p>
</desc>
      </commanddef>

      <commanddef>
        <command><method>toXSLTcmd</method> ?<m>objVar</m>?</command>
     
        <desc>If the DOM tree represents a valid XSLT stylesheet, this method
transforms the DOM tree into an xslt command, otherwise it returns error. The
created xsltCmd is returnd and stored in the <m>objVar</m>, if a var name was
given. A successful transformation of the DOM tree to an xsltCmd removes the
domDoc cmd and all nodeCmds of the document. 

<p>The syntax of the created xsltCmd is:</p>
 
<syntax>
<cmd>xsltCmd</cmd> <option>method</option> <option>?arg ...?</option>
</syntax>

<p>The valid methods are:</p>

<commandlist>
  <commanddef>
    <command><method>transform</method> <option>?-parameters
parameterList?</option> <option>?-ignoreUndeclaredParameters?</option>
<option>?-xsltmessagecmd script?</option> <m>domDoc</m> <m>?outputVar?</m></command>          

        <desc>Applies XSLT transformation on the document
<m>domDoc</m>. Returns a document object containing the
result document of that transformation and stores it in the optional
<m>outputVar</m>. 

<p>The optional <m>-parameters</m> option sets top level
&lt;xsl:param&gt; to string values. The <m>parameterList</m> has to be a tcl
list consisting of parameter name and value pairs.</p>

<p>If the option <m>-ignoreUndeclaredParameters</m> is given, then parameter
names in the <m>parameterList</m> given to the <m>-parameters</m> options 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.</p>

<p>The <m>-xsltmessagecmd</m> option sets a callback for xslt:message elements
in the stylesheet. The actual command consists of the script, given as argument
to the option, appended with the XML Fragment from instantiating the
xsl:message element content as string (as if the XPath string() function would
have been applied to the XML Fragment) and a flag, which indicates, if the
xsl:message has an attribute "terminate" with the value "yes".</p>
</desc>
      </commanddef>

  <commanddef>
    <command><method>delete</method></command>          
    <desc>Deletes the xsltCmd and cleans up all used recourses</desc>
  </commanddef>
</commandlist>

<p>If the first argument to an xsltCmd is a domDoc or starts with a "-",
then the command is processed in the same way as 
<m>&lt;xsltCmd&gt; transform</m>.</p>
 </desc>
      </commanddef>

      <commanddef>
        <command><method>normalize</method> <m>?-forXPath?</m></command>
        <desc>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
<m>-forXPath</m> is given, all CDATA sections in the nodes are
converted to text nodes, as a first step before the
normalization. </desc>
      </commanddef>

      <commanddef>
        <command><method>nodeType</method></command>
        <desc>Returns the node type of the document node. This is always
DOCUMENT_NODE.</desc>
      </commanddef>

      <commanddef>
        <command><method>getElementById</method> <m>id</m></command>
        <desc>Returns the node having a id attribute with value
<m>id</m> or the emtpy string, if no node has an id attribute with that value.</desc>
      </commanddef>

      <commanddef>
        <command><method>firstChild</method> <variable>?objVar?</variable></command>
        <desc>Returns the first top level node of the document.</desc>
      </commanddef>

      <commanddef>
        <command><method>lastChild</method> <variable>?objVar?</variable></command>
        <desc>Returns the last top level node of the document.</desc>
      </commanddef>

      <commanddef>
        <command><method>appendChild</method> <m>newChild</m></command>
        <desc>Append <m>newChild</m> to the end of the list of top level nodes
of the document.</desc>
      </commanddef>

      <commanddef>
        <command><method>removeChild</method> <m>child</m></command>
        <desc>Removes <m>child</m> from the list of top level nodes of the
document. <m>child</m> will be part of the document fragment list
after this operation. It is not physically deleted.</desc>
      </commanddef>

      <commanddef>
        <command><method>hasChildNodes</method></command>
        <desc>Returns 1 if the document has any nodes in the tree. Otherwise 0 is returned.</desc>
      </commanddef>

      <commanddef>
        <command><method>childNodes</method></command>
        <desc>Returns a list of the top level nodes of the document.</desc>
      </commanddef>

      <commanddef>
        <command><method>ownerDocument</method> <variable>?domObjVar?</variable></command>
        <desc>Returns the document itself.</desc>
      </commanddef>

      <commanddef>
        <command><method>insertBefore</method> <m>newChild</m>  <m>refChild</m></command>
        <desc>Insert <m>newChild</m> before the <m>refChild</m> into the list of
top level nodes of the document. If <m>refChild</m> is the empty string, insert
<m>newChild</m> at the end of the top level nodes.</desc>
      </commanddef>

      <commanddef>
        <command><method>replaceChild</method> <m>newChild</m>  <m>oldChild</m></command>
        <desc>Replace <m>oldChild</m> with <m>newChild</m> in the list of
children of that node. The <m>oldChild</m> node will be part of the
document fragment list after this operation.</desc>
      </commanddef>

      <commanddef>
        <command><method>appendFromList</method> <m>list</m></command>
        <desc>Parses <m>list</m> , creates an according DOM subtree and
appends this subtree at the end of the current list of top level nodes of the document.</desc>
      </commanddef>

      <commanddef>
        <command><method>appendXML</method> <m>XMLstring</m></command>
        <desc>Parses <m>XMLstring</m>, creates an according DOM subtree and
appends this subtree at the end of the current list of top level nodes of the document.</desc>
      </commanddef>

      <commanddef>
          <command><method>selectNodes</method> <option>?-namespaces prefixUriList?</option> <option>?-cache &lt;boolean&gt;?</option> <m>xpathQuery</m> <option>?typeVar?</option></command>
          <desc><p>Returns the result of applying the XPath query
<m>xpathQuery</m> to 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. If <m>typeVar</m> is given
the result type name is stored into that variable (empty,
bool, number, string, nodes, attrnodes or mixed).</p>

<p>The argument <m>xpathQuery</m> has to be a valid XPath
expression. However, there is one exception to that rule. Tcl variable
names can appear in the XPath statement at any position where it is
legal according to the rules of the XPath syntax to put an XPath
variable. The value of the variable is substituted for the variable
name. Ignoring the syntax rules of XPath the Tcl variable name may be
any legal Tcl var name: local variables, global variables, array
entries and so on.</p>

<p>The option <m>-namespaces</m> expects a tcl list with prefix /
namespace pairs as argument. If this option is not given, then any
namespace prefix within the xpath expression will be first resolved
against the list of prefix / namespace pairs set with the
selectNodesNamespaces method for the document, the node belongs to. If
this fails, then the namespace definitions in scope of the context
node will be used to resolve the prefix. If this option is given, any
namespace prefix within the xpath expression will be first resolved
against that given list (and ignoring the document global prefix /
namespace list). If the list bind the same prefix to different
namespaces, then the first binding will win.  If this fails, then the
namespace definitions in scope of the context node will be used to
resolve the prefix, as usual.</p>

<p>If the <m>-cache</m> option is used with a true value, then the
<m>xpathQuery</m> will be looked up in a document specific cache. If the query
is found, then the stored pre-compiled query will be used. If the
query isn't found, it will be pre-compiled and stored in the cache,
for use in further calls. Please notice, that the <m>xpathQuery</m> as given as
string is used as key for the cache. This means, that equal XPath
expressions, which differ only in white space are treated as
different cache entries. Special care is needed, if the XPath
expression includes namespace prefixes. During pre-compilation, the
prefixes will be resolved first to the prefix / namespace pairs of
the <m>-namespaces</m> option, if given, and to the namespaces
in scope of the context node at pre-compilation time. If the XPath
is found in the cache, neither the <m>-namespaces</m> option nor
the namespaces in scope of the context node will be taken in
account but the already resolved (stored) namespaces will be used
for the query.</p>

<p>Examples:</p>
          <example>set paragraphNodes [$node selectNodes {chapter[3]//para[@type='warning' or @type='error'} ]
foreach paragraph $paragraphNodes {
    lappend  values [$paragraph selectNodes attribute::type]
}

set doc [dom parse {&lt;doc xmlns="http://www.defaultnamespace.org"&gt;&lt;child/&gt;&lt;/doc&gt;}]
set root [$doc documentElement]
set childNodes [$root selectNodes -namespaces {default http://www.defaultnamespace.org} default:child]</example>

          </desc>
      </commanddef>

      <commanddef>
        <command><method>baseURI</method> <m>?URI?</m></command>
        <desc>Returns the present baseURI of the document. If the optional 
argument URI is given, sets the base URI of the document to the given URI.</desc>
      </commanddef>

      <commanddef>
        <command><method>appendFromScript</method> <m>tclScript</m></command>
        <desc>Appends the nodes created by the <m>tclScript</m> by
Tcl functions, which have been built using <m>dom createNodeCmd</m>, at the end
of the current list of top level nodes of the document.</desc>
      </commanddef>

      <commanddef>
        <command><method>insertBeforeFromScript</method> <m>tclScript</m> <m>refChild</m></command>
        <desc>Inserts the nodes created in the <m>tclScript</m> by Tcl
functions, which have been built using <m>dom
createNodeCmd</m>, before the <m>refChild</m> into to the list
of top level nodes of the document. If <m>refChild</m> is the
empty string, the new nodes will be appended.</desc>
      </commanddef>

      <commanddef>
        <command><method>deleteXPathCache</method> <m>?xpathQuery?</m></command>
        <desc>If called without the optional argument, all cached XPath
expressions of the document are freed. If called with the optional
argument <m>xpathQuery</m>, this single XPath query will be removed
from the cache, if it is there. The method always returns an
empty string.</desc>
      </commanddef>

  </commandlist>

      <p>Otherwise, if an unknown method name is given, the command with the
same name as the given metho within the namespace <samp>::dom::domDoc</samp> is
tried to be executed. This allows quick method additions on Tcl level.</p>

      <p>Newly created nodes are appended to a hidden fragment list. If they
are not moved into the tree they are automaticaly deleted, when the whole
document gets deleted.</p>

    </section>

    <seealso>
      <ref>dom</ref>
      <ref>domNode</ref>
    </seealso>

    <keywords>
      <keyword>DOM node creation</keyword>
      <keyword>document element</keyword>
    </keywords>

</manpage>

