简体   繁体   English

R 中的 XML 中没有前缀的命名空间

[英]Namespace without prefix in XML in R

In the XML package in R, it is possible to create a new xmlTree object with a namespace, eg using:R的 XML package 中,可以使用命名空间创建新的xmlTree ZA8CFDE63391BD59EB66696C:

library(XML)
d = xmlTree("foo", namespaces = list(prefix = "url"))
d$doc()
# <?xml version="1.0"?>
# <foo xmlns:prefix="url"/>

How do I create a default namespace, without the prefix bar , such that it looks like the following?如何创建一个没有前缀bar的默认命名空间,使其如下所示?

# <?xml version="1.0"?>
# <foo xmlns="url"/>

The following does not produce what I expected.以下不会产生我所期望的。

library(XML)
d = xmlTree("foo", namespaces = list("url"))
d$doc()
# <?xml version="1.0"?>
# <url:foo xmlns:url="<dummy>"/>

There seems to be a difference between nameless lists and lists with an empty name in R. R 中的无名列表和具有空名称的列表之间似乎存在差异。

1 - A nameless list: 1 - 无名列表:

list("url")
# [[1]]
# [1] "url"
names(list("url"))
# NULL

2 - A named list: 2 - 命名列表:

list(prefix = "url")
# $prefix
# [1] "url"
names(list(prefix = "url"))
# [1] "prefix"

3 - An incorrectly initialised empty-name list: 3 - 一个错误初始化的空名列表:

list("" = "url")
# Error: attempt to use zero-length variable name

4 - An hacky way to initialise an empty-name list: 4 - 一种初始化空名称列表的 hacky 方法:

setNames(list(prefix = "url"), "")
# [[1]]
# [1] "url"
names(setNames(list(prefix = "url"), ""))
# [1] ""

It would seem 1. and 4. are identical, however, in the package XML they produce different results.看起来 1. 和 4. 是相同的,但是,在 package XML 中它们会产生不同的结果。 The first gives the incorrect XML as mentioned in the OP, whereas option 4. produces:第一个给出了 OP 中提到的不正确的 XML,而选项 4. 产生:

library(XML)
d = d = xmlTree("foo", namespaces = setNames(list(prefix = "url"), ""))
d$doc() 
# <?xml version="1.0"?>
# <foo xmlns="url"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM