[R] another XML package question

Duncan Temple Lang dtemplelang at ucdavis.edu
Mon Sep 8 12:09:56 CEST 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



Antje wrote:
> Hi there,
> 
> does anybody know how to return the xmlPath from a node?
> For example, at several location in the xml file, I have nodes with the
> same name and I'd like to process only the nodes from a certain path.
> 
> Any idea?

As with your previous question, there are ways to do this
with either XPath queries or R functions that operate on
the nodes from the earlier queries.

By "xmlPath", let's assume you mean the ordered collection of
nodes from the node to the root node of the document,
i.e. the collection of ancestor nodes.
So using XPath, you could use

   a = getNodeSet( node, "ancestor::*")

where node is the R variable containing the node within the tree
whose ancestors you want, e.g.
    getNodeSet(doc, "//val")[[1]]

The nodes in are in "reverse" order.


You can do the same thing with the R function
xmlParent().  To get the ancestors,

  tmp = xmlParent(node)
  ans = list()
  while( !is.null(tmp)) {
      ans = c(ans, tmp)
      tmp = xmlParent(tmp)
  }

and of course in your case you could terminate the loop
at any point.


But a different approach to the problem is to use a more specific
XPath query in the first place to get only the nodes of interest.
For example, to get the <val> nodes in the second <data> node of
your example, you could use

  getNodeSet(doc, "//data[2]/val")

or to find all <val> nodes which have the attribute  i = "t2",

   getNodeSet(doc, "//val[@i='t2']")

Or to find all <val> nodes with an ancestor which have an ancestor
with an attribute name "loc"

     getNodeSet(doc, "//*[@loc='1']//val")



(
The  sample XML document was

<root>
   <data loc="1">
     <val i="t1"> 22 </val>
     <val i="t2"> 45 </val>
   </data>
   <data loc="2">
     <val i="t1"> 44 </val>
     <val i="t2"> 11 </val>
   </data>
</root>

)


 D.

> 
> Antje
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkjE+fQACgkQ9p/Jzwa2QP6KBwCeImMuyCL0kpF/0eRqo77ywZj/
AloAn3HRWaD+RDV+ZETRagtfV7zlJpk6
=JtiD
-----END PGP SIGNATURE-----



More information about the R-help mailing list