如何根据不同的属性值访问属性值?

时间:2013-05-10 18:11:48

标签: ruby xpath attributes nokogiri

我正在教自己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 个答案:

答案 0 :(得分:1)

您不需要if声明;您可以在单个XPath语句中执行此操作。

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

您只需选择所需的address元素,然后抓取addr属性即可。你很亲密。

相关问题