xmlNamespace.RdEach XML node has a namespace identifier which is a string indicating
in which DTD (Document Type Definition) the definition of that element
can be found. This avoids the problem of having different document
definitions using the same names for XML elements that have different
meaning.
To resolve the name space, i.e.
i.e. find out to where the identifier points,
one can use the
expression xmlNamespace(xmlRoot(doc)).
The class of the result is
is an S3-style object of class XMLNamespace.
xmlNamespace(x)
xmlNamespace(x, ...) <- valueFor non-root nodes, this returns a string giving the identifier of the name space for this node. For the root node, this returns a list with 2 elements:
the identifier by which other nodes refer to this namespace.
the URI or location that defines this namespace.
? (can't remember off-hand).
doc <- xmlTreeParse(system.file("exampleData", "job.xml", package="XML"))
xmlNamespace(xmlRoot(doc))
#> [1] "gjob"
#> attr(,"class")
#> [1] "XMLNamespacePrefix"
xmlNamespace(xmlRoot(doc)[[1]][[1]])
#> [1] "gjob"
#> attr(,"class")
#> [1] "XMLNamespacePrefix"
doc <- xmlInternalTreeParse(system.file("exampleData", "job.xml", package="XML"))
# Since the first node, xmlRoot() will skip that, by default.
xmlNamespace(xmlRoot(doc))
#> gjob
#> "http://www.gnome.org/some-location"
#> attr(,"class")
#> [1] "XMLNamespace"
xmlNamespace(xmlRoot(doc)[[1]][[1]])
#> gjob
#> "http://www.gnome.org/some-location"
#> attr(,"class")
#> [1] "XMLNamespace"
node <- xmlNode("arg", xmlNode("name", "foo"), namespace="R")
xmlNamespace(node)
#> [1] "R"
doc = xmlParse('<top xmlns:r="http://www.r-project.org"><bob><code>a = 1:10</code></bob></top>')
node = xmlRoot(doc)[[1]][[1]]
xmlNamespace(node) = "r"
node
#> <r:code>a = 1:10</r:code>
doc = xmlParse('<top xmlns:r="http://www.r-project.org"><bob><code>a = 1:10</code></bob></top>')
node = xmlRoot(doc)[[1]][[1]]
xmlNamespaces(node, set = TRUE) = c(omg = "https://www.omegahat.net")
node
#> <omg:code xmlns:omg="https://www.omegahat.net">a = 1:10</omg:code>