【问题标题】:Write XML using pipe operator with xml2使用带有 xml2 的管道运算符编写 XML
【发布时间】:2018-08-08 18:47:58
【问题描述】:

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>

【问题讨论】:

    标签: r xml2


    【解决方案1】:

    我首先要说的是,我认为这通常是个坏主意。 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>
    

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

    另一个选择,如果你真的想在整个过程中使用管道是使用%T&gt;% 管道,而不是%&gt;% 管道(或者更确切地说,两者的混合)。两者的区别如下:

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

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

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

    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>
    

    注意最后的 %&gt;% 而不是 %T&gt;%。如果您将%&gt;% 换成%T&gt;%,则整个管道的值将只是“父”节点树:

    {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

    【讨论】:

    • 这很棒。你提到管道是错误的做法。在 R 中从头开始构建文档的正确方法是什么?只需在自己的行上运行每个 xml_add_child() 函数?
    • 是的。您只需要保留要附加孩子的对象,然后每次再次引用它。和第一个示例一样,将父节点保存为p,跳过所有管道,在示例代码中使用. 的任何地方都引用p。你可以用管道来做,我只是认为它最终会比它需要的更混乱,因为你实际上不需要将它们传递给你想要的输出。
    猜你喜欢
    • 1970-01-01
    • 2016-11-21
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2020-01-24
    • 2015-03-12
    • 2023-04-01
    相关资源
    最近更新 更多