将多个XPath组合到一个XPath中

时间:2016-03-15 15:54:53

标签: xml xpath

我有一个包含多个host元素的XML文件。请参阅下面的示例。

<host starttime="1458033438" endtime="1458033455">
      <status state="up" reason="localhost-response" reason_ttl="0" />
      <address addr="192.168.1.58" addrtype="ipv4" />
      <hostnames>
         <hostname name="ubuntu.gateway.2wire.net" type="PTR" />
      </hostnames>
      <ports>
         <port protocol="tcp" portid="22">
            <state state="open" reason="syn-ack" reason_ttl="64" />
            <service name="ssh" method="table" conf="3" />
         </port>
         <port protocol="tcp" portid="23">
            <state state="closed" reason="reset" reason_ttl="64" />
            <service name="telnet" method="table" conf="3" />
         </port>
      </ports>
      <os>
         <portused state="open" proto="tcp" portid="22" />
         <portused state="closed" proto="tcp" portid="23" />
         <portused state="closed" proto="udp" portid="43164" />
         <osmatch name="Linux 3.7 - 3.9" accuracy="98" line="76104">
            <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="98">
               <cpe>cpe:/o:linux:linux_kernel:3</cpe>
            </osclass>
         </osmatch>
         <osmatch name="Netgear DG834G WAP or Western Digital WD TV media player" accuracy="96" line="61906">
            <osclass type="WAP" vendor="Netgear" osfamily="embedded" accuracy="96">
               <cpe>cpe:/h:netgear:dg834g</cpe>
            </osclass>
            <osclass type="media device" vendor="Western Digital" osfamily="embedded" accuracy="96">
               <cpe>cpe:/o:westerndigital:wd_tv</cpe>
            </osclass>
         </osmatch>
         <osmatch name="Linux 3.8" accuracy="95" line="77648">
            <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="95">
               <cpe>cpe:/o:linux:linux_kernel:3</cpe>
            </osclass>
         </osmatch>
         <osmatch name="Linux 3.1" accuracy="93" line="76656">
            <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="93">
               <cpe>cpe:/o:linux:linux_kernel:3</cpe>
            </osclass>
         </osmatch>
         <osmatch name="Linux 3.2" accuracy="93" line="77811">
            <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="93">
               <cpe>cpe:/o:linux:linux_kernel:3</cpe>
            </osclass>
         </osmatch>
         <osmatch name="AXIS 210A or 211 Network Camera (Linux 2.6)" accuracy="92" line="7328">
            <osclass type="webcam" vendor="AXIS" osfamily="Linux" osgen="2.6.X" accuracy="92">
               <cpe>cpe:/h:axis:210a_network_camera</cpe>
               <cpe>cpe:/h:axis:211_network_camera</cpe>
               <cpe>cpe:/o:axis:linux_kernel:2.6</cpe>
            </osclass>
         </osmatch>
         <osmatch name="Linux 3.7" accuracy="92" line="77269">
            <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="92">
               <cpe>cpe:/o:linux:linux_kernel:3</cpe>
            </osclass>
         </osmatch>
         <osmatch name="Linux 3.9" accuracy="91" line="78127">
            <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="3.X" accuracy="91">
               <cpe>cpe:/o:linux:linux_kernel:3</cpe>
            </osclass>
         </osmatch>
         <osmatch name="Crestron XPanel control system" accuracy="91" line="15741">
            <osclass type="specialized" vendor="Crestron" osfamily="2-Series" accuracy="91">
               <cpe>cpe:/o:crestron:2_series</cpe>
            </osclass>
         </osmatch>
         <osmatch name="Linux 2.4.26 (Slackware 10.0.0)" accuracy="91" line="34666">
            <osclass type="general purpose" vendor="Linux" osfamily="Linux" osgen="2.4.X" accuracy="91">
               <cpe>cpe:/o:linux:linux_kernel:2.4.26</cpe>
            </osclass>
         </osmatch>
         <osfingerprint fingerprint="OS:SCAN(V=6.40%E=4%D=3/15%OT=22%CT=23%CU=43164%PV=Y%DS=0%DC=L%G=Y%TM=56E7D3&#xA;OS:2F%P=x86_64-pc-linux-gnu)SEQ(SP=105%GCD=2%ISR=108%TI=Z%CI=I%TS=8)SEQ(SP=&#xA;OS:105%GCD=1%ISR=108%TI=Z%CI=I%II=I%TS=8)OPS(O1=MFFD7ST11NW7%O2=MFFD7ST11NW&#xA;OS:7%O3=MFFD7NNT11NW7%O4=MFFD7ST11NW7%O5=MFFD7ST11NW7%O6=MFFD7ST11)WIN(W1=A&#xA;OS:AAA%W2=AAAA%W3=AAAA%W4=AAAA%W5=AAAA%W6=AAAA)ECN(R=Y%DF=Y%T=40%W=AAAA%O=M&#xA;OS:FFD7NNSNW7%CC=Y%Q=)T1(R=Y%DF=Y%T=40%S=O%A=S+%F=AS%RD=0%Q=)T2(R=N)T3(R=N)&#xA;OS:T4(R=Y%DF=Y%T=40%W=0%S=A%A=Z%F=R%O=%RD=0%Q=)T5(R=Y%DF=Y%T=40%W=0%S=Z%A=S&#xA;OS:+%F=AR%O=%RD=0%Q=)T6(R=Y%DF=Y%T=40%W=0%S=A%A=Z%F=R%O=%RD=0%Q=)T7(R=Y%DF=&#xA;OS:Y%T=40%W=0%S=Z%A=S+%F=AR%O=%RD=0%Q=)U1(R=Y%DF=N%T=40%IPL=164%UN=0%RIPL=G&#xA;OS:%RID=G%RIPCK=G%RUCK=G%RUD=G)IE(R=Y%DFI=N%T=40%CD=S)&#xA;" />
      </os>
      <uptime seconds="4548" lastboot="Tue Mar 15 16:01:47 2016" />
      <distance value="0" />
      <tcpsequence index="262" difficulty="Good luck!" values="8881A15E,A2265F66,91E9E2AB,6F639226,E215DCDA,9548210E" />
      <ipidsequence class="All zeros" values="0,0,0,0,0,0" />
      <tcptssequence class="other" values="117BB0,117BC9,117BE3,117BFC,117C15,117C2E" />
      <times srtt="43" rttvar="508" to="100000" />
   </host>

