Learn R Programming

XML (version 0.99-92)

getNodeSet: Find matching nodes in an internal XML tree/DOM

Description

This function provides a way to find XML nodes that match a particular criterion. It uses the XPath syntax and allows quite powerful expressions for identifying nodes. The XPath language requires some knowledge, but tutorials are available.

Usage

getNodeSet(doc, path, namespaces = character())

Arguments

doc
an object of class XMLInternalDocument
path
a string (character vector of length 1) giving the XPath expression to evaluate.
namespaces
a named character vector giving the namespace prefix and URI pairs that are to be used in the XPath expression and matching of nodes. The prefix is just a simple string that acts as a short-hand or alias for the URI that is the unique ident

Value

  • The results can currently be different based on the returned value from the XPath expression evaluation:
  • lista node set
  • numerica number
  • logicala boolean
  • charactera string, i.e. a single character element.

Details

This calls the libxml routine xmlXPathEval.

References

http://xmlsoft.org, http://www.w3.org/xml http://www.w3.org/TR/xpath http://www.omegahat.org/RSXML

See Also

xmlTreeParse with useInternalNodes as TRUE.

Examples

Run this code
doc = xmlTreeParse(system.file("exampleData", "tagnames.xml", package = "XML"), useInternalNodes = TRUE)
 getNodeSet(doc, "/doc//b[@status]")
 getNodeSet(doc, "/doc//b[@status='foo']")

 
 els = getNodeSet(doc, "/doc//a[@status]")
 sapply(els, function(el) xmlGetAttr(el, "status"))

  # Using a namespace
 f = system.file("exampleData", "SOAPNamespaces.xml", package = "XML") 
 z = xmlTreeParse(f, useInternal = TRUE)
 getNodeSet(z, "/a:Envelope/a:Body", c("a" = "http://schemas.xmlsoap.org/soap/envelope/"))
 getNodeSet(z, "//a:Body", c("a" = "http://schemas.xmlsoap.org/soap/envelope/"))

  # Get two items back with namespaces
 f = system.file("exampleData", "gnumeric.xml", package = "XML") 
 z = xmlTreeParse(f, useInternal = TRUE)
 getNodeSet(z, "//gmr:Item/gmr:name", c(gmr="http://www.gnome.org/gnumeric/v2"))

Run the code above in your browser using DataLab