File: xml_attr.R

package info (click to toggle)
r-cran-xml2 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 976 kB
  • sloc: cpp: 1,828; xml: 333; javascript: 238; ansic: 213; sh: 74; makefile: 6
file content (230 lines) | stat: -rw-r--r-- 5,780 bytes parent folder | download | duplicates (2)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#' Retrieve an attribute.
#'
#' `xml_attrs()` retrieves all attributes values as a named character
#' vector, `xml_attrs() <-` or `xml_set_attrs()` sets all attribute
#' values. `xml_attr()` retrieves the value of single attribute and
#' `xml_attr() <-` or `xml_set_attr()` modifies its value. If the
#' attribute doesn't exist, it will return `default`, which defaults to
#' `NA`. `xml_has_attr()` tests if an attribute is present.
#'
#' @inheritParams xml_name
#' @param attr Name of attribute to extract.
#' @param default Default value to use when attribute is not present.
#' @return `xml_attr()` returns a character vector. `NA` is used
#'  to represent of attributes that aren't defined.
#'
#'  `xml_has_attr()` returns a logical vector.
#'
#'  `xml_attrs()` returns a named character vector if `x` x is single
#'  node, or a list of character vectors if given a nodeset
#' @export
#' @examples
#' x <- read_xml("<root id='1'><child id ='a' /><child id='b' d='b'/></root>")
#' xml_attr(x, "id")
#' xml_attr(x, "apple")
#' xml_attrs(x)
#'
#' kids <- xml_children(x)
#' kids
#' xml_attr(kids, "id")
#' xml_has_attr(kids, "id")
#' xml_attrs(kids)
#'
#' # Missing attributes give missing values
#' xml_attr(xml_children(x), "d")
#' xml_has_attr(xml_children(x), "d")
#'
#' # If the document has a namespace, use the ns argument and
#' # qualified attribute names
#' x <- read_xml('
#'  <root xmlns:b="http://bar.com" xmlns:f="http://foo.com">
#'    <doc b:id="b" f:id="f" id="" />
#'  </root>
#' ')
#' doc <- xml_children(x)[[1]]
#' ns <- xml_ns(x)
#'
#' xml_attrs(doc)
#' xml_attrs(doc, ns)
#'
#' # If you don't supply a ns spec, you get the first matching attribute
#' xml_attr(doc, "id")
#' xml_attr(doc, "b:id", ns)
#' xml_attr(doc, "id", ns)
#'
#' # Can set a single attribute with `xml_attr() <-` or `xml_set_attr()`
#' xml_attr(doc, "id") <- "one"
#' xml_set_attr(doc, "id", "two")
#'
#' # Or set multiple attributes with `xml_attrs()` or `xml_set_attrs()`
#' xml_attrs(doc) <- c("b:id" = "one", "f:id" = "two", "id" = "three")
#' xml_set_attrs(doc, c("b:id" = "one", "f:id" = "two", "id" = "three"))
xml_attr <- function(x, attr, ns = character(), default = NA_character_) {
  .Call(node_attr, x, attr, as.character(default), ns)
}

#' @export
#' @rdname xml_attr
xml_has_attr <- function(x, attr, ns = character()) {
  !is.na(xml_attr(x, attr, ns = ns))
}

#' @export
#' @rdname xml_attr
xml_attrs <- function(x, ns = character()) {
  .Call(node_attrs, x, nsMap = ns)
}

#' @param value character vector of new value.
#' @rdname xml_attr
#' @export
`xml_attr<-` <- function(x, attr, ns = character(), value) {
  UseMethod("xml_attr<-")
}

#' @export
`xml_attr<-.xml_node` <- function(x, attr, ns = character(), value) {
  if (is.null(value)) {
    .Call(node_remove_attr, x$node, attr, ns)
  } else {
    value <- as.character(value)
    .Call(node_set_attr, x$node, attr, value, ns)
  }
  x
}

#' @export
`xml_attr<-.xml_nodeset` <- function(x, attr, ns = character(), value) {
  if (length(x) == 0) {
    return(x)
  }

  if (length(value) == 0) {
    value <- list(value)
  }

  mapply(
    `xml_attr<-`,
    x,
    attr = attr,
    value = value,
    SIMPLIFY = FALSE,
    MoreArgs = list(ns = ns)
  )
  x
}

#' @export
`xml_attr<-.xml_missing` <- function(x, attr, ns = character(), value) {
  x
}

#' @rdname xml_attr
#' @export
xml_set_attr <- function(x, attr, value, ns = character()) {
  UseMethod("xml_set_attr")
}

# This function definition is used for all methods, we need to rearrange the `ns`
# argument to be at the end of the set function
set_attr_fun <- function(x, attr, value, ns = character()) {
  xml_attr(x = x, attr = attr, ns = ns) <- value
}

#' @export
xml_set_attr.xml_node <- set_attr_fun

#' @export
xml_set_attr.xml_nodeset <- set_attr_fun

#' @export
xml_set_attr.xml_missing <- set_attr_fun

#' @rdname xml_attr
#' @export
`xml_attrs<-` <- function(x, ns = character(), value) {
  UseMethod("xml_attrs<-")
}

#' @export
`xml_attrs<-.xml_node` <- function(x, ns = character(), value) {
  if (!is_named(value)) {
    cli::cli_abort("{.arg value} must be a named character vector or `NULL`")
  }

  attrs <- names(value)

  # as.character removes all attributes (including names)
  value <- stats::setNames(as.character(value), attrs)

  current_attrs <- names(xml_attrs(x, ns = ns))

  existing <- intersect(current_attrs, attrs)
  new <- setdiff(attrs, current_attrs)
  removed <- setdiff(current_attrs, attrs)

  # replace existing attributes and add new ones
  Map(
    function(attr, val) {
      xml_attr(x, attr, ns) <- val
    },
    attr = c(existing, new),
    value[c(existing, new)]
  )

  # Remove attributes which no longer exist
  Map(
    function(attr) {
      xml_attr(x, attr, ns) <- NULL
    },
    attr = removed
  )

  x
}

#' @export
`xml_attrs<-.xml_nodeset` <- function(x, ns = character(), value) {
  if (length(x) == 0) {
    return(x)
  }
  if (!is.list(ns)) {
    ns <- list(ns)
  }
  if (!is.list(value)) {
    value <- list(value)
  }
  if (!all(vapply(value, is_named, logical(1)))) {
    cli::cli_abort("{.arg {value}} must be a list of named character vectors.")
  }

  Map(`xml_attrs<-`, x, ns, value)

  x
}

#' @export
`xml_attrs<-.xml_missing` <- function(x, ns = character(), value) {
  x
}

#' @rdname xml_attr
#' @export
xml_set_attrs <- function(x, value, ns = character()) {
  UseMethod("xml_set_attrs")
}

# This function definition is used for all methods, we need to rearrange the `ns`
# argument to be at the end of the set function
set_attrs_fun <- function(x, value, ns = character()) {
  xml_attrs(x = x, ns = ns) <- value
}

#' @export
xml_set_attrs.xml_node <- set_attrs_fun

#' @export
xml_set_attrs.xml_nodeset <- set_attrs_fun

#' @export
xml_set_attrs.xml_missing <- set_attrs_fun