代码之家  ›  专栏  ›  技术社区  ›  user2956477

使用xpath通过xidel从xml文档中提取值

  •  1
  • user2956477  · 技术社区  · 9 年前

    尝试使用Xidel/Xpath从DNS XML输出的所有行中提取“文本和值”对,但对此类表达式没有把握:

    xidel -e "/status/section[*]//item[*]@value/@text"
    

    顺便说一句:Stackoverflow输入框中说有大部分代码,并问我更多的细节,但我不知道什么样的细节可以添加。。。。

    源XML:

    <?xml version="1.0" encoding="utf-16"?>
    <status>
      <section id="general" text="General Status">
        <item id="state" text="Current state" value="1" valueText="Running" />
        <item id="uptime" text="Server uptime" value="573262" valueText="6 days, 15 hours, 14 minutes, 22 seconds" />
        <item id="zones" text="DNS zones" value="13" />
        <item id="cached" text="DNS records cached" value="88756" />
      </section>
      <section id="InReq" text="DNS Requests Received">
        <item id="total" text="Total" value="20810744" />
        <item id="udp" text="UDP" value="20810402" />
        <item id="tcp" text="TCP" value="342" />
        <item id="rd" text="Recursive (RD)" value="20599874" />
        <item id="OpCode0" text="OpCode 0 - Request" value="20810732" />
        <item id="OpCode5" text="OpCode 5 - Update Request" value="1" />
        <item id="OpCode10" text="OpCode 10 - unknown" value="11" />
        <item id="QType1" text="Query record type A" value="16596407" />
        <item id="QType2" text="Query record type NS" value="4425" />
        <item id="QType5" text="Query record type CNAME" value="439" />
        <item id="QType6" text="Query record type SOA" value="13101" />
        <item id="QType12" text="Query record type PTR" value="1849989" />
        <item id="QType15" text="Query record type MX" value="8529" />
        <item id="QType16" text="Query record type TXT" value="41016" />
        <item id="QType28" text="Query record type AAAA" value="2084624" />
        <item id="QType29" text="Query record type LOC" value="2" />
        <item id="QType33" text="Query record type SRV" value="104612" />
        <item id="QType35" text="Query record type NAPTR" value="90" />
        <item id="QType38" text="Query record type A6" value="49" />
        <item id="QType43" text="Query record type DS" value="654" />
        <item id="QType48" text="Query record type DNSKEY" value="14" />
        <item id="QType51" text="Query record type NSEC3PARAM" value="1" />
        <item id="QType60" text="Query record type TYPE60" value="35" />
        <item id="QType99" text="Query record type SPF" value="16" />
        <item id="QType252" text="Query record type AXFR" value="6" />
        <item id="QType255" text="Query record type *" value="106719" />
        <item id="typeOther256" text="Other query types" value="4" />
        <item id="classIN" text="Query class IN" value="20810649" />
        <item id="classCH" text="Query class CH" value="80" />
        <item id="classOther256" text="Other query classes" value="3" />
      </section>
      <section id="DropReq" text="Dropped/Ignored DNS Requests">
        <item id="total" text="Total" value="510261" />
        <item id="repeat" text="Repeat requests" value="510261" />
        <item id="ipblock" text="IP Blocking" value="0" />
        <item id="stealth" text="Lame request Stealth option" value="0" />
        <item id="notify" text="Notify request not matched" value="0" />
        <item id="maxrecur" text="Exceeded max. parallel recursive" value="0" />
        <item id="ignoreroot" text="Ignored UDP request for &lt;root&gt;" value="0" />
        <item id="ignoreudpany" text="Ignored UDP 'ANY' request" value="0" />
        <item id="ignoreplugin" text="Ignore DNS request plug-in" value="0" />
      </section>
      <section id="OutResp" text="DNS Responses Sent">
        <item id="total" text="Total" value="20300475" />
        <item id="aa" text="Authoritative (AA)" value="176020" />
        <item id="tc" text="Truncated (TC)" value="1430" />
        <item id="ra" text="Recursion Available (RA)" value="20032753" />
        <item id="rcode0" text="RCODE 0 - No Error" value="16196886" />
        <item id="rcode1" text="RCODE 1 - Format Error" value="262" />
        <item id="rcode2" text="RCODE 2 - Server Failure" value="35985" />
        <item id="rcode3" text="RCODE 3 - Non-Existent Domain" value="3943192" />
        <item id="rcode4" text="RCODE 4 - Not implemented" value="80" />
        <item id="rcode5" text="RCODE 5 - Query Refused" value="124070" />
      </section>
      <section id="OutReq" text="DNS Requests Sent">
        <item id="total" text="Total" value="5220257" />
        <item id="notify" text="Notify" value="6" />
        <item id="zt" text="Zone transfer" value="7" />
        <item id="ixfr" text="Incremental zone transfer (IXFR)" value="53" />
        <item id="soarefresh" text="Zone SOA refresh" value="0" />
      </section>
      <section id="InResp" text="DNS Responses Received">
        <item id="total" text="Total" value="5152878" />
      </section>
      <ReqPerSec>39,42,48,40,38,36,40,41,26,31,35,18,26,31,19,32,19,32,35,45,22,24,33,18,32,35,17,34,46,37,29,25,46,55,22,36,35,36,40,34,44,34,40,36,21,17,20,36,17,33,31,26,23,27,40,51
    ,31,40,25,42</ReqPerSec>
    </status>
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Reino    5 年前
    -e "//item/concat(@text,' - ',@value)"
    

    将返回:

    Current state - 1
    Server uptime - 573262
    DNS zones - 13
    

    等

    -e "//section/(let $a:=@id return item/concat('[',$a,'] ',@text,' - ',@value))"
    

    [general] Current state - 1
    [general] Server uptime - 573262
    [general] DNS zones - 13
    

    等


    虽然变量的使用效果很好,但实际上没有必要,因为 id <section> 元素节点的级别更高,可以通过前缀访问 ../ :

    -e "//item/concat('[',../@id,'] ',@text,' - ',@value)"
    

    或与 xidel 的扩展字符串语法:

    -e "//item/x'[{../@id}] {@text} - {@value}'"
    

    [/编辑]