| 1 |
<html> |
| 2 |
<head> |
| 3 |
<title>FAQ for the XML package in R/S-Plus</title> |
| 4 |
</head> |
| 5 |
|
| 6 |
<body> |
| 7 |
<h1>FAQ for the XML package in R/S-Plus</h1> |
| 8 |
|
| 9 |
<dl> |
| 10 |
|
| 11 |
<dt> |
| 12 |
<li> I have compiled the package from source and everything goes |
| 13 |
fine, but when I try to load it, I get an error about an |
| 14 |
"undefined symbol xmlOutputBufferCreateBuffer". What's the problem? |
| 15 |
<dd> |
| 16 |
The most obvious explanation is that you are <i>compiling</i> against |
| 17 |
one version of libxml2 but <i>loading</i> another. |
| 18 |
Here is the explanation I gave to several people. |
| 19 |
<div style="border: solid 1px; background-color: lightgrey; padding: 2pt"> |
| 20 |
That particular routine is either "borrowed" |
| 21 |
from libxml2.so or if not available from that, a compiled |
| 22 |
copy I added to the XML source is used. |
| 23 |
So it looks like the configuration is smart enough not |
| 24 |
to compile the one we provide because xmlOutputBufferCreateBuffer() |
| 25 |
is available in libxml2-2.6.32, but then it fails to find it |
| 26 |
at load time. |
| 27 |
<p/> |
| 28 |
So the first thing that comes to mind is that there is some inconsistency |
| 29 |
between your compile/link configuration and your run-time load configuration. |
| 30 |
Is it possible that you have an older version of libxml2...so somewhere |
| 31 |
on your system that is being loaded at run time |
| 32 |
Try the shell command |
| 33 |
<pre> |
| 34 |
R CMD ldd <wherever XML is installed>/libs/XML.so |
| 35 |
</pre> |
| 36 |
and see where it thinks libxml2 is. Hopefully it will |
| 37 |
list it and we might see that it is /usr/lib/libxml2.so |
| 38 |
and that you are compiling against a version in |
| 39 |
/usr/local/lib/. |
| 40 |
If that is the case, you would need to change your personal setting for |
| 41 |
<b>LD_LIBRARY_PATH</b> |
| 42 |
or add /usr/local/lib to /etc/ld.so.conf file at a system level. |
| 43 |
</div> |
| 44 |
|
| 45 |
<dt> |
| 46 |
<li> How do I create my own XML content from within R |
| 47 |
and not just parse other people's XML documents? |
| 48 |
<dd> |
| 49 |
There are several facilities for doing this in the XML package. |
| 50 |
Basically you will be creating a tree, a hierarchical collection |
| 51 |
of XML nodes. |
| 52 |
So you need to be able to create the nodes and then arrange them |
| 53 |
into this hierarachical structure. |
| 54 |
You can create all the nodes and then do the tree construction |
| 55 |
but it is typically easier to create the nodes and specify |
| 56 |
its parent during the creation. |
| 57 |
The XML package provides low-level functions |
| 58 |
for creating the nodes and |
| 59 |
several functions which provide a higher level interface |
| 60 |
that try to facilitate adding nodes to a 'current' point |
| 61 |
in the tree. These manage the notion of 'current' for you. |
| 62 |
These functions share a very similar interface and so |
| 63 |
it is easy to switch from one to the other. They differ |
| 64 |
in how they represent the tree which can be complicated |
| 65 |
in R since there are no references/pointers. |
| 66 |
<p/> |
| 67 |
The approach I prefer is the <code>xmlTree()</code> |
| 68 |
function. |
| 69 |
This uses the low-level node creation functions |
| 70 |
(e.g. newXMLNode, newXMLComment, newXMLPINode, etc.) |
| 71 |
but also allows us to manage a stack of "open" nodes |
| 72 |
and a default namespace prefix. |
| 73 |
New nodes are by default added to the most recent |
| 74 |
"open" node, i.e. that node acts as the parent for new nodes. |
| 75 |
<p/> |
| 76 |
|
| 77 |
<dt> |
| 78 |
<li> Doesn't the use of internal nodes such as with <code |
| 79 |
class="rfunc">xmlTree()</code> mean that we cannot store the |
| 80 |
tree across R sessions since they are external pointers to C data |
| 81 |
structures in memory. |
| 82 |
|
| 83 |
<dd> Well, yes and no. We cannot use R's regular serialization |
| 84 |
to RDA files of the variables holding the tree or the individual |
| 85 |
nodes. But we can easily create a text representation from the |
| 86 |
internal nodes by dumping the tree, either to a file or a |
| 87 |
character vector of length 1, and then we can restore |
| 88 |
that XML tree by parsing it again: |
| 89 |
<pre> |
| 90 |
tt = xmlTree("top") |
| 91 |
tt$addTag("b", "Some text") |
| 92 |
save(saveXML(tt), file = "tt.rda") |
| 93 |
load("tt.rda") |
| 94 |
tt = xmlTreeParse(tt, asText = TRUE, useInternal = TRUE) |
| 95 |
</pre> |
| 96 |
We don't get back the XMLInternalDOM with information about open |
| 97 |
nodes, etc. from which we could continue to add nodes. |
| 98 |
But we do get back the exact tree. |
| 99 |
|
| 100 |
<p/> |
| 101 |
We can also convert the nodes from internal nodes to |
| 102 |
regular R base nodes. |
| 103 |
And from that |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
<dt> |
| 109 |
<li> My XML document has attributes that have a namespace |
| 110 |
prefix (e.g. |
| 111 |
<code> |
| 112 |
<b><node mine:foo="abc" /></b> |
| 113 |
</code> |
| 114 |
) |
| 115 |
When I parse this document into S, the namespace prefix |
| 116 |
on the attribute is dropped. Why and how can I fix it? |
| 117 |
|
| 118 |
<dd> |
| 119 |
The first thing to do is |
| 120 |
use a value of <code>TRUE</code> for the <code>addAttributeNamespaces</code> |
| 121 |
argument in the call to <code>xmlTreeParse</code>. |
| 122 |
<br> |
| 123 |
The next thing is to ensure that the namespace |
| 124 |
(<code>mine</code>, in our example) |
| 125 |
is defined in the document. |
| 126 |
In other words, there must be be an |
| 127 |
<code>xmlns:mine="<i>some url</i>"</code> |
| 128 |
attribute in some node before or in the node |
| 129 |
that is being processed. |
| 130 |
If no definition for the namespace is in the document, |
| 131 |
the libxml parser drops the prefix on the attribute. |
| 132 |
<br> |
| 133 |
The same applies to namespaces for node names, and not just |
| 134 |
attributes. |
| 135 |
|
| 136 |
|
| 137 |
<dt> |
| 138 |
<li> <font class="question">I define a method in the closure, but it |
| 139 |
never gets called.</font> |
| 140 |
<dd> The most likely cause is that you omitted to add it to the list |
| 141 |
of functions returned by the closure. Another possibility is |
| 142 |
that you have mis-spelled the name of the method. The matching |
| 143 |
is case-sensitive and exact. |
| 144 |
If the function corresponds to a particular XML element name, |
| 145 |
check whether the value of the argument <code>useTagName</code> is T, |
| 146 |
and also that there really is a tag with this name in the |
| 147 |
document. |
| 148 |
Again, the case is important. |
| 149 |
|
| 150 |
<dt> |
| 151 |
<li><font class="question">When I compile the source code, I get |
| 152 |
lots of warning messages such as |
| 153 |
<pre> |
| 154 |
"RSDTD.c", line 110: warning: argument #2 is incompatible with prototype: |
| 155 |
prototype: pointer to const uchar : "unknown", line 0 |
| 156 |
argument : pointer to const char |
| 157 |
</pre> |
| 158 |
</font> |
| 159 |
<dd> This is because the XML libraries work on unsigned characters |
| 160 |
for UniCode. The R and S facilities do not. |
| 161 |
I am not yet certain which direction to adapt things for this |
| 162 |
package. The warnings are typically harmless. |
| 163 |
|
| 164 |
<dt> |
| 165 |
<li> <font class="question">When I compile the chapter for |
| 166 |
Splus5/S4, I get warning messages about SET_CLASS being redefined.</font> |
| 167 |
<dd> This is ok, in this situation. The warning is left there to |
| 168 |
remind people that there are some games being played and that |
| 169 |
if there are problems, to consider these warnings. |
| 170 |
The SET_CLASS macro being redefined is a local version for S3/R |
| 171 |
style classes. The one in the Splus5/S4 header files is for the |
| 172 |
S4 style classes. |
| 173 |
|
| 174 |
<dt> |
| 175 |
<li> <font class="question">On which platfforms does it compile?</font> |
| 176 |
<dd> I have used gcc on both Linux (RedHat 6.1) (egcs-2.91.66) |
| 177 |
and Solaris (gcc-2.7.2.3), and the Sun compilers, cc 4.2 on |
| 178 |
Solaris 2.6/SunOS 5.6 and cc 5.0 on Solaris 2.7/SunOS 5.7. |
| 179 |
</dl> |
| 180 |
|
| 181 |
|
| 182 |
<dl> |
| 183 |
|
| 184 |
|
| 185 |
<dt> |
| 186 |
<li><font class="question">I can't seem to use conditional DTD |
| 187 |
segments via the IGNORE/INCLUDE mechanism.</font> |
| 188 |
<dd> Libxml doesn't support this. Perhaps we will add code for this. |
| 189 |
<p> |
| 190 |
Daneil Veillard might add this. |
| 191 |
|
| 192 |
|
| 193 |
<dt> |
| 194 |
<li> <font class="question">When I read a relatively simple tree in |
| 195 |
Splus5 and print it to the terminal/console, I get an error |
| 196 |
about nested expressions exceeding the limit of 256. |
| 197 |
</font> |
| 198 |
<dd> The simple fix is to set the value of the |
| 199 |
<code>expressions</code> |
| 200 |
option to a value larger than 256. |
| 201 |
<pre> |
| 202 |
options(expressions=1000) |
| 203 |
</pre> |
| 204 |
|
| 205 |
The main cause of this is that |
| 206 |
S and R are programming languages not specialized for handling |
| 207 |
trees. |
| 208 |
(They are functional languages and have no facilities for |
| 209 |
pointers or references as in C or Java.) |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
<dt> |
| 214 |
<li> <font class="question">I get errors when using parameter |
| 215 |
entities in DTDs?</font> |
| 216 |
<dd> |
| 217 |
This was true in version 1.7.3 and 1.8.2 of |
| 218 |
libxml. Thanks to Daneil Veillard for fixing |
| 219 |
this quickly when I pointed it out. |
| 220 |
|
| 221 |
<p> |
| 222 |
|
| 223 |
Parameters are allowed, but the libxml parsing library is fussy |
| 224 |
about white-space, etc. |
| 225 |
The following is is ok |
| 226 |
<pre> |
| 227 |
<!ELEMENT legend (%PlotPrimitives;)* > |
| 228 |
</pre> |
| 229 |
but |
| 230 |
<pre> |
| 231 |
<!ELEMENT legend (%PlotPrimitives; )* > |
| 232 |
</pre> |
| 233 |
is not. The extra space preceeding the <code>)</code> |
| 234 |
causes an error in the parser something like |
| 235 |
<pre> |
| 236 |
1: XML Parsing Error: ../Histogram.dtd:80: xmlParseElementChildrenContentDecl : ',' '|' or ')' expected |
| 237 |
2: XML Parsing Error: ../Histogram.dtd:80: xmlParseElementChildrenContentDecl : ',' expected |
| 238 |
3: XML Parsing Error: ../Histogram.dtd:80: xmlParseElementDecl: expected '>' at the end |
| 239 |
4: XML Parsing Error: ../Histogram.dtd:80: Extra content at the end of the document |
| 240 |
</pre> |
| 241 |
|
| 242 |
This can be fixed by adding a call to SKIP_BLANKS at the end of |
| 243 |
the loop <code>while(CUR!= ')' { ... } </code> in the routine |
| 244 |
<code>xmlParseElementChildrenContentDecl()</code> in parser.c |
| 245 |
The problem lies in the transition between the different input |
| 246 |
buffers introduced by the entity expansion. |
| 247 |
|
| 248 |
|
| 249 |
<dt> |
| 250 |
<li> I am trying to use XPath and getNodeSet(). But I am not |
| 251 |
matching any nodes. |
| 252 |
|
| 253 |
<dd> |
| 254 |
If you are certain that the XPath expression should match what you |
| 255 |
want, then it is probably a matter of namespaces. |
| 256 |
If the document in which you are trying to find the nodes has a |
| 257 |
default namespace (at the top-level node or a sub-node |
| 258 |
involved in your match), then you have to explicitly identify |
| 259 |
the namespace. Unfortunately, XPath doesn't use the default |
| 260 |
namespace of the target document, but requires the namespace |
| 261 |
to be explicitly mentioned in the XPath expression. |
| 262 |
<p/> |
| 263 |
For example, suppose we have a document that looks like |
| 264 |
<pre> |
| 265 |
<![CDATA[ |
| 266 |
<doc xmlns="http://www.omegahat.net"> |
| 267 |
<topic><title>My Title</title></topic> |
| 268 |
</doc> |
| 269 |
]]> |
| 270 |
</pre> |
| 271 |
and we want to use an XPath expression to find the title node. |
| 272 |
We might think that <code>"/doc/topic/title"</code> |
| 273 |
would do the trick. But in fact, we need |
| 274 |
<pre> |
| 275 |
/ns:doc/ns:topci/ns:title |
| 276 |
</pre> |
| 277 |
And then we need to map ns to the URI "http://www.omegahat.net". |
| 278 |
We do this in a call to getNodeSet() as |
| 279 |
<pre> |
| 280 |
getNodeSet(doc, "/ns:doc/ns:topci/ns:title", c(ns = "http://www.omegahat.net")) |
| 281 |
</pre> |
| 282 |
|
| 283 |
<p/> |
| 284 |
As a simplification, getNodeSet() will create the map between |
| 285 |
the prefix and the URI of the default namespace of the XML |
| 286 |
document if you specify a single string with no name as the |
| 287 |
value |
| 288 |
of the <code>namespaces</code> argument, e.g. |
| 289 |
<pre> |
| 290 |
getNodeSet(doc, "/ns:doc/ns:topci/ns:title", "ns") |
| 291 |
</pre> |
| 292 |
|
| 293 |
<p/> |
| 294 |
|
| 295 |
There are some additional comments <a |
| 296 |
href="http://plasmasturm.org/log/259/">here</a>. |
| 297 |
|
| 298 |
<dt> |
| 299 |
<li> There is an table of data in HTML that I want to read into R |
| 300 |
as a data frame. How can I do it? |
| 301 |
<dd> |
| 302 |
Well, it is relatively easy, although the technology underlying |
| 303 |
it is quite powerful and somewhat complex in the general case. |
| 304 |
There is a <a href="htmlTables.xml">document describing the approach(es)</a> |
| 305 |
|
| 306 |
|
| 307 |
<dt> |
| 308 |
<li> I have a "large" XML file. Can I use DOM parsing or do I have |
| 309 |
to use SAX style parsing via the more complex xmlEventParse(). |
| 310 |
<dd> Well, I was given a 70 Mb XML file (which when compressed is |
| 311 |
6MB) and after uncompressing the file, I can read it into R |
| 312 |
via <code class="r">xmlTreeParse(, useInternalTrue = |
| 313 |
TRUE)</code> |
| 314 |
This file contained 2,895,409 nodes |
| 315 |
(<code class="r">length(getNodeSet(z, "//*"))</code>) |
| 316 |
This took 9.4 seconds on Intel MacBook Pro with a 2.33Ghz Dual |
| 317 |
processor and 3G of RAM, and on a machine with dual core 64bit AMD, |
| 318 |
it took 20 seconds. |
| 319 |
To find the nodes of interest took 8.9 seconds on the Mac, and |
| 320 |
(apparently) 1.1 seconds on the AMD. |
| 321 |
|
| 322 |
|
| 323 |
<dt> |
| 324 |
<li> I want to include one document inside another. How can I do this? |
| 325 |
|
| 326 |
<dd> Firstly, you want to look into XInclude. |
| 327 |
When processing the document in R, use <code>xinclude = TRUE</code>, which |
| 328 |
is the default, in calls to xmlTreeParse(). |
| 329 |
|
| 330 |
<dt> |
| 331 |
<li> I want to use XInclude to include part of the same |
| 332 |
document. I can't figure out how to do it. Any ideas? |
| 333 |
|
| 334 |
<dd> |
| 335 |
Yes. Use |
| 336 |
<pre> |
| 337 |
<xi:include xpointer="xpointer(//mynode)"/> |
| 338 |
</pre> |
| 339 |
adapting that to what you want. |
| 340 |
Note that the attribute is named xpointer. |
| 341 |
There is no href so the XInclude defaults to this document |
| 342 |
and the expression for the xpointer attribute |
| 343 |
uses the function xpointer. This is not element. |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
<dt> |
| 348 |
<li> When I parse HTML content, what does the &nbsp; entity turn into? |
| 349 |
<dd> |
| 350 |
This turns into the Unicode character U+00A0, or in R "\u00A0". |
| 351 |
</dl> |
| 352 |
|
| 353 |
<hr> |
| 354 |
<address><a href="http://www.stat.ucdavis.edu/~duncan">Duncan Temple Lang</a> |
| 355 |
<a href=mailto:[email protected]><[email protected]></a></address> |
| 356 |
|
| 357 |
Last modified: Fri Dec 14 04:29:02 PST 2012 |
| 358 |
|
| 359 |
</body> </html> |