前提・実現したいこと
PowerShell(Win10x64上)にて.xml
ファイルを入力、加工し.tsv
(タブ区切り)出力しています。
入力XML(test.xml)
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<results version="2"> 3 <errors> 4 <error id="unreadVariable"> 5 <location file="func.cpp" line="10"/> 6 </error> 7 <error id="redundantAssignment"> 8 <location file="main.cpp" line="36"/> 9 <location file="main.cpp" line="34"/> 10 </error> 11 </errors> 12</results>
XMLデータは、error
要素内にlocation
要素が1個以上、複数存在しえます。
このXMLからlocation.file
, location.line
, error.id
列で構成されたTSVを出力したいです。
試したこと
以下のようにいくつかSelect
を試してみましたが、locationの内容が欠けたり、先頭の1文字しか出力できません。
PowerShell
1$xmlRes = [xml](Get-Content test.xml) 2 3# 方法1 : locationをそのまま出力 4# 結果 : 連結して出力される 5$res1 = $xmlRes.results.errors.error | Select @{Name="file"; Expression={$_.location.file}},@{Name="line"; Expression={$_.location.line}},id 6 7# 方法2 : location[0]を出力 8# 結果 : location1個だと、location列データが出力されない。 9$res2 = $xmlRes.results.errors.error | Select @{Name="file"; Expression={$_.location[0].file}},@{Name="line"; Expression={$_.location[0].line}},id 10 11# 方法3 : location.file[0], line[0]を出力 12# 結果 : location1個だと、先頭の1文字?しか出力されない。 13$res3 = $xmlRes.results.errors.error | Select @{Name="file"; Expression={$_.location.file[0]}},@{Name="line"; Expression={$_.location.line[0]}},id 14 15echo "res1---" 16echo ($res1 | Select file,line,id) 17$res1 | Export-Csv -Path res1.tsv -NoTypeInformation -Delimiter "`t" 18 19echo "res2---" 20echo ($res2 | Select file,line,id) 21$res2 | Export-Csv -Path res2.tsv -NoTypeInformation -Delimiter "`t" 22 23echo "res3---" 24echo ($res3 | Select file,line,id) 25$res3 | Export-Csv -Path res3.tsv -NoTypeInformation -Delimiter "`t"
実行結果
res1--- file line id ---- ---- -- func.cpp 10 unreadVariable {main.cpp, main.cpp} {36, 34} redundantAssignment res2--- unreadVariable main.cpp 36 redundantAssignment res3--- f 1 unreadVariable main.cpp 36 redundantAssignment
欲しい結果は以下のいずれかです。
# 結果1 : 先頭のlocationのみを出力 file line id ---- ---- -- func.cpp 10 unreadVariable main.cpp 36 redundantAssignment # 結果2 : locationまで展開して出力 file line id ---- ---- -- func.cpp 10 unreadVariable main.cpp 36 redundantAssignment main.cpp 34 redundantAssignment
補足情報
- 元データは、CUI版
cppcheck
で出力したものです。 - 方法1で作成できる
.tsv
は行列データずれもなくexcel
で開けるので、できればといった感じの質問です。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/23 13:28