Tuszynski, Jaroslaw W.
2005-May-27 14:14 UTC
[R] xmlAttrs and problems with reading node attributes of XML file (b ug?)
Hi, Consider the following code: require(XML) xmlFile = paste( "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n", "<mzXML xmlns=\"a\" xmlns:xsi=\"b\" xsi:schemaLocation=\"c\">\n", "<parentFile a=\"a\" b=\"b\" />\n", "</mzXML>\n") cat(xmlFile) a = function(x,...){ cat("Attributes of ", xmlName(x), ": "); print(xmlAttrs(x)); cat("\n"); NULL } xmlTreeParse(file=xmlFile, asText=TRUE, handlers=list("startElement"=a) ) And its output: <?xml version="1.0" encoding="ISO-8859-1"?> <mzXML xmlns="a" xmlns:xsi="b" xsi:schemaLocation="c"> <parentFile a="a" b="b" /> </mzXML> Attributes of parentFile : a b "a" "b" Attributes of mzXML : schemaLocation "c" It seems to me that XML parser was able to correctly list all the attributes of the "parentFile" node but it failed on "mzXML" node. Am I missusing the functions somehow or is it a bug? Attribute names in "mzXML" node are part of mzXML file format and can not be changed (removing all ':' would fix the problem) and I would like to store than so whan I write mzXML file I have the same header. Jarek ====================================================\====== Jarek Tuszynski, PhD. o / \ Science Applications International Corporation <\__,| (703) 676-4192 "> \ Jaroslaw.W.Tuszynski@saic.com ` \ [[alternative HTML version deleted]]
Tuszynski, Jaroslaw W.
2005-May-31 17:00 UTC
[R] xmlAttrs and problems with reading node attributes of XML file (b ug?)
Duncan, Thanks, that that make sense, I have never encountered namespaces so I did not recognized them. So in my example xmlns="a" xmlns:xsi="b" are namespace variables, and xsi:schemaLocation="c" is a "Schema-Related Markup" attribute. This last one seemed to be handled strangely, since it is listed as attribute but its name got truncated from "xsi:schemaLocation" to "schemaLocation". Jarek -----Original Message----- From: Duncan Temple Lang [mailto:duncan at wald.ucdavis.edu] Sent: Tuesday, May 31, 2005 11:38 AM To: Tuszynski, Jaroslaw W. Cc: (r-help at stat.math.ethz.ch.); 'duncan at research.bell-labs.com' Subject: Re: [R] xmlAttrs and problems with reading node attributes of XML file (b ug?) Hi Jarek. Is the problem you are referring to simply that the xmlns="a" and xmlns:xsi="b" are not appearing when your handler prints the attributes? If it is this the case, the explanation is quite simple - they are namespace definitions, not attributes. They are in the node object x$namespaceDefinitions names(unclass(x)) will help to see what is in the node. HTH, D. Tuszynski, Jaroslaw W. wrote:> Hi, > > Consider the following code: > > require(XML) > xmlFile = paste( "<?xml version=\"1.0\" > encoding=\"ISO-8859-1\"?>\n", > "<mzXML xmlns=\"a\" xmlns:xsi=\"b\" > xsi:schemaLocation=\"c\">\n", > "<parentFile a=\"a\" b=\"b\" />\n", > "</mzXML>\n") > cat(xmlFile) > > a = function(x,...){ > cat("Attributes of ", xmlName(x), ": "); > print(xmlAttrs(x)); > cat("\n"); > NULL > } > > xmlTreeParse(file=xmlFile, asText=TRUE, > handlers=list("startElement"=a) ) > > And its output: > > <?xml version="1.0" encoding="ISO-8859-1"?> > <mzXML xmlns="a" xmlns:xsi="b" xsi:schemaLocation="c"> > <parentFile a="a" b="b" /> > </mzXML> > > Attributes of parentFile : a b > "a" "b" > > Attributes of mzXML : schemaLocation > "c" > It seems to me that XML parser was able to correctly list all the > attributes of the "parentFile" node but it failed on "mzXML" node. > Am I missusing the functions somehow or is it a bug? > Attribute names in "mzXML" node are part of mzXML file format and can > not be changed (removing all ':' would fix the problem) and I would > like to store than so whan I write mzXML file I have the same header. > > Jarek > ====================================================\======> > Jarek Tuszynski, PhD. o / \ > Science Applications International Corporation <\__,| > (703) 676-4192 "> \ > Jaroslaw.W.Tuszynski at saic.com ` \ > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html