我有一个 XML 输入文件。该文件包含有关某些交易的数据。XML 文件如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Message xmlns:bs="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02"
xmlns="urn:bcsis"
xmlns:head="urn:iso:std:iso:20022:tech:xsd:head.001.001.01">
<bs:stmt>
<bs:Bal>
<bs:Tp>
<bs:CdOrPrtry>
<bs:Prtry>Outward</bs:Prtry>
</bs:CdOrPrtry>
</bs:Tp>
<bs:Amt Ccy="USD">300.00</bs:Amt>
<bs:CdtDbtInd>DBIT</bs:CdtDbtInd>
<bs:Dt>
<bs:Dt>2016-10-04</bs:Dt>
</bs:Dt>
</bs:Bal>
<bs:Ntry>
<bs:Amt Ccy="USD">300.00</bs:Amt>
</bs:Ntry>
</bs:stmt>
<bs:stmt>
<bs:Bal>
<bs:Tp>
<bs:CdOrPrtry>
<bs:Prtry>Inward</bs:Prtry>
</bs:CdOrPrtry>
</bs:Tp>
<bs:Amt Ccy="USD">250.00</bs:Amt>
<bs:CdtDbtInd>DBIT</bs:CdtDbtInd>
<bs:Dt>
<bs:Dt>2016-10-04</bs:Dt>
</bs:Dt>
</bs:Bal>
<bs:Ntry>
<bs:Amt Ccy="USD">250.00</bs:Amt>
</bs:Ntry>
</bs:stmt>
</Message>
我需要提取交易类型(bs:Prtry)为“Outward”的交易金额。
这是我到目前为止所做的:
library(xml2)
library(XML)
library(dplyr)
d <- read_xml("~/CEED/sample1.dat") # read the file
in_out <- xml_find_all(d, ".//bs:stmt/bs:Bal/bs:Tp/bs:CdOrPrtry/bs:Prtry") # Transaction Type
out_txns <- in_out[which(in_out %>% xml_text() == "Outward")] # Select only Outward
这是我接下来需要做的:
- 向上导航到 out_txns 中的 bs:stmt 标记
- 找到 bs:Ntry, bs:Amt 标签并提取值
我已经尝试了一些东西(xml_find_parents)但无法找出正确的方法