user2956477
user2956477

Reputation: 1360

Extract values from xml document by xidel using xpath

Trying to extract 'text and value' pairs from all lines of DNS XML output using Xidel/Xpath but have no luck with such expression:

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

In fact, I am not able to extraxt any single pair nor single value :-/ Anybody know Xpath better?

BTW: Stackoverflow entering box says there are mostly code and ask me for more details, but I have no idea what kind of details could be added....

Source 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>

Upvotes: 1

Views: 1347

Answers (1)

Reino
Reino

Reputation: 3433

-e "//item/concat(@text,' - ',@value)"

Would return:

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

etc.

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

Would add the section id in front of each line:

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

etc.

[edit]
Although the use of a variable works just fine, it's not really necessary, because the id attribute of the <section> element node, a level higher, can be accessed by prepending ../:

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

Or with xidel's extended string syntax:

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

[/edit]

Upvotes: 1

Related Questions