简体   繁体   English

来自 XML 的嵌套数据作为 tibble 或 Dataframe in R

[英]nested data from XML as tibble or Dataframe in R

I'm stuck at work with a code problem in R that I can't solve.我在 R 遇到了一个我无法解决的代码问题。 I have the following XML Data:我有以下 XML 数据:

<?xml version="1.0" encoding="utf-8"?>
<Votacion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://opendata.camara.cl/camaradiputados/v1">
  <Id>15446</Id>
  <Descripcion>Proyecto de Acuerdo N° 574</Descripcion>
  <Fecha>2012-06-13T14:47:29</Fecha>
  <TotalSi>12</TotalSi>
  <TotalNo>2</TotalNo>
  <TotalAbstencion>2</TotalAbstencion>
  <TotalDispensado>0</TotalDispensado>
  <Quorum Valor="1">Quórum Simple</Quorum>
  <Resultado Valor="2">Unánime</Resultado>
  <Tipo Valor="3">Proyecto de Acuerdo</Tipo>
  <Votos>
    <Voto>
      <Diputado>
        <Id>810</Id>
        <Nombre>Gabriel</Nombre>
        <ApellidoPaterno>Ascencio</ApellidoPaterno>
        <ApellidoMaterno>Mansilla</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="1">Afirmativo</OpcionVoto>
    </Voto>
    <Voto>
      <Diputado>
        <Id>855</Id>
        <Nombre>Carlos Abel</Nombre>
        <ApellidoPaterno>Jarpa</ApellidoPaterno>
        <ApellidoMaterno>Wevar</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="1">Afirmativo</OpcionVoto>
    </Voto>
    <Voto>
      <Diputado>
        <Id>862</Id>
        <Nombre>Pablo</Nombre>
        <ApellidoPaterno>Lorenzini</ApellidoPaterno>
        <ApellidoMaterno>Basso</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="0">En Contra</OpcionVoto>
    </Voto>
    <Voto>
      <Diputado>
        <Id>898</Id>
        <Nombre>Gabriel</Nombre>
        <ApellidoPaterno>Silber</ApellidoPaterno>
        <ApellidoMaterno>Romo</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="1">Afirmativo</OpcionVoto>
    </Voto>
  </Votos>
</Votacion>

I got the data from this api https://opendata.camara.cl/camaradiputados/WServices/WSLegislativo.asmx/retornarVotacionDetalle?prmVotacionId=15446我从这个 api https://opendata.camara.cl/camaradiputados/WServices/WSLegislativo.asmx/retornarVotacionDetalle?prmVotacionId=15446得到了数据

I want to process the data to obtain a tibble like this我想处理数据以获得这样的小标题

Id ID Nombre名称 ApellidoPaterno阿佩利多帕特诺 ApellidoMaterno阿佩利多玛特诺 OpcionVoto投票权
810 810 Gabriel加布里埃尔 Acencio阿森西奥 Mansilla曼西利亚 Afirmativo肯定语
855 855 Abel亚伯 Jarpa贾帕 Webar网吧 Afirmativo肯定语
862 862 Pablo巴勃罗 Lorenzini洛伦齐尼 Basso巴索 En Contra魂斗罗

Any kind of help will be amazing, please !!!任何形式的帮助都会很棒,请 !!!

Something inspired by https://tidyr.tidyverse.org/articles/rectangle.html :https://tidyr.tidyverse.org/articles/rectangle.html启发的东西:

library(xml2)
library(dplyr)
library(tidyr)

read_xml(xml_str) %>% 
  xml_ns_strip() %>% 
  xml_find_all("//Voto") %>% 
  as_list() %>% 
  tibble(lst = .) %>% 

  # A tibble: 4 × 1

  unnest_wider(lst) %>% 

  # A tibble: 4 × 2
  # Diputado           OpcionVoto
  # 1 <named list [4]> <list [1]>

  unnest_wider("Diputado") %>% 

  # A tibble: 4 × 5
  #   Id         Nombre     ApellidoPaterno ApellidoMaterno OpcionVoto
  #   <list>     <list>     <list>          <list>          <list>    
  # 1 <list [1]> <list [1]> <list [1]>      <list [1]>      <list [1]>  

  unnest_longer(everything())
#> # A tibble: 4 × 5
#>   Id    Nombre      ApellidoPaterno ApellidoMaterno OpcionVoto
#>   <chr> <chr>       <chr>           <chr>           <chr>     
#> 1 810   Gabriel     Ascencio        Mansilla        Afirmativo
#> 2 855   Carlos Abel Jarpa           Wevar           Afirmativo
#> 3 862   Pablo       Lorenzini       Basso           En Contra 
#> 4 898   Gabriel     Silber          Romo            Afirmativo

Input:输入:

