1

I wrote a a query xmlstarlet sel -T -t -m badges/row -v "concat(@row, ',')" -n Badges.xml and the output is repeated as:

NaNNaNNaN...

for what is likely the amount of rows in the Badges.xml file. I am following this tut: https://www.joyofdata.de/blog/transforming-xml-document-into-csv-using-xmlstarlet/ to transform the xml formatted data to CSV. Anyone understand what is going on here?

enter image description here

Edit, here are some sample rows of the XML file:

<?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" />
  • You probably need to provide your input data too. It looks like it's converting something to a number that shouldn't be. – Michael Homer Mar 19 '18 at 19:26
  • No I did not understand what is going on, please proofread and edit question. By the way NaN is often printed if you try to print a number that is not a number (Not a Number). This can happen if you divide a number by 0, or depending on the language try to do arithmetic with something that is not a number. – ctrl-alt-delor Mar 19 '18 at 20:08
1

In XPATH @ points to an attribute name. To generate csv records based on attributes values of each <row> node use the following approach:

xmlstarlet sel -T -t -m 'badges/row' -v \
'concat(@Id, ",", @UserId, ",", @Name, ",", translate(@Date," ",""), 
",", @Class, ",", @TagBased)' -n Badges.xml

The output:

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
  • Really great explanation of how @ works in XPath. Did not understand it before this post. – Greg Iven Mar 22 '18 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.