File: xml_ns.Rd

package info (click to toggle)
r-cran-xml2 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 976 kB
  • sloc: cpp: 1,826; xml: 333; javascript: 238; ansic: 178; sh: 71; makefile: 6
file content (53 lines) | stat: -rw-r--r-- 1,425 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xml_namespaces.R
\name{xml_ns}
\alias{xml_ns}
\alias{xml_ns_rename}
\title{XML namespaces.}
\usage{
xml_ns(x)

xml_ns_rename(old, ...)
}
\arguments{
\item{x}{A document, node, or node set.}

\item{old, ...}{An existing xml_namespace object followed by name-value
(old prefix-new prefix) pairs to replace.}
}
\value{
A character vector with class \code{xml_namespace} so the
default display is a little nicer.
}
\description{
\code{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 \code{d1}, \code{d2} etc. Use \code{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.
}
\examples{
x <- read_xml('
 <root>
   <doc1 xmlns = "http://foo.com"><baz /></doc1>
   <doc2 xmlns = "http://bar.com"><baz /></doc2>
 </root>
')
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))
}