xml_str <- 
'<?xml version="1.0" encoding="utf-8"?>
<Votacion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://opendata.camara.cl/camaradiputados/v1">
  <Id>15446</Id>
  <Descripcion>Proyecto de Acuerdo N° 574</Descripcion>
  <Fecha>2012-06-13T14:47:29</Fecha>
  <TotalSi>12</TotalSi>
  <TotalNo>2</TotalNo>
  <TotalAbstencion>2</TotalAbstencion>
  <TotalDispensado>0</TotalDispensado>
  <Quorum Valor="1">Quórum Simple</Quorum>
  <Resultado Valor="2">Unánime</Resultado>
  <Tipo Valor="3">Proyecto de Acuerdo</Tipo>
  <Votos>
    <Voto>
      <Diputado>
        <Id>810</Id>
        <Nombre>Gabriel</Nombre>
        <ApellidoPaterno>Ascencio</ApellidoPaterno>
        <ApellidoMaterno>Mansilla</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="1">Afirmativo</OpcionVoto>
    </Voto>
    <Voto>
      <Diputado>
        <Id>855</Id>
        <Nombre>Carlos Abel</Nombre>
        <ApellidoPaterno>Jarpa</ApellidoPaterno>
        <ApellidoMaterno>Wevar</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="1">Afirmativo</OpcionVoto>
    </Voto>
    <Voto>
      <Diputado>
        <Id>862</Id>
        <Nombre>Pablo</Nombre>
        <ApellidoPaterno>Lorenzini</ApellidoPaterno>
        <ApellidoMaterno>Basso</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="0">En Contra</OpcionVoto>
    </Voto>
    <Voto>
      <Diputado>
        <Id>898</Id>
        <Nombre>Gabriel</Nombre>
        <ApellidoPaterno>Silber</ApellidoPaterno>
        <ApellidoMaterno>Romo</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="1">Afirmativo</OpcionVoto>
    </Voto>
  </Votos>
</Votacion>'

Created on 2022-11-25 with reprex v2.0.2创建于 2022-11-25,使用reprex v2.0.2

library(xml2)
library(purrr)

xml <- read_xml('<?xml version="1.0" encoding="utf-8"?>
<Votacion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://opendata.camara.cl/camaradiputados/v1">
  <Id>15446</Id>
  <Descripcion>Proyecto de Acuerdo N° 574</Descripcion>
  <Fecha>2012-06-13T14:47:29</Fecha>
  <TotalSi>12</TotalSi>
  <TotalNo>2</TotalNo>
  <TotalAbstencion>2</TotalAbstencion>
  <TotalDispensado>0</TotalDispensado>
  <Quorum Valor="1">Quórum Simple</Quorum>
  <Resultado Valor="2">Unánime</Resultado>
  <Tipo Valor="3">Proyecto de Acuerdo</Tipo>
  <Votos>
    <Voto>
      <Diputado>
        <Id>810</Id>
        <Nombre>Gabriel</Nombre>
        <ApellidoPaterno>Ascencio</ApellidoPaterno>
        <ApellidoMaterno>Mansilla</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="1">Afirmativo</OpcionVoto>
    </Voto>
    <Voto>
      <Diputado>
        <Id>855</Id>
        <Nombre>Carlos Abel</Nombre>
        <ApellidoPaterno>Jarpa</ApellidoPaterno>
        <ApellidoMaterno>Wevar</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="1">Afirmativo</OpcionVoto>
    </Voto>
    <Voto>
      <Diputado>
        <Id>862</Id>
        <Nombre>Pablo</Nombre>
        <ApellidoPaterno>Lorenzini</ApellidoPaterno>
        <ApellidoMaterno>Basso</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="0">En Contra</OpcionVoto>
    </Voto>
    <Voto>
      <Diputado>
        <Id>898</Id>
        <Nombre>Gabriel</Nombre>
        <ApellidoPaterno>Silber</ApellidoPaterno>
        <ApellidoMaterno>Romo</ApellidoMaterno>
      </Diputado>
      <OpcionVoto Valor="1">Afirmativo</OpcionVoto>
    </Voto>
  </Votos>
</Votacion>')

xml_ns(xml)
# d1  <-> http://opendata.camara.cl/camaradiputados/v1
# xsd <-> http://www.w3.org/2001/XMLSchema
# xsi <-> http://www.w3.org/2001/XMLSchema-instance


xml %>% xml_find_all(".//d1:Voto") %>% 
  map_dfr(~ set_names(
    c(.x %>% xml_child("d1:Diputado") %>% xml_children() %>% map(xml_text),
      .x %>% xml_child("d1:OpcionVoto") %>% xml_text()),
    c(.x %>% xml_child("d1:Diputado") %>% xml_children() %>% map(xml_name), "OpcionVoto")))

# A tibble: 4 × 5
#  Id    Nombre      ApellidoPaterno ApellidoMaterno OpcionVoto
#  <chr> <chr>       <chr>           <chr>           <chr>     
#1 810   Gabriel     Ascencio        Mansilla        Afirmativo
#2 855   Carlos Abel Jarpa           Wevar           Afirmativo
#3 862   Pablo       Lorenzini       Basso           En Contra 
#4 898   Gabriel     Silber          Romo            Afirmativo

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

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