R/extract_dsd.R

Defines functions extract_dsd

Documented in extract_dsd

#' @title Extract values from XML 
#' @description Extracts the values from the XML Data Structure Definition (DSD) file
#' @param concept a character vector with concept ids  
#' @param dsd_xml an XML file with DSD content
#' @export 
#' @details It is a subfunction to use in the \code{\link{get_eurostat_dsd}} function.
#' @return a matrix with 3 columns if the concepts has code list in the DSD file
#' @examples 
#' \dontshow{
#' if ((parallel::detectCores()<2)|(Sys.info()[['sysname']]=='Windows')){
#'    options(restatapi_cores=1)
#' }else{
#'    options(restatapi_cores=2)
#' }    
#' }
#' dsd_url<-"http://ec.europa.eu/eurostat/SDMX/diss-web/rest/datastructure/ESTAT/DSD_nama_10_a10_e"
#' tryCatch({
#'   dsd_xml<-xml2::read_xml(dsd_url)}, 
#'   error=function(e){
#'   message("Unable to download the xml file.\n",e)}, 
#'   warning=function(w){
#'   message("Unable to download the xml file.\n",w)}) 
#' if (exists("dsd_xml")) {extract_dsd("GEO",dsd_xml)} 
#' 

extract_dsd<-function(concept=NULL,dsd_xml=NULL){
  if (is.null(dsd_xml)|is.null(concept)){
    message("The XML file or the concept is missing.")
    return(NULL)
  } else {
    xml_clc<-xml2::xml_attr(xml2::xml_find_all(dsd_xml,paste0('//str:Codelist[@id="CL_',concept,'"]/str:Code')),"id")
    if (length(xml_clc)>0){
      xml_cln<-xml2::xml_text(xml2::xml_find_all(dsd_xml,paste0('//str:Codelist[@id="CL_',concept,'"]/str:Code/com:Name')))
      cbind(concept,xml_clc,xml_cln)
    }  
  }
}  

Try the restatapi package in your browser

Any scripts or data that you put into this service are public.

restatapi documentation built on July 24, 2019, 1:05 a.m.