XMLStarlet 没有 select xpath 查询正确

XMLStarlet doesn't select xpath query correctly

我有以下XML

<?xml version='1.0' encoding='UTF-8'?>
<ListBucketResult xmlns='http://doc.s3.amazonaws.com/2006-03-01'>
    <Name>chromedriver</Name>
    <Prefix></Prefix>
    <Marker></Marker>
    <IsTruncated>false</IsTruncated>
    <Contents>
        <Key>2.0/chromedriver_linux32.zip</Key>
        <Generation>1380149859530000</Generation>
        <MetaGeneration>4</MetaGeneration>
        <LastModified>2013-09-25T22:57:39.349Z</LastModified>
        <ETag>"c0d96102715c4916b872f91f5bf9b12c"</ETag>
        <Size>7262134</Size>
    </Contents>
    <Contents>
        <Key>2.0/chromedriver_linux64.zip</Key>
        <Generation>1380149860664000</Generation>
        <MetaGeneration>4</MetaGeneration>
        <LastModified>2013-09-25T22:57:40.449Z</LastModified>
        <ETag>"858ebaf47e13dce7600191ed59974c09"</ETag>
        <Size>7433593</Size>
    </Contents>
    ...
</ListBucketResult>

而且我尝试 select 使用此命令的唯一关键节点:

xmlstarlet sel -T -t -m '/ListBucketResult/Contents/Key' -v '.' -n file.xml

我尝试了一些命令,但是 none return 任何值

然后我试着用 el 查看结构:

xmlstarlet el file.xml
ListBucketResult
ListBucketResult/Name
ListBucketResult/Prefix
ListBucketResult/Marker
ListBucketResult/IsTruncated
ListBucketResult/Contents
ListBucketResult/Contents/Key
ListBucketResult/Contents/Generation
ListBucketResult/Contents/MetaGeneration
ListBucketResult/Contents/LastModified
ListBucketResult/Contents/ETag
ListBucketResult/Contents/Size

我不知道哪里不对

您的 XML 元素绑定到命名空间 http://doc.s3.amazonaws.com/2006-03-01,但您的 XPath 未引用任何命名空间(未使用命名空间前缀)。因此,它试图引用“无名称空间”中的元素,但一无所获。

您需要使用 -N 开关声明带有命名空间前缀的命名空间,并在您的 XPath 中使用命名空间前缀:

xmlstarlet sel -N s3="http://doc.s3.amazonaws.com/2006-03-01" -T -t -m '/s3:ListBucketResult/s3:Contents/s3:Key' -v '.' -n file.xml

参考: http://xmlstar.sourceforge.net/doc/UG/ch05s01.html