Hi, Why is the following retuning a nodset of length 0:> library(XML) > test <- xmlTreeParse( > "http://www.unimod.org/xml/unimod_tables.xml",useInternalNodes=TRUE) > getNodeSet(test,"//modifications_row")Thanks for any hint. Joh
Hi Johannes This is a common issue. The document has a default XML namespace, e.g. the root node is defined as <unimod xmlns="http://www.unimod.org/xmlns/schema/unimod_tables_1"...> ..... So you need to specify which namespace to match in the XPath expression in getNodeSet(). The XML package provides a "convenient" facility for this. You need only specify the prefix such as "x" and that will be bound to the default namespace. You need to specify this in two places - where you use it in the XPath expression and in the namespaces argument of getNodeSet() So getNodeSet(test, "//x:modifications_row", "x") gives you probably what you want. D. On 8/30/10 8:02 AM, Johannes Graumann wrote:> library(XML) >> test <- xmlTreeParse( >> "http://www.unimod.org/xml/unimod_tables.xml",useInternalNodes=TRUE) >> getNodeSet(test,"//modifications_row")
Thanks! but:> library(XML) > xmlDoc <- xmlTreeParse("http://www.unimod.org/xml/unimod_tables.xml") > getNodeSet(xmlDoc,"//x:modifications_row", "x")Error in function (classes, fdef, mtable) : unable to find an inherited method for function "saveXML", for signature "XMLDocument" ? Thanks, Joh Duncan Temple Lang wrote:> > Hi Johannes > > This is a common issue. The document has a default XML namespace, e.g. > the root node is defined as > > <unimod xmlns="http://www.unimod.org/xmlns/schema/unimod_tables_1"...> > ..... > > So you need to specify which namespace to match in the XPath expression > in getNodeSet(). The XML package provides a "convenient" facility for > this. You need only specify the prefix such as "x" and that will > be bound to the default namespace. You need to specify this in > two places - where you use it in the XPath expression and > in the namespaces argument of getNodeSet() > > So > getNodeSet(test, "//x:modifications_row", "x") > > gives you probably what you want. > > D. > > > > On 8/30/10 8:02 AM, Johannes Graumann wrote: >> library(XML) >>> test <- xmlTreeParse( >>> "http://www.unimod.org/xml/unimod_tables.xml",useInternalNodes=TRUE) >>> getNodeSet(test,"//modifications_row")
Johannes Graumann wrote:> Thanks! > but: > > library(XML) > > xmlDoc <- xmlTreeParse("http://www.unimod.org/xml/unimod_tables.xml")You need to xmlParse() or xmlTreeParse(url, useInternalNodes = TRUE) (which are equivalent) in order to be able to use getNodeSet(). The error you are getting is because you are using xmlTreeParse() and the result is a tree represented in R rather than internal C-level data structures on which getNodeSet() can operate. xmlParse() is faster than xmlTreeParse() and one can use XPath to query it. D.> > getNodeSet(xmlDoc,"//x:modifications_row", "x") > Error in function (classes, fdef, mtable) : > unable to find an inherited method for function "saveXML", for signature > "XMLDocument" > > ? > > Thanks, Joh > > > Duncan Temple Lang wrote: > > > > > Hi Johannes > > > > This is a common issue. The document has a default XML namespace, e.g. > > the root node is defined as > > > > <unimod xmlns="http://www.unimod.org/xmlns/schema/unimod_tables_1"...> > > ..... > > > > So you need to specify which namespace to match in the XPath expression > > in getNodeSet(). The XML package provides a "convenient" facility for > > this. You need only specify the prefix such as "x" and that will > > be bound to the default namespace. You need to specify this in > > two places - where you use it in the XPath expression and > > in the namespaces argument of getNodeSet() > > > > So > > getNodeSet(test, "//x:modifications_row", "x") > > > > gives you probably what you want. > > > > D. > > > > > > > > On 8/30/10 8:02 AM, Johannes Graumann wrote: > >> library(XML) > >>> test <- xmlTreeParse( > >>> "http://www.unimod.org/xml/unimod_tables.xml",useInternalNodes=TRUE) > >>> getNodeSet(test,"//modifications_row") > > ______________________________________________ > 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.-- "There are men who can think no deeper than a fact" - Voltaire Duncan Temple Lang duncan at wald.ucdavis.edu Department of Statistics work: (530) 752-4782 4210 Mathematical Sciences Bldg. fax: (530) 752-7099 One Shields Ave. University of California at Davis Davis, CA 95616, USA -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100831/454622f2/attachment.bin>
Sorry about that - got dropped from my attempts yesterday (see the first example below, that has the "useInternalNodes=TRUE") ... Thanks again, Joh Duncan Temple Lang wrote:> Johannes Graumann wrote: >> Thanks! >> but: >> > library(XML) >> > xmlDoc <- xmlTreeParse("http://www.unimod.org/xml/unimod_tables.xml") > > You need to xmlParse() or xmlTreeParse(url, useInternalNodes = TRUE) > (which are equivalent) in order to be able to use getNodeSet(). > > The error you are getting is because you are using xmlTreeParse() > and the result is a tree represented in R rather than internal > C-level data structures on which getNodeSet() can operate. > > xmlParse() is faster than xmlTreeParse() > and one can use XPath to query it. > > D. > >> > getNodeSet(xmlDoc,"//x:modifications_row", "x") >> Error in function (classes, fdef, mtable) : >> unable to find an inherited method for function "saveXML", for >> signature >> "XMLDocument" >> >> ? >> >> Thanks, Joh >> >> >> Duncan Temple Lang wrote: >> >> > >> > Hi Johannes >> > >> > This is a common issue. The document has a default XML namespace, >> > e.g. >> > the root node is defined as >> > >> > <unimod xmlns="http://www.unimod.org/xmlns/schema/unimod_tables_1"...> >> > ..... >> > >> > So you need to specify which namespace to match in the XPath >> > expression >> > in getNodeSet(). The XML package provides a "convenient" facility for >> > this. You need only specify the prefix such as "x" and that will >> > be bound to the default namespace. You need to specify this in >> > two places - where you use it in the XPath expression and >> > in the namespaces argument of getNodeSet() >> > >> > So >> > getNodeSet(test, "//x:modifications_row", "x") >> > >> > gives you probably what you want. >> > >> > D. >> > >> > >> > >> > On 8/30/10 8:02 AM, Johannes Graumann wrote: >> >> library(XML) >> >>> test <- xmlTreeParse( >> >>> "http://www.unimod.org/xml/unimod_tables.xml",useInternalNodes=TRUE) >> >>> getNodeSet(test,"//modifications_row") >> >> ______________________________________________ >> 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. >