xpath find if node exists

后端 未结 6 1461
我在风中等你
我在风中等你 2020-11-28 04:09

Using a xpath query how do you find if a node (tag) exists at all?

For example if I needed to make sure a website page has the correct basic structure like /html/bod

6条回答
  •  心在旅途
    2020-11-28 04:22

    I work in Ruby and using Nokogiri I fetch the element and look to see if the result is nil.

    require 'nokogiri'
    
    url = "http://somthing.com/resource"
    
    resp = Nokogiri::XML(open(url))
    
    first_name = resp.xpath("/movies/actors/actor[1]/first-name")
    
    puts "first-name not found" if first_name.nil?
    

提交回复
热议问题