XML namespace control
Hi Duncan,
On Oct 29, 2012, at 9:24 AM, Duncan Temple Lang wrote:
Hi Ben
Can you tell us the slightly bigger picture, please?
Do you want to create a single similar node entirely in isolation
or do you want to create it as part of an XML tree/document?
Who will be reading the resulting XML.
You can use a parent node
top = newXMLNode("storms", namespaceDefinitions = c(weather = "http://my.weather.com/events"))
Then
newXMLNode("storm", "ripsnorter", namespace = "weather",
attrs = c(type = "hurrican", name = "Sandy"),
parent = top )
That gives you
<weather:storm type="hurrican" name="Sandy">ripsnorter</weather:storm>
So now what are you going to do with that node?
The namespace prefix is local to a document, chosen by the author of that XML document.
The namespace URI is the global key that authors and consumers must agree upon.
While your database may use "udf", you may chose a different prefix or even the default
prefix to correspond to that same URI. So each document must explicitly declare the
prefix = URI mapping for it to be understood.
Ah! The parent - of course! This makes perfect sense (and says so in the docs). I think I was focussing on building up the hierarchy from the inside-to-the-outside. That's obviously backwards (of course it's obvious now that I have gone public!) I must have I missed the boat when I read the documentation for the namespace argument to newXMLNode() ... "If this is a character vector with a) one element and b) with an empty names attribute and c) whose value does not start with http:/ or ftp:/, then it is assumed that the value is a namespace prefix for a namespace defined in an ancestor node. To be able to resolve this prefix to a namespace definition, parent must be specified so that we can traverse the chain of ancestor nodes. " I wonder if that last sentence might be amended to read, "To be able to resolve this prefix to a namespace definition, parent must be specified so that we can traverse the chain of ancestor nodes _to where the namespace is defined._" I realize that this addition repeats the meaning of the previous sentence, but repetition never hurts boneheads like me. The bigger picture is that I am using this with a RESTful system which has been very kindly to this neophyte. The parent tree, of which these user defined fields are children, will be POSTed to the system (using RCurl). I have the parent just as you have shown 'top' above, but I had not included the namespace definitions that any of its children might need. I have added the namespace definitions needed to the parent definition and all is well. Thanks, once again, for making the XML package available. It is incredibly useful. Cheers, Ben
D. On 10/29/12 5:54 AM, Ben Tupper wrote:
Hello, I am working with a database system from which I can retrieve these kinds of user defined fields formed as XML ... <udf:field unit="uM" type="Numeric" name="facs.Stain final concentration">5</udf:field> You can see in the above example that "field" is defined in the namespace "udf", but that the "udf" namespace is not defined along with the attributes of the node. That is, 'xmlns:udf = "http://blah.blah.com/blah"' doesn't appear. I would like to create a similar node from scratch, but I can't seem to define the node with a namespace without providing the namespace definition. library(XML) node1 <- newXMLNode("storm", "ripsnorter", namespace = "weather", namespaceDefinitions = c(weather = "http://my.weather.com/events"), attrs = c(type = "hurricane", name = "Sandy")) node1 # this returns the new node with the namespace prefix (which I want) # and the definition (which I don't want) # <weather:storm xmlns:weather="http://my.weather.com/events" type="hurricane" name="Sandy">ripsnorter</weather:storm> node2 <- newXMLNode("storm", "ripsnorter", namespace = "weather", attrs = c(type = "hurricane", name = "Sandy"), suppressNamespaceWarning = TRUE) node2 # produces the node without the namespace prefix and without the definition # <storm type="hurricane" name="Sandy">ripsnorter</storm> Is there some way to create a node with a namespace prefix but without embedding the namespace definition along with the attributes? Thanks! Ben Ben Tupper Bigelow Laboratory for Ocean Sciences 180 McKown Point Rd. P.O. Box 475 West Boothbay Harbor, Maine 04575-0475 http://www.bigelow.org
sessionInfo()
R version 2.15.0 (2012-03-30) Platform: i386-apple-darwin9.8.0/i386 (32-bit) locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] tripack_1.3-4 RColorBrewer_1.0-5 Biostrings_2.24.1 IRanges_1.14.2 BiocGenerics_0.2.0 RCurl_1.91-1 [7] bitops_1.0-4.1 XML_3.9-4 loaded via a namespace (and not attached): [1] stats4_2.15.0 tools_2.15.0
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Ben Tupper Bigelow Laboratory for Ocean Sciences 180 McKown Point Rd. P.O. Box 475 West Boothbay Harbor, Maine 04575-0475 http://www.bigelow.org