我有以下文件 log.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<EntryList xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/">
  <Entries>
    <RankedEntry>
      <Entry>
        <id>1</id>
        <code1>001</code1>
        <displayName>Entry 1</displayName>
        <code2>003</code2>
        <code3>0100000000000005203</code3>
        <actualName>Entry Number 1</actualName>
        <entryType>1</entryType>
        <metaScore>1</metaScore>
        <usage>81</usage>
      </Entry>
      <factor>30.738496780395508</factor>
      <virtualFactor>45.738494873046875</virtualFactor>
    </RankedEntry>
    <RankedEntry>
      <Entry>
        <id>2</id>
        <code1>001</code1>
        <displayName>Entry 2</displayName>
        <code2>003</code2>
        <code3>0200000000000005203</code3>
        <actualName>Entry Number 2</actualName>
        <entryType>26</entryType>
        <metaScore>1</metaScore>
        <usage>61</usage>
      </Entry>
      <factor>40.738496780395508</factor>
      <virtualFactor>55.738494873046875</virtualFactor>
    </RankedEntry>
    <RankedEntry>
      <Entry>
        <id>3</id>
        <code1>001</code1>
        <displayName>Entry 3</displayName>
        <code2>003</code2>
        <code3>0300000000000005203</code3>
        <actualName>Entry Number 3</actualName>
        <entryType>3</entryType>
        <metaScore>1</metaScore>
        <usage>41</usage>
      </Entry>
      <factor>50.738496780395508</factor>
      <virtualFactor>65.738494873046875</virtualFactor>
    </RankedEntry>
  </Entries>
</EntryList>

在使用 xmlstarlet 1.6.1 的 Window 7 上,如果我运行以下命令,

xml sel -t -m "/EntryList/Entries/RankedEntry/Entry/entryType"-v "."日志文件

预期结果是:
1
26
3

实际的:
但我没有得到任何返回

任何想法如何解决它?

最佳答案

您必须考虑命名空间。使用 XMLStarlet -N 选项定义命名空间前缀:

  -N <name>=<value>         - predefine namespaces (name without 'xmlns:')
                              ex: xsql=urn:oracle-xsql
                              Multiple -N options are allowed.

然后这个 XMLStarlet 命令:
xml sel -N s=http://schemas.datacontract.org/2004/07/ -t -v "/s:EntryList/s:Entries/s:RankedEntry/s:Entry/s:entryType" log.xml

将产生请求的输出:
1
26
3

关于xml sel -t -m 根本不适合我,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25889670/

10-16 20:29