在我的网络中,有几台计算机运行Linux,Microsoft和Apple操作系统。我只对显示“Linux”系统感兴趣。我已经能够使用下面的XPath,但这仅适用于一个属性:IP或可用的MAC地址。

//host[status[@state='up'] and os[osmatch/osclass[@osfamily='Linux']]]/address[@addrtype='ipv4' or @addrtype='mac']/@addr 

除IP地址外,我还需要显示hostname的名称和协议(服务名称(SSH / Telnet),打开/关闭状态和端口号。我有以下内容XPath的:

//host[status[@state='up'] and os[osmatch/osclass[@osfamily='Linux']]]/hostnames/hostname[@name]

//host[status[@state='up'] and os[osmatch/osclass[@osfamily='Linux']]]/os/portused/@portid

//host[status[@state='up'] and os[osmatch/osclass[@osfamily='Linux']]]/os/portused/@open

我需要结合所有这些XPath来生成以下输出:

Hosts running Linux
------------------------- 
Hostname: ubuntu.gateway.2wire.net
Host IP: 192.168.1.158 
Protocols: SSH (open/22), Telnet (closed/23)

无论如何,我可以将所有XPath合并到一个Xpath中吗?

1 个答案:

答案 0 :(得分:0)

我会使用XSLT,它可以一次性完成 - 拉出你想要的XPath并显示它旁边的标签。