Search code examples
rubyxmlnokogirilibxml2

How to get the tag.contents


I have this XML:

<record>
    <f id="27">John Smith</f>
    <f id="28"/>
</record>

and parse it with Nokogiri this way:

# I get the record from the whole document
... 
fields = record.xpath("f")
for field in fields
    puts field.content
end

which returns this:

John Smith
\n 28 \n

This is incorrect. The second field tag does not have anything inside the tag, it should return an empty value. Right?

By the way, the same thing happens with LibXML.

This is the Actual code:

xml = Nokogiri::XML("<?xml version="1.0" ?><records><record><f id="27">John Smith</f><f id="38"/></record></records>")

records = xml.xpath("//record")
records.map{|record|
    fields = record.xpath("f")
    fields.to_enum(:each_with_index).collect{|field,index|
        [field.content, index]
    }
}

Solution

  • I'll answer the question. The tag probably contains other tags in it that you might've missed.