4

xml2包允许用户创建 XML 文档。我正在尝试使用管道运算符创建一个文档,%>%以添加子节点和兄弟节点的各种组合。我无法弄清楚如何在原始孩子的兄弟姐妹之后的子节点中创建子节点(参见下面的示例)。

是否可以“提升”一个级别以创建更多节点,或者必须在链接命令之外创建它们?

我想要的是

library(xml2)
x1 <- read_xml("<parent><child>1</child><child><grandchild>2</grandchild></child><child>3</child><child>4</child></parent>")
message(x1)
#> <?xml version="1.0" encoding="UTF-8"?>
#> <parent>
#>  <child>1</child>
#>  <child>
#>    <grandchild>2</grandchild>
#>  </child>
#>  <child>3</child>
#>  <child>4</child>
#> </parent>

我正在创造的东西是错误的

library(magrittr)
library(xml2)
x2 <- xml_new_document()
x2 %>% 
  xml_add_child("parent") %>%
  xml_add_child("child", 1) %>%
  xml_add_sibling("child", 4, .where="after") %>%
  xml_add_sibling("child", 3) %>%
  xml_add_sibling("child", .where="before") %>%
  xml_add_child("grandchild", 2)
message(x2)
#> <?xml version="1.0" encoding="UTF-8"?>
#> <parent>
#>  <child>1</child>
#>  <child>4</child>
#>  <child>
#>    <grandchild>2</grandchild>
#>  </child>
#>  <child>3</child>
#> </parent>

使用 XML 包的解决方案

如果使用XML包,这实际上是相当简单的。

library(XML)
x2 <- newXMLNode("parent")
invisible(newXMLNode("child", 1, parent=x2))
invisible(newXMLNode("child", newXMLNode("grandchild", 2), parent=x2))
invisible(newXMLNode("child", 3, parent=x2))
invisible(newXMLNode("child", 4, parent=x2))
x2
#> <?xml version="1.0" encoding="UTF-8"?>
#> <parent>
#>  <child>1</child>
#>  <child>
#>    <grandchild>2</grandchild>
#>  </child>
#>  <child>3</child>
#>  <child>4</child>
#> </parent>
4

1 回答 1

8

我首先要说的是,我认为这通常是一个坏主意。xml2 使用指针工作,这意味着它具有引用语义(“按引用传递”),这不是 R 中的典型行为。xml2 中的函数通过在 XML 树上产生副作用而不是像函数式编程那样通过返回值来工作(“按值传递”)。

这意味着管道基本上是错误的原则。您只需要一系列以正确顺序修改对象的步骤。

也就是说,你可以这样做:

library("magrittr")
library("xml2")
x2 <- xml_new_document()
x2 %>% 
  xml_add_child(., "parent") %>%
{
  xml_add_child(., "child", 1, .where = "after")
  (xml_add_child(., "child") %>% xml_add_child("grandchild", 2))
  xml_add_child(., "child", 3, .where = "after")
  xml_add_child(., "child", 4, .where = "after")
}
message(x2)
## <?xml version="1.0" encoding="UTF-8"?>
## <parent>
##   <child>1</child>
##   <child>
##     <grandchild>2</grandchild>
##   </child>
##   <child>3</child>
##   <child>4</child>
## </parent>

.告诉%>%在后续调用中将“父”节点放置在何处xml_add_child()。中间的()带括号的表达式利用了这样一个事实,即您希望通过管道进入“子”节点,然后将该子节点通过管道传输到孙节点。

如果您真的想始终使用管道,另一种选择是使用%T>%管道,而不是%>%管道(或者更确切地说,两者的混合)。两者的区别如下:

> 1:3 %>% mean() %>% str()
 num 2
> 1:3 %T>% mean() %>% str()
 int [1:3] 1 2 3

管道将左侧表达式的%T>%值推入右侧表达式,但进一步将其推入后续表达式。这意味着您可以在管道中间调用函数以获取其副作用,并继续在管道中向前传递较早的对象引用。

当您说“提升一个级别”时,这就是您要尝试做的事情 - 即,恢复到管道中的先前值并从那里开始工作。所以你只需要%T>%管道直到你到达你想要%>%管道的点(例如,创建孙子),然后返回%T>%管道继续向前传递父对象引用。一个例子:

x3 <- xml_new_document()
x3 %>% 
  xml_add_child("parent") %T>%
    xml_add_child("child", 1, .where = "after") %T>%
    {xml_add_child(., "child") %>% xml_add_child("grandchild", 2)} %T>%
    xml_add_child("child", 3, .where = "after") %>%
    xml_add_child("child", 4, .where = "after")
message(x3)
## <?xml version="1.0" encoding="UTF-8"?>
## <parent>
##   <child>1</child>
##   <child>
##     <grandchild>2</grandchild>
##   </child>
##   <child>3</child>
##   <child>4</child>
## </parent>

注意 final%>%而不是%T>%. 如果您交换整个管道的值,则仅是“父”节点树%>%%T>%

{xml_document}
<parent>
[1] <child>1</child>
[2] <child>\n  <grandchild>2</grandchild>\n</child>
[3] <child>3</child>
[4] <child>4</child>

(这 - 再次 - 最终并不重要,因为我们实际上是在x3使用副作用进行构建,但它会将父节点树打印到控制台,这可能会令人困惑。)

同样,鉴于尴尬,我建议根本不要使用管道,但这取决于你。更好的方法是保留要附加孩子的每个对象,然后每次再次引用它。与第一个示例一样,将父节点另存为p,跳过所有管道,仅引用示例代码中使用的所有位置p.

于 2018-08-08T19:41:24.173 回答