Вот один из способов получить то, что вам нужно. Вы можете изменить этот ответ, если в других частях документа XML также есть текст, соответствующий вашему целевому шаблону.
library(xml2)
library(stringr)
# read in the XML doc
xmldoc <- as_list(read_xml("https://www.ris.bka.gv.at/Dokumente/Vfgh/JFT_20191212_19G00164_00/JFT_20191212_19G00164_00.xml"))
# extract all the "absatz" elements as these seem to hold the relevant text
absatz <- lapply(1:length(xmldoc$risdok$nutzdaten$abschnitt),
function(x){
xmldoc$risdok$nutzdaten$abschnitt[x]$absatz[[1]]
})
# extract all the "cts" elements
cts <- lapply(1:length(xmldoc$risdok$nutzdaten$abschnitt),
function(x){
attributes(xmldoc$risdok$nutzdaten$abschnitt[x]$absatz)$ct
})
# flatten the cts to get a character vector
cts <- unlist(cts)
# find the indices of the cts which equal "begruendung"
begs_idx <- grep(pattern = "begruendung", x = cts)
# subset the absatz list using the begruendung indices
absatz_bg <- absatz[begs_idx]
# remove null items in the absatz_bg list
names(absatz_bg) <- seq_along(absatz_bg)
absatz_bg[sapply(absatz_bg, is.null)] <- NULL
# flatten the list to get a character vector
flat_absatz_bg <- unlist(absatz_bg)
# find instances of the pattern GXXXX/YYYY,
output <- flat_absatz_bg %>% str_extract(pattern = "G\\d+/\\d+")
# remove the NAs
gs <- output[!is.na(output)]
# look at the result
gs
#> [1] "G164/2019" "G415/2017" "G136/2017" "G136/2017" "G136/2017" "G136/2017"
#> [7] "G156/2018" "G136/207" "G136/2017" "G308/2018" "G179/2015" "G156/2018"
#> [13] "G415/2017"
Создан в 2020-01-23 пакетом представ. (v0.3.0)