在第一个节点内的任何级别循环 select 遍历节点和 select 个子节点
Loop selected over nodes and select subnodes at any level within first node
我正在使用 xml2 包。我想:
- Select 个具有特定标签的节点(在本例中为所有
envelope
)
- 循环遍历这些节点和内部的 select 个节点,而不管嵌套(在这种情况下,在
card-entry
个标签内找到的所有 value
个标签)
- 用 space
连接文本
有三个信封,所以我希望能够 return 一个包含三个向量的向量,其中包含 card-entry
标签内的 value
标签内的所有文本。我无法做到这一点。我怎样才能做到这一点?为什么我的以下方法不起作用?
MWE
library(xml2)
myxml <- read_xml('
<inside>
<box>
<card-entry>
<card-id type="integer">605383</card-id>
<value>get well</value>
</card-entry>
</box>
<envelope>
<card-entry>
<card-id type="integer">605380</card-id>
<value>coke</value>
</card-entry>
<card-entry>
<card-id type="integer">610954</card-id>
<value>pizza</value>
</card-entry>
<card-entry>
<card-id type="integer">605381</card-id>
<value>surprise</value>
</card-entry>
<card-entry>
<card-id type="integer">610958</card-id>
<value>joke</value>
<random>true</random>
</card-entry>
</envelope>
<envelope>
<card-entry>
<card-id type="integer">605381</card-id>
<value>charlie horse</value>
</card-entry>
<card-entry>
<card-id type="integer">605380</card-id>
<value>rug bug</value>
</card-entry>
<subenvelope>
<value>dont get me</value>
</subenvelope>
<card-entry>
<card-id type="integer">610954</card-id>
<value>mario cart</value>
</card-entry>
</envelope>
<envelope>
<card-entry>
<card-id type="integer">605377</card-id>
<value>trogdor</value>
</card-entry>
<subenvelope>
<card-entry>
<card-id type="integer"></card-id>
<value>jorb</value>
</card-entry>
</subenvelope>
<card-entry>
<card-id type="integer">605333</card-id>
<value></value>
</card-entry>
</envelope>
</inside>
'
)
期望的输出
## [1] coke pizza surprise joke
## [2] charlie horse rug bug mario cart
## [3] trogdor jorb
我试过的
children <- lapply(xml2::xml_find_all(myxml, '//envelope'), xml2::xml_children)
rm_na <- function(x) x[!is.na(x)]
lapply(children, function(x){
paste(rm_na(unlist(xml2::xml_text(xml2::xml_child(x, '//card-entry//value')))), collapse = ' ')
})
## [[1]]
## [1] ""
##
## [[2]]
## [1] ""
##
## [[3]]
## [1] ""
##
## There were 11 warnings (use warnings() to see them)
lapply(children, function(x){
paste(rm_na(unlist(xml2::xml_text(xml2::xml_find_all(x, '//card-entry//value')))), collapse = ' ')
})
## [[1]]
## [1] "get well coke pizza surprise joke charlie horse rug bug mario cart trogdor jorb "
##
## [[2]]
## [1] "get well coke pizza surprise joke charlie horse rug bug mario cart trogdor jorb "
##
## [[3]]
## [1] "get well coke pizza surprise joke charlie horse rug bug mario cart trogdor jorb "
在你的内部选择器上,你想让它相对于当前节点,所以你想要.//
而不是//
,后者会再次搜索整个树
sapply(xml_find_all(myxml,"//envelope"), function(x)
paste(xml_text(xml_find_all(x, ".//card-entry/value")), collapse=" ")
)
# [1] "coke pizza surprise joke"
# [2] "charlie horse rug bug mario cart"
# [3] "trogdor jorb "
我正在使用 xml2 包。我想:
- Select 个具有特定标签的节点(在本例中为所有
envelope
) - 循环遍历这些节点和内部的 select 个节点,而不管嵌套(在这种情况下,在
card-entry
个标签内找到的所有value
个标签) - 用 space 连接文本
有三个信封,所以我希望能够 return 一个包含三个向量的向量,其中包含 card-entry
标签内的 value
标签内的所有文本。我无法做到这一点。我怎样才能做到这一点?为什么我的以下方法不起作用?
MWE
library(xml2)
myxml <- read_xml('
<inside>
<box>
<card-entry>
<card-id type="integer">605383</card-id>
<value>get well</value>
</card-entry>
</box>
<envelope>
<card-entry>
<card-id type="integer">605380</card-id>
<value>coke</value>
</card-entry>
<card-entry>
<card-id type="integer">610954</card-id>
<value>pizza</value>
</card-entry>
<card-entry>
<card-id type="integer">605381</card-id>
<value>surprise</value>
</card-entry>
<card-entry>
<card-id type="integer">610958</card-id>
<value>joke</value>
<random>true</random>
</card-entry>
</envelope>
<envelope>
<card-entry>
<card-id type="integer">605381</card-id>
<value>charlie horse</value>
</card-entry>
<card-entry>
<card-id type="integer">605380</card-id>
<value>rug bug</value>
</card-entry>
<subenvelope>
<value>dont get me</value>
</subenvelope>
<card-entry>
<card-id type="integer">610954</card-id>
<value>mario cart</value>
</card-entry>
</envelope>
<envelope>
<card-entry>
<card-id type="integer">605377</card-id>
<value>trogdor</value>
</card-entry>
<subenvelope>
<card-entry>
<card-id type="integer"></card-id>
<value>jorb</value>
</card-entry>
</subenvelope>
<card-entry>
<card-id type="integer">605333</card-id>
<value></value>
</card-entry>
</envelope>
</inside>
'
)
期望的输出
## [1] coke pizza surprise joke
## [2] charlie horse rug bug mario cart
## [3] trogdor jorb
我试过的
children <- lapply(xml2::xml_find_all(myxml, '//envelope'), xml2::xml_children)
rm_na <- function(x) x[!is.na(x)]
lapply(children, function(x){
paste(rm_na(unlist(xml2::xml_text(xml2::xml_child(x, '//card-entry//value')))), collapse = ' ')
})
## [[1]]
## [1] ""
##
## [[2]]
## [1] ""
##
## [[3]]
## [1] ""
##
## There were 11 warnings (use warnings() to see them)
lapply(children, function(x){
paste(rm_na(unlist(xml2::xml_text(xml2::xml_find_all(x, '//card-entry//value')))), collapse = ' ')
})
## [[1]]
## [1] "get well coke pizza surprise joke charlie horse rug bug mario cart trogdor jorb "
##
## [[2]]
## [1] "get well coke pizza surprise joke charlie horse rug bug mario cart trogdor jorb "
##
## [[3]]
## [1] "get well coke pizza surprise joke charlie horse rug bug mario cart trogdor jorb "
在你的内部选择器上,你想让它相对于当前节点,所以你想要.//
而不是//
,后者会再次搜索整个树
sapply(xml_find_all(myxml,"//envelope"), function(x)
paste(xml_text(xml_find_all(x, ".//card-entry/value")), collapse=" ")
)
# [1] "coke pizza surprise joke"
# [2] "charlie horse rug bug mario cart"
# [3] "trogdor jorb "