1\name{xmlGetAttr} 2\alias{xmlGetAttr} 3\title{Get the value of an attribute in an XML node} 4\description{ 5 This is a convenience function that retrieves 6 the value of a named attribute in an XML node, 7 taking care of checking for its existence. 8 It also allows the caller to provide a default 9 value to use as the return value if the 10 attribute is not present. 11} 12\usage{ 13xmlGetAttr(node, name, default = NULL, converter = NULL, 14 namespaceDefinition = character(), 15 addNamespace = length(grep(":", name)) > 0) 16} 17%- maybe also `usage' for other objects documented here. 18\arguments{ 19 \item{node}{the XML node} 20 \item{name}{the name of the attribute} 21 \item{default}{a value to use as the default return if the attribute 22 is not present in the XML node. } 23 \item{converter}{an optional function which if supplied is invoked 24 with the attribute value and the value returned. 25 This can be used to convert the string to an arbitrary 26 value which is useful if it is, for example, a number. 27 This is only called if the attribute exists within the node. 28 In other words, it is not applied to the \code{default} value.} 29 \item{namespaceDefinition}{a named character vector giving 30 name space prefixes and URIs to use when resolving for the 31 the attribute with a namespace. 32 The values are used to compare the name space prefix used in 33 the \code{name} given by the user to the name space 34 definition in the node to ensure they match. 35 This is important as we might ask for an attribute named 36 \code{r:width} assuming that the prefix \code{r} corresponded to the 37 URI \code{http://www.r-project.org}. However, there may 38 be a name space prefix \code{r} defined on the node that points 39 to a different URI and so this would be an erroneous match. 40 } 41 \item{addNamespace}{a logical value that indicates whether we should put the 42 namespace prefix on the resulting name. 43 This is passed on to \code{\link{xmlAttrs}} and so controls whether the resulting 44 attribute names have the prefix attached. 45 So one specifies \code{TRUE} for this argument if the attribute identifier 46 has a namespace prefix. 47 } 48} 49\details{ 50 This just checks that the attribute list is 51 non-NULL and that there is an element with 52 the specified name. 53} 54\value{ 55 If the 56 attribute is present, 57 the return value is a string which is the value of the attribute. 58 Otherwise, the value of \code{default} is returned. 59} 60\references{\url{http://www.w3.org/XML}, \url{http://www.jclark.com/xml}, 61\url{http://www.omegahat.net} } 62\author{ Duncan Temple Lang } 63 64\seealso{ 65 \code{\link{xmlAttrs}} 66} 67 68\examples{ 69 node <- xmlNode("foo", attrs=c(a="1", b="my name")) 70 71 xmlGetAttr(node, "a") 72 xmlGetAttr(node, "doesn't exist", "My own default value") 73 74 xmlGetAttr(node, "b", "Just in case") 75} 76\keyword{file} 77 78