[R] XML namespace control

Duncan Temple Lang dtemplelang at ucdavis.edu
Mon Oct 29 14:24:17 CET 2012


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.

    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.
> 
>




More information about the R-help mailing list