home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Node.pod < prev    next >
Encoding:
Text File  |  2003-08-22  |  17.1 KB  |  784 lines

  1. =head1 NAME
  2.  
  3. XML::LibXML::Node - Abstract Base Class of XML::LibXML Nodes
  4.  
  5. =head1 SYNOPSIS
  6.  
  7.   $name = $node->nodeName;
  8.   $node->setNodeName( $newName );
  9.   $bool = $node->isSameNode( $other_node );
  10.   $bool = $node->isEqual( $other_node );
  11.   $content = $node->nodeValue;
  12.   $content = $node->textContent;
  13.   $type = $node->nodeType;
  14.   $lineno = $node->line_number();
  15.   $node->unbindNode()
  16.   $childnode = $node->removeChild( $childnode )
  17.   $oldnode = $node->replaceChild( $newNode, $oldNode )
  18.   $node->replaceNode($newNode);
  19.   $childnode = $node->appendChild( $childnode );
  20.   $childnode = $node->addChild( $chilnode );
  21.   $node = $parent->addNewChild( $nsURI, $name );
  22.   $node->addSibling($newNode);
  23.   $newnode =$node->cloneNode( $deep )
  24.   $parentnode = $node->parentNode;
  25.   $nextnode = $node->nextSibling()
  26.   $prevnode = $node->previousSibling()
  27.   $boolean = $node->hasChildNodes();
  28.   $childnode = $node->firstChild;
  29.   $childnode = $node->lastChild;
  30.   $documentnode = $node->ownerDocument;
  31.   $node = $node->getOwner;
  32.   $node->setOwnerDocument( $doc );
  33.   $node->insertBefore( $newNode, $refNode )
  34.   $node->insertAfter( $newNode, $refNode )
  35.   @nodes = $node->findnodes( $xpath_statement );
  36.   $result = $node->find( $xpath );
  37.   print $node->findvalue( $xpath );
  38.   @childnodes = $node->childNodes;
  39.   $xmlstring = $node->toString($format,$docencoding);
  40.   $c14nstring = $node->toString($with_comments, $xpath_expression);
  41.   $str = $doc->serialze($format); 
  42.   $c14nstr = $doc->serialize_c14n($comment_flag,$xpath); 
  43.   $localname = $node->localname;
  44.   $nameprefix = $node->prefix;
  45.   $uri = $node->namespaceURI()
  46.   $boolean = $node->hasAttributes();
  47.   @attributelist = $node->attributes();
  48.   $URI = $node->lookupNamespaceURI( $prefix );
  49.   $prefix = $node->lookupNamespacePrefix( $URI );
  50.   $iter = $node->iterator;
  51.   $node->normalize;
  52.   @nslist = $node->getNamespaces;
  53.   $node->removeChildNodes();
  54.  
  55.  
  56. =head1 DESCRIPTION
  57.  
  58. XML::LibXML::Node defines functions that are common to all Node Types. A
  59. LibXML::Node should never be created standalone, but as an instance of a high
  60. level class such as LibXML::Element or LibXML::Text. The class itself should
  61. provide only common functionality. In XML::LibXML each node is part either of a
  62. document or a document-fragment. Because of this there is no node without a
  63. parent. This may causes confusion with "unbound" nodes.
  64.  
  65. =over 4
  66.  
  67. =item B<nodeName>
  68.  
  69.   $name = $node->nodeName;
  70.  
  71. Returns the node's name. This Function is aware about namesaces and returns the
  72. full name of the current node (prefix:localname)
  73.  
  74.  
  75.  
  76. =back
  77.  
  78. =over 4
  79.  
  80. =item B<setNodeName>
  81.  
  82.   $node->setNodeName( $newName );
  83.  
  84. In very limited situation it is usefull to change a nodes name. In the DOM
  85. specification this should throw an error. This Function is aware about
  86. namespaces.
  87.  
  88.  
  89.  
  90. =back
  91.  
  92. =over 4
  93.  
  94. =item B<isSameNode>
  95.  
  96.   $bool = $node->isSameNode( $other_node );
  97.  
  98. returns TRUE (1) if the given nodes refer to the same node structure, otherwise
  99. FALSE (0) is returned.
  100.  
  101.  
  102.  
  103. =back
  104.  
  105. =over 4
  106.  
  107. =item B<isEqual>
  108.  
  109.   $bool = $node->isEqual( $other_node );
  110.  
  111. depraced version of isSameNode().
  112.  
  113. NOTE isEqual will change behaviour to follow the DOM specification
  114.  
  115.  
  116.  
  117. =back
  118.  
  119. =over 4
  120.  
  121. =item B<nodeValue>
  122.  
  123.   $content = $node->nodeValue;
  124.  
  125. If the node has any content (such as stored in a text node) it can get
  126. requested through this function.
  127.  
  128. NOTE: Element Nodes have no content per definition. To get the text value of an
  129. Element use textContent() instead!
  130.  
  131.  
  132.  
  133. =back
  134.  
  135. =over 4
  136.  
  137. =item B<textContent>
  138.  
  139.   $content = $node->textContent;
  140.  
  141. this function returns the content of all text nodes in the descendants of the
  142. given node as spacified in DOM.
  143.  
  144.  
  145.  
  146. =back
  147.  
  148. =over 4
  149.  
  150. =item B<nodeType>
  151.  
  152.   $type = $node->nodeType;
  153.  
  154. Retrun the node's type. The possible types are described in the libxml2 tree.h
  155. documentation. The return value of this function is a numeric value. Therefore
  156. it differst with the result of perl ref function.
  157.  
  158.  
  159. =item B<line_number>
  160.  
  161.   $lineno = $node->line_number();
  162.  
  163. This function returns the line number where the tag was found during parsing.
  164. If a node is added to the document the line number is 0. Problems may occour,
  165. if a node from one document is passed to another one.
  166.  
  167. Note: line_number() is special to XML::LibXML and not part of the DOM
  168. specification.
  169.  
  170. If the line_numbers flag of the parser was not activated before parsing,
  171. line_number() will always return 0.
  172.  
  173.  
  174.  
  175. =back
  176.  
  177. =over 4
  178.  
  179. =item B<unbindNode>
  180.  
  181.   $node->unbindNode()
  182.  
  183. Unbinds the Node from its siblings and Parent, but not from the Document it
  184. belongs to. If the node is not inserted into the DOM afterwards it will be lost
  185. after the programm terminated. From a low level view, the unbound node is
  186. stripped from the context it is and inserted into a (hidden) document-fragment.
  187.  
  188.  
  189.  
  190. =back
  191.  
  192. =over 4
  193.  
  194. =item B<removeChild>
  195.  
  196.   $childnode = $node->removeChild( $childnode )
  197.  
  198. This will unbind the Child Node from its parent $node. The function returns the
  199. unbound node. If oldNode is not a child of the given Node the function will
  200. fail.
  201.  
  202.  
  203.  
  204. =back
  205.  
  206. =over 4
  207.  
  208. =item B<replaceChild>
  209.  
  210.   $oldnode = $node->replaceChild( $newNode, $oldNode )
  211.  
  212. Replaces the $oldNode with the $newNode. The $oldNode will be unbound from the
  213. Node. This function differs from the DOM L2 specification, in the case, if the
  214. new node is not part of the document, the node will be imported first.
  215.  
  216.  
  217.  
  218. =back
  219.  
  220. =over 4
  221.  
  222. =item B<replaceNode>
  223.  
  224.   $node->replaceNode($newNode);
  225.  
  226. This function is very similar to replaceChild(), but it replaces the node
  227. itself rather than a childnode. This is useful if a node found by any XPath
  228. function, should be replaced.
  229.  
  230.  
  231.  
  232. =back
  233.  
  234. =over 4
  235.  
  236. =item B<appendChild>
  237.  
  238.   $childnode = $node->appendChild( $childnode );
  239.  
  240. The function will add the $childnode to the end of $node's children. The
  241. function should fail, if the new childnode is allready a child of $node. This
  242. function differs from the DOM L2 specification, in the case, if the new node is
  243. not part of the document, the node will be imported first.
  244.  
  245.  
  246.  
  247. =back
  248.  
  249. =over 4
  250.  
  251. =item B<addChild>
  252.  
  253.   $childnode = $node->addChild( $chilnode );
  254.  
  255. As an alternative to appendChild() one can use the addChild() function. This
  256. function is a bit faster, because it avoids all DOM confomity checks. Therefore
  257. this function is quite usefull if one builds XML documents in memory where the
  258. order and ownership (ownerDocument) is ashured.
  259.  
  260. addChild() uses libxml2's own xmlAddChild() function. Thus it has to be used
  261. with extra care: If a text node is added to a node and the node itself or its
  262. last childnode is aswell a text node, the node to add will be merged with the
  263. one already available. The current node will be removed from memory after this
  264. action. Because perl is not aware about this action, the perl instance is still
  265. available. XML::LibXML will catch the loss of a node an avoid to run any
  266. function called on that node.
  267.  
  268.    my $t1 = $doc->createTextNode( "foo" );
  269.    my $t2 = $doc->createTextNode( "bar" );
  270.    $t1->addChild( $t2 );       # is ok
  271.    my $val = $t2->nodeValue(); # will fail, script dies
  272.  
  273. Also addChild() will not check it the added node belongs to the same document
  274. as the node it will be added to. This could lead to inconsistent documents and
  275. in more worse cases even to memory violations, if one does not keep track of
  276. this issue.
  277.  
  278. Although this sounds like a lot of trouble, addChild() is usefull if a document
  279. is build from a stream, such as happens sometimes in SAX handlers or filters.
  280.  
  281. If you are not shure about the source of your nodes, you better stay with
  282. appendChild(), because this function is more user friendly in the sense of more
  283. error tolerance.
  284.  
  285.  
  286.  
  287. =back
  288.  
  289. =over 4
  290.  
  291. =item B<addNewChild>
  292.  
  293.   $node = $parent->addNewChild( $nsURI, $name );
  294.  
  295. Similar to addChild(), this function uses low level libxml2 functionality to
  296. provide faster interface for DOM building. addNewChild() uses xmlNewChild() to
  297. create a new node on a given parent element.
  298.  
  299. addNewChild() has two parameters $nsURI and $name, where $nsURI is an
  300. (optional) namespace URI. $name is the fully qualified element name;
  301. addNewChild() will determine the correct prefix if nessecary.
  302.  
  303. The function returns the newly created node.
  304.  
  305. This function is very usefull for DOM building, where a created node can be
  306. directly associated to its parent. NOTE this function is not part of the DOM
  307. specification and its use will limit your code to XML::LibXML.
  308.  
  309.  
  310.  
  311. =back
  312.  
  313. =over 4
  314.  
  315. =item B<addSibling>
  316.  
  317.   $node->addSibling($newNode);
  318.  
  319. addSibling() allows to add an additional node to the end of a nodelist, defined
  320. by the given node.
  321.  
  322.  
  323.  
  324. =back
  325.  
  326. =over 4
  327.  
  328. =item B<cloneNode>
  329.  
  330.   $newnode =$node->cloneNode( $deep )
  331.  
  332. cloneNode creates a copy of $node. Wether $deep is set to 1 (true) the function
  333. will copy all childnodes as well. If $deep is 0 only the current node will be
  334. copied.
  335.  
  336. cloneNode will not copy any namespace information if it is not run recursivly.
  337.  
  338.  
  339.  
  340. =back
  341.  
  342. =over 4
  343.  
  344. =item B<parentNode>
  345.  
  346.   $parentnode = $node->parentNode;
  347.  
  348. Returns simply the Parent Node of the current node.
  349.  
  350.  
  351.  
  352. =back
  353.  
  354. =over 4
  355.  
  356. =item B<nextSibling>
  357.  
  358.   $nextnode = $node->nextSibling()
  359.  
  360. Returns the next sibling if any .
  361.  
  362.  
  363.  
  364. =back
  365.  
  366. =over 4
  367.  
  368. =item B<previousSibling>
  369.  
  370.   $prevnode = $node->previousSibling()
  371.  
  372. Analogous to getNextSibling the function returns the previous sibling if any.
  373.  
  374.  
  375.  
  376. =back
  377.  
  378. =over 4
  379.  
  380. =item B<hasChildNodes>
  381.  
  382.   $boolean = $node->hasChildNodes();
  383.  
  384. If the current node has Childnodes this function returns TRUE (1), otherwise it
  385. returns FALSE (0, not undef).
  386.  
  387.  
  388.  
  389. =back
  390.  
  391. =over 4
  392.  
  393. =item B<firstChild>
  394.  
  395.   $childnode = $node->firstChild;
  396.  
  397. If a node has childnodes this function will return the first node in the
  398. childlist.
  399.  
  400.  
  401.  
  402. =back
  403.  
  404. =over 4
  405.  
  406. =item B<lastChild>
  407.  
  408.   $childnode = $node->lastChild;
  409.  
  410. If the $node has childnodes this function returns the last child node.
  411.  
  412.  
  413.  
  414. =back
  415.  
  416. =over 4
  417.  
  418. =item B<ownerDocument>
  419.  
  420.   $documentnode = $node->ownerDocument;
  421.  
  422. Through this function it is allways possible to access the document the current
  423. node is bound to.
  424.  
  425.  
  426.  
  427. =back
  428.  
  429. =over 4
  430.  
  431. =item B<getOwner>
  432.  
  433.   $node = $node->getOwner;
  434.  
  435. This function returns the node the current node is associated with. In the very
  436. most cases this will be a document node or a document fragment node.
  437.  
  438.  
  439.  
  440. =back
  441.  
  442. =over 4
  443.  
  444. =item B<setOwnerDocument>
  445.  
  446.   $node->setOwnerDocument( $doc );
  447.  
  448. This function binds a node to another DOM. This method unbinds the node first,
  449. if it is allready bound to another document.
  450.  
  451. This function is the oposite calling of XML::LibXML::Document's adoptNode()
  452. function. Because of this it has the same limitations with Entity References as
  453. adoptNode().
  454.  
  455.  
  456.  
  457. =back
  458.  
  459. =over 4
  460.  
  461. =item B<insertBefore>
  462.  
  463.   $node->insertBefore( $newNode, $refNode )
  464.  
  465. The method inserts $newNode before $refNode. If $refNode is undefined, the
  466. newNode will be set as the new first child of the parent node. This function
  467. differs from the DOM L2 specification, in the case, if the new node is not part
  468. of the document, the node will be imported first.
  469.  
  470.  
  471.  
  472. =back
  473.  
  474. =over 4
  475.  
  476. =item B<insertAfter>
  477.  
  478.   $node->insertAfter( $newNode, $refNode )
  479.  
  480. The method inserts $newNode after $refNode. If $refNode is undefined, the
  481. newNode will be set as the new last child of the parent node.
  482.  
  483.  
  484.  
  485. =back
  486.  
  487. =over 4
  488.  
  489. =item B<findnodes>
  490.  
  491.   @nodes = $node->findnodes( $xpath_statement );
  492.  
  493. findnodes performs the xpath statement on the current node and returns the
  494. result as an array. In scalar context returns a XML::LibXML::NodeList object.
  495.  
  496.  
  497.  
  498. =back
  499.  
  500. =over 4
  501.  
  502. =item B<find>
  503.  
  504.   $result = $node->find( $xpath );
  505.  
  506. find performs the xpath expression using the current node as the context of the
  507. expression, and returns the result depending on what type of result the XPath
  508. expression had. For example, the XPath "1 * 3 + 52" results in a
  509. XML::LibXML::Number object being returned. Other expressions might return a
  510. XML::LibXML::Boolean object, or a XML::LibXML::Literal object (a string). Each
  511. of those objects uses Perl's overload feature to "do the right thing" in
  512. different contexts.
  513.  
  514.  
  515.  
  516. =back
  517.  
  518. =over 4
  519.  
  520. =item B<findvalue>
  521.  
  522.   print $node->findvalue( $xpath );
  523.  
  524. findvalue is exactly equivalent to:
  525.  
  526.    $node->find( $xpath )->to_literal;              
  527.  
  528. That is, it returns the literal value of the results. This enables you to
  529. ensure that you get a string back from your search, allowing certain shortcuts.
  530. This could be used as the equivalent of XSLT's <xsl:value-of
  531. select="some_xpath"/>.
  532.  
  533.  
  534.  
  535. =back
  536.  
  537. =over 4
  538.  
  539. =item B<childNodes>
  540.  
  541.   @childnodes = $node->childNodes;
  542.  
  543. getChildnodes implements a more intuitive interface to the childnodes of the
  544. current node. It enables you to pass all children directly to a map or grep. If
  545. this function is called in scalar context, a XML::LibXML::NodeList object will
  546. be returned.
  547.  
  548.  
  549.  
  550. =back
  551.  
  552. =over 4
  553.  
  554. =item B<toString>
  555.  
  556.   $xmlstring = $node->toString($format,$docencoding);
  557.  
  558. This is the equivalent to XML::LibXML::Document::toString for a single node.
  559. This means a node and all its childnodes will be dumped into the result string.
  560.  
  561. Additionally to the $format flag of XML::LibXML::Document, this version accepts
  562. the optional $docencoding flag. If this flag is set this function returns the
  563. string in its original encoding (the encoding of the document) rather than
  564. UTF8.
  565.  
  566.  
  567. =item B<toStringC14N>
  568.  
  569.   $c14nstring = $node->toString($with_comments, $xpath_expression);
  570.  
  571. The function is similar to toString(). Instead of simply searializing the
  572. document tree, it transforms it as it is specified in the XML-C14N
  573. Specification. Such transformation is known as canonization.
  574.  
  575. If $with_comments is 0 or not defined, the result-document will not contain any
  576. comments that exist in the original document. To include comments into the
  577. canonized document, $with_comments has to be set to 1.
  578.  
  579. The parameter $xpath_expression defines the nodeset of nodes that should be
  580. visible in the resulting document. This can be used to filter out some nodes.
  581. One has to note, that only the nodes that are part of the nodeset, will be
  582. included into the result-document. Their child-nodes will not exist in the
  583. resulting document, unless they are part of the nodeset defined by the xpath
  584. expression.
  585.  
  586. If $xpath_expression is ommitted or empty, toStringC14N() will include all
  587. nodes in the given sub-tree.
  588.  
  589. No serializing flags will be recognized by this function!
  590.  
  591.  
  592. =item B<serialize>
  593.  
  594.   $str = $doc->serialze($format); 
  595.  
  596. Alternative form of toString(). This function name added to be more conform
  597. with libxml2's examples.
  598.  
  599.  
  600. =item B<serialize_c14n>
  601.  
  602.   $c14nstr = $doc->serialize_c14n($comment_flag,$xpath); 
  603.  
  604. Alternative form of toStringC14N().
  605.  
  606.  
  607.  
  608. =back
  609.  
  610. =over 4
  611.  
  612. =item B<localname>
  613.  
  614.   $localname = $node->localname;
  615.  
  616. Returns the local name of a tag. This is the part behind the colon.
  617.  
  618.  
  619.  
  620. =back
  621.  
  622. =over 4
  623.  
  624. =item B<prefix>
  625.  
  626.   $nameprefix = $node->prefix;
  627.  
  628. Returns the prefix of a tag. This is the part before the colon.
  629.  
  630.  
  631.  
  632. =back
  633.  
  634. =over 4
  635.  
  636. =item B<namespaceURI>
  637.  
  638.   $uri = $node->namespaceURI()
  639.  
  640. returns the URI of the current namespace.
  641.  
  642.  
  643.  
  644. =back
  645.  
  646. =over 4
  647.  
  648. =item B<hasAttributes>
  649.  
  650.   $boolean = $node->hasAttributes();
  651.  
  652. returns 1 (TRUE) if the current node has any attributes set, otherwise 0
  653. (FALSE) is returned.
  654.  
  655.  
  656.  
  657. =back
  658.  
  659. =over 4
  660.  
  661. =item B<attributes>
  662.  
  663.   @attributelist = $node->attributes();
  664.  
  665. This function returns all attributes and namespace declarations assigned to the
  666. given node.
  667.  
  668. Because XML::LibXML does not implement namespace declarations and attributes
  669. the same way, it is required to test what kind of node is handled while
  670. accessing the functions result.
  671.  
  672. If this function is called in array context the attribute nodes are returned as
  673. an array. In scalar context the function will return a
  674. XML::LibXML::NamedNodeMap object.
  675.  
  676.  
  677.  
  678. =back
  679.  
  680. =over 4
  681.  
  682. =item B<lookupNamespaceURI>
  683.  
  684.   $URI = $node->lookupNamespaceURI( $prefix );
  685.  
  686. Find a namespace URI by its prefix starting at the current node.
  687.  
  688.  
  689.  
  690. =back
  691.  
  692. =over 4
  693.  
  694. =item B<lookupNamespacePrefix>
  695.  
  696.   $prefix = $node->lookupNamespacePrefix( $URI );
  697.  
  698. Find a namespace prefix by its URI starting at the current node.
  699.  
  700. NOTE Only the namespace URIs are ment to be unique. The prefix is only document
  701. related. also document might has more than a single prefix defined for a
  702. namespace.
  703.  
  704.  
  705.  
  706. =back
  707.  
  708. =over 4
  709.  
  710. =item B<iterator>
  711.  
  712.   $iter = $node->iterator;
  713.  
  714. This function is depraced sind XML::LibXML 1.54. It is only a dummyfunction
  715. that will get removed entirely in one of the next versions.
  716.  
  717. To make use of iterator functions use XML::LibXML::Iterator Module available on
  718. CPAN.
  719.  
  720.  
  721.  
  722. =back
  723.  
  724. =over 4
  725.  
  726. =item B<normalize>
  727.  
  728.   $node->normalize;
  729.  
  730. This function normalizes adjacent textnodes. This function is not as strict as
  731. libxml2's xmlTextMerge() function, since it will not free a node that is still
  732. refered by the perl layer.
  733.  
  734.  
  735.  
  736. =back
  737.  
  738. =over 4
  739.  
  740. =item B<getNamespaces>
  741.  
  742.   @nslist = $node->getNamespaces;
  743.  
  744. If a node has any namespaces defined, this function will return these
  745. namespaces. Note, that this will not return all namespaces that are in scope,
  746. but only the ones declares explicitly for that node.
  747.  
  748. Although getNamespaces is available for all nodes, it makes only sense if used
  749. with element nodes.
  750.  
  751.  
  752.  
  753. =back
  754.  
  755. =over 4
  756.  
  757. =item B<removeChildNode>
  758.  
  759.   $node->removeChildNodes();
  760.  
  761. This function is not specified for any DOM level: It removes all childnodes
  762. from a node in a single step. Other than the libxml2 function itself
  763. (xmlFreeNodeList), this function will not imediatly remove the nodes from the
  764. memory. This safes one from getting memory violations, if there are nodes still
  765. refered from the Perl level.
  766.  
  767.  
  768.  
  769. =back
  770.  
  771. =head1 AUTHORS
  772.  
  773. Matt Sergeant, 
  774. Christian Glahn, 
  775. =head1 VERSION
  776.  
  777. 1.56
  778.  
  779. =head1 COPYRIGHT
  780.  
  781. 2001-2002, AxKit.com Ltd; 2001-2003 Christian Glahn, All rights reserved.
  782.  
  783. =cut
  784.