2016-10-31 16 views
2

私はXML入力ファイルを持っています。このファイルには、いくつかのトランザクションに関するデータがあります。XML - R(xml2)の選択ノードを解析する

<?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 

これは私が次の操作を行うために必要なものです:

  • BSまで移動します。out_txns内のstmtタグ
  • を検索しますbs:Ntry、bs:Amtタグを抽出して値を抽出します。

私はいくつかのことを試しましたが(xml_find_parents)これを行う正しい方法を見つけ出すことはできません

答えて

2

あなたのアプローチは適切なものでしたが、あまりにも早くやろうとしていました。

最初のステップは、すべてのノードを見つけてノードのベクトルとして保存することです(in_out変数)。次に、ノードの "in_out"ベクトルから "Outward"要求されたサブセットを解析してフィルタリングし、out_txnsを作成します。ノードのこの縮小リストから、要求された「量」情報を抽出した。

library(xml2) 
d<-read_xml("~/CEED/sample1.dat") # read the file 

#find all of the stmt nodes 
in_out <- xml_find_all(d, ".//bs:stmt") # Transaction Type 

#filter the nodes and Select only Outward 
out_txns <- in_out[xml_text(xml_find_first(in_out, ".//bs:Prtry")) == "Outward"] 
#Extract the amounts from the remianing nodes 
amount<-xml_text(xml_find_first(out_txns, ".//bs:Amt")) 
関連する問題