1

Having an xml file:

<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  <Contents>
    <Key>README</Key>
    <LastModified>2018-09-27T16:04:24.616Z</LastModified>
    <ETag>"00000000000000000000000000000000-1"</ETag>
    <Size>94</Size>
    <Owner>
      <ID>02d6176db174dc93cb1b899f7c6078f08654445fe8cf1b6ce98d8855f66bdbf4</ID>
      <DisplayName/>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Contents>
  <Contents>
    <Key>file1.zip</Key>
    <LastModified>2009-09-13T16:09:35.000Z</LastModified>
    <ETag>"00000000000000000000000000000000-1"</ETag>
    <Size>170486537</Size>
    <Owner>
      <ID>02d6176db174dc93cb1b899f7c6078f08654445fe8cf1b6ce98d8855f66bdbf4</ID>
      <DisplayName/>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Contents>
  <Contents>
    <Key>file2.zip.torrent</Key>
    <LastModified>2009-09-14T03:35:56.000Z</LastModified>
    <ETag>"00000000000000000000000000000000-1"</ETag>
    <Size>13884</Size>
    <Owner>
      <ID>02d6176db174dc93cb1b899f7c6078f08654445fe8cf1b6ce98d8855f66bdbf4</ID>
      <DisplayName/>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Contents>
</ListBucketResult>

Now trying to extract with xmlstarlet:

xmlstarlet sel -t -c '//Key' dl.xml

gives back nothing. Same for

xmlstarlet sel -t -c '/ListBucket/Result' dl.xml

The only working selections are:

xmlstarlet sel -t -c '*' dl.xml
xmlstarlet sel -t -c '/*' dl.xml
xmlstarlet sel -t -c '//*' dl.xml

As soon as I try to select anything like:

xmlstarlet sel -t -c '//Key' dl.xml
xmlstarlet sel -t -c '/ListBucketResult' dl.xml

No output is given any more. The file given (dl.xml) is plain ASCII.

xmlstarlet reports version 1.6.1-2.1. Within the original file there are about 50000 Contents-nodes, containing a same number of Key-nodes.

Any idea why it does not work as expected and described? Is this file just to big to be processed by xmlstarlet?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • Please add your desired output (no description, no images, no links) for that sample input to your question (no comment). – Cyrus Oct 20 '21 at 14:40

1 Answers1

1

Your file uses namespaces. I suggest:

xmlstarlet select -N x="http://s3.amazonaws.com/doc/2006-03-01/" \
                  --template --value-of '//x:Key' -n dl.xml

Output:

README
file1.zip
file2.zip.torrent
Cyrus
  • 84,225
  • 14
  • 89
  • 153