Skip to content
Prev 366902 / 398502 Next

Failure to understand namespaces in XML::getNodeSet

Hadley,

It?s sometimes amazing the mistakes I can make. No, it did not do what I wanted, which was
read_xml(str_c(with_ns_xml, collapse = ?")

Reproducible example follows:
library(stringr)
library(xml2)
## Given the correct argument value for collapse, the next two lines work
no_ns <- read_xml(str_c(no_ns_xml, collapse = ""))
with_ns <- read_xml(str_c(with_ns_xml, collapse = ""))
## The next line finds the node in the XML without a namespace
xml_find_all(no_ns, "//WorkSet//Description")
## With a namespace designated in the XML
## Neither of the next two work, though I thought the second should
xml_find_all(with_ns, "//WorkSet//Description")
xml_find_all(with_ns, "/WorkSet//Description", ns = xml_ns(with_ns))
## Using xml_ns_strip() works as predicted
xml_find_all(xml_ns_strip(with_ns), "//WorkSet//Description")
## I was surprised to find the incorrect namespace value did not matter
xml_find_all(no_ns, "//WorkSet//Description", ns = xml_ns(with_ns))
## This also seems to ignore the namespace argument value
xml_find_all(xml_ns_strip(with_ns), "/WorkSet//Description", ns = xml_ns(with_ns))


Full output follows:
{xml_nodeset (1)}
[1] <Description>MFIA 9-Plex (CharlesRiver)</Description>
{xml_nodeset (0)}
{xml_nodeset (0)}
{xml_nodeset (1)}
[1] <Description>MFIA 9-Plex (CharlesRiver)</Description>
{xml_nodeset (1)}
[1] <Description>MFIA 9-Plex (CharlesRiver)</Description>
{xml_nodeset (1)}
[1] <Description>MFIA 9-Plex (CharlesRiver)</Description>
R. Mark Sharp, Ph.D.
msharp at TxBiomed.org
CONFIDENTIALITY NOTICE: This e-mail and any files and/or attachments transmitted, may contain privileged and confidential information and is intended solely for the exclusive use of the individual or entity to whom it is addressed. If you are not the intended recipient, you are hereby notified that any review, dissemination, distribution or copying of this e-mail and/or attachments is strictly prohibited. If you have received this e-mail in error, please immediately notify the sender stating that this transmission was misdirected; return the e-mail to sender; destroy all paper copies and delete all electronic copies from your system without disclosing its contents.
Message-ID: <21BF4313-A61A-4D89-9DE8-BEF08B34B43A@txbiomed.org>
In-Reply-To: <CABdHhvFk5-pV0cdEF7mMmZDijNfFw7i9gDAOeY_N4NV0x1Nz2Q@mail.gmail.com>