Search code examples
formattingxmlstarlet

xmlstarlet format with two main tags


I am trying to format this file using xmlstarlet

The file:

<file_info>
<name><OUTFILE_0/></name>
<generated_locally/>
<upload_when_present/><max_nbytes>99999999999999999999999999999999999</max_nbytes><url><UPLOAD_URL/></url>
</file_info>
<result>
<file_ref><file_name><OUTFILE_0/></file_name>
<open_name>output</open_name>
<copy_file/></file_ref>
</result>

The command: xmlstarlet fo --indent-tab --omit-decl templates/newApp_re_novo.xml > templates/newApp_re.xml

And i receive this message: templates/newApp_re_novo.xml:1.145: Extra content at the end of the document 9999999999999999999999999999999

I think it happens because I have two main tags: and But I need the file like this. What could I do?

Thanks. Felipe


Solution

  • A valid XML document always has a single root node and xmlstarlet can only handle valid XML documents. A bit of pre and post processing can get around this:

    { echo '<R>'; cat forest.xml; echo '</R>'; }      | # make into a proper tree
    xmlstarlet fo --indent-tab                        | # indent
    xmlstarlet sel -t -c /R/file_info -n -c /R/result | # break apart into a forest
    sed 's/^\t//'                                       # remove extra indentation