xmlParent                package:XML                R Documentation

_G_e_t _p_a_r_e_n_t _n_o_d_e _o_f _X_M_L_I_n_t_e_r_n_a_l_N_o_d_e _o_r _a_n_c_e_s_t_o_r _n_o_d_e_s

_D_e_s_c_r_i_p_t_i_o_n:

     'xmlParent' operates on an XML node and returns a reference to its
     parent node  within the document tree. This works for an internal,
     C-level 'XMLInternalNode' object created, for examply, using
     'newXMLNode' and related functions or 'xmlTree' or  from
     'xmlTreeParse' with the 'useInternalNodes' parameter.

     It is possible to find the parent of an R-level XML node when
     using a tree created with, for example,  'xmlHashTree' as the
     parent information is stored separately.

     'xmlAncestors' walks the chain of parens to the top of the
     document and either returns a list of those nodes, or
     alternatively a list of the values obtained by applying a function
     to each of the nodes.

_U_s_a_g_e:

     xmlParent(x)
     xmlAncestors(x, fun = NULL, ...)

_A_r_g_u_m_e_n_t_s:

       x: an object of class 'XMLInternalNode' whose parent is being
          requested. 

     fun: an R function which is invoked for each  node as we walk up
          the tree.

     ...: any additional arguments that are passed in calls to 'fun'
          after the node object.

_D_e_t_a_i_l_s:

     This uses the internal libxml structures to access the parent in
     the DOM tree.  This function is generic so that we can add methods
     for other types of nodes if we so want in the future.

_V_a_l_u_e:

     'xmlParent' returns object of class 'XMLInternalNode'.

     If 'fun' is 'NULL', 'xmlAncestors' returns a list of the nodes in
     order of top-most node or root of the tree, then its child, then
     the child of that child, etc. This is the reverse order in which
     the nodes are visited/found.

     If 'fun' is a function, 'xmlAncestors' returns a list whose
     elements are the results of calling that function for each node.
     Again, the order is top down.

_A_u_t_h_o_r(_s):

     Duncan Temple Lang

_R_e_f_e_r_e_n_c_e_s:

     <URL: http://www.w3.org/XML>

_S_e_e _A_l_s_o:

     'xmlChildren' 'xmlTreeParse' 'xmlNode'

_E_x_a_m_p_l_e_s:

       top = newXMLNode("doc")
       s = newXMLNode("section", attr = c(title = "Introduction"))
       a = newXMLNode("article", s)
       addChildren(top, a)

       xmlName(xmlParent(s))
       xmlName(xmlParent(xmlParent(s)))

         # Find the root node.
       root = a
       while(!is.null(xmlParent(root)))
           root = xmlParent(root)

        # find the names of the parent nodes of each 'h' node.
        # use a global variable to "simplify" things and not use a closure.

       filename = system.file("exampleData", "branch.xml", package = "XML")
       parentNames <- character()
       xmlTreeParse(filename,
                     handlers =
                       list(h = function(x) {
                        parentNames <<- c(parentNames, xmlName(xmlParent(x)))
                       }), useInternalNodes = TRUE)

       table(parentNames)

