【发布时间】:2021-07-27 15:37:16
【问题描述】:
我有代码:
transformXML <- function(xml, fn) {
x <- xml2::read_xml(xml)
fn(x)
tmp <- tempfile(fileext = ".xml")
xml2::write_xml(x, tmp, options = "format")
paste(unlist(readLines(tmp)), collapse='\n')
}
fname <- "inst/shiny/test.svg"
svg <- readChar(fname, file.info(fname)$size)
## move text that is in top of the plot tot the left
svg <- transformXML(svg, function(xml) {
text.nodes <- xml2::xml_find_all(xml, ".//text")
for (text in text.nodes) {
if (as.double(xml2::xml_attr(text, 'y')) < 60) {
xml2::xml_set_attr(text, 'x', '0')
}
}
})
message(svg)
当我有这样的 SVG 时:
<?xml version='1.0' encoding='UTF-8' ?>
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' viewBox='0 0 1319.34 500.00'>
但它在 SVG 中找不到任何东西。它总是返回 0 个节点,即使我使用 //svg XPath。
如果我删除 xmlns='http://www.w3.org/2000/svg' 它可以正常工作,如何在 XML 中使用默认命名空间并使用 R 处理文件?
【问题讨论】: