Learn R Programming

xml2 (version 1.3.3)

xml_ns: XML namespaces.

Description

xml_ns extracts all namespaces from a document, matching each unique namespace url with the prefix it was first associated with. Default namespaces are named d1, d2 etc. Use xml_ns_rename to change the prefixes. Once you have a namespace object, you can pass it to other functions to work with fully qualified names instead of local names.

Usage

xml_ns(x)

xml_ns_rename(old, ...)

Value

A character vector with class xml_namespace so the default display is a little nicer.

Arguments

x

A document, node, or node set.

old, ...

An existing xml_namespace object followed by name-value (old prefix-new prefix) pairs to replace.

Examples

Run this code
x <- read_xml('
 
   
   
 
')
xml_ns(x)

# When there are default namespaces, it's a good idea to rename
# them to give informative names:
ns <- xml_ns_rename(xml_ns(x), d1 = "foo", d2 = "bar")
ns

# Now we can pass ns to other xml function to use fully qualified names
baz <- xml_children(xml_children(x))
xml_name(baz)
xml_name(baz, ns)

xml_find_all(x, "//baz")
xml_find_all(x, "//foo:baz", ns)

str(as_list(x))
str(as_list(x, ns))

Run the code above in your browser using DataLab