2013-05-10 134 views
1

我正在自學Ruby和Nokogiri。我有一個nmap的掃描,輸出如下:如何根據不同的屬性值訪問屬性值?

<host starttime="1368204336" endtime="1368204506"><status state="up" reason="arp- response" reason_ttl="0"/> 
<address addr="192.168.1.254" addrtype="ipv4"/> 
<address addr="88:53:D4:07:F1:3B" addrtype="mac" vendor="Huawei Technologies Co."/> 

我試圖用引入nokogiri通過聲明應該忽略if addrtype="mac"只提取IP地址。這裏是我的代碼:

hosts = nmap_file.xpath('//host/address/@addr') if nmap_file.xpath('//host/address[not(@addrtype="mac")]') 

這是行不通的,當我puts hosts MAC地址仍然包括在內。任何提示將不勝感激。

回答

1

您不需要if聲明;您可以在單個XPath語句中執行此操作。

.xpath('//host/address[not(@addrtype="mac")]/@addr') 

您只需選擇你想要的address元素,然後抓住addr屬性。你很近。

+0

你先生,是一個傳奇。非常感謝。 – Guarin 2013-05-10 20:50:14