Csv
XmlStarlet xml 到 csv 輸出為 ‘NaNNaNNaN’
我寫了一個查詢
xmlstarlet sel -T -t -m badges/row -v "concat(@row, ',')" -n Badges.xml
,輸出重複為:NaNNaNNaN...
Badges.xml 文件中的行數可能是多少。我正在關注這個 tut:https ://www.joyofdata.de/blog/transforming-xml-document-into-csv-using-xmlstarlet/將 xml 格式的數據轉換為 CSV。任何人都明白這裡發生了什麼?
編輯,這裡是 XML 文件的一些範例行:
<?xml version="1.0" encoding="utf-8"?> <badges> <row Id="1" UserId="1" Name="Autobiographer" Date="2016-08-02T15:56:07.133" Class="3" TagBased="False" /> <row Id="2" UserId="8" Name="Autobiographer" Date="2016-08-02T15:56:07.133" Class="3" TagBased="False" /> <row Id="3" UserId="9" Name="Autobiographer" Date="2016-08-02T15:56:07.133" Class="3" TagBased="False" /> <row Id="4" UserId="19" Name="Autobiographer" Date="2016-08- 02T15:56:07.133" Class="3" TagBased="False" /> <row Id="5" UserId="27" Name="Autobiographer" Date="2016-08- 02T15:56:07.133" Class="3" TagBased="False" />
在 XPATH
@
中指向一個屬性名。要根據每個節點的屬性值生成 csv 記錄,<row>
請使用以下方法:xmlstarlet sel -T -t -m 'badges/row' -v \ 'concat(@Id, ",", @UserId, ",", @Name, ",", translate(@Date," ",""), ",", @Class, ",", @TagBased)' -n Badges.xml
輸出:
1,1,Autobiographer,2016-08-02T15:56:07.133,3,False 2,8,Autobiographer,2016-08-02T15:56:07.133,3,False 3,9,Autobiographer,2016-08-02T15:56:07.133,3,False 4,19,Autobiographer,2016-08-02T15:56:07.133,3,False 5,27,Autobiographer,2016-08-02T15:56:07.133,3,False