Get child elements of an element without text nodes

I am using Nokogiri with Ruby to interpret the contents of an XML file. I would like to get an array (or similar) of all elements that are direct children of <where> in my example. However, I get various text nodes (for example, "\n\t\t\t" ), which I don't want. Is there a way to remove or ignore them?

 @body = " <xml> <request> <where> <username compare='e'>Admin</username> <rank compare='gt'>5</rank> </where> </request> </xml>" #in my code, the XML contains tab-indentation, rather than spaces. It is edited here for display purposes. @noko = Nokogiri::XML(@body) xml_request = @noko.xpath("//xml/request") where = xml_request.xpath("where") c = where.children pc 

The above Ruby script outputs:

[#<Nokogiri::XML::Text:0x100344c "\n\t\t\t">, #<Nokogiri::XML::Element:0x1003350 name="username" attributes=[#<Nokogiri::XML::Attr:0x10032fc name="compare" value="e">] children=[#<Nokogiri::XML::Text:0x1007580 "Admin">]>, #<Nokogiri::XML::Text:0x100734c "\n\t\t\t">, #<Nokogiri::XML::Element:0x100722c name="rank" attributes=[#<Nokogiri::XML::Attr:0x10071d8 name="compare" value="gt">] children=[#<Nokogiri::XML::Text:0x1006cec "5">]>, #<Nokogiri::XML::Text:0x10068a8 "\n\t\t">]

I would like to somehow get the following object:

[#<Nokogiri::XML::Element:0x1003350 name="username" attributes=[#<Nokogiri::XML::Attr:0x10032fc name="compare" value="e">] children=[#<Nokogiri::XML::Text:0x1007580 "Admin">]>, #Nokogiri::XML::Element:0x100722c name="rank" attributes=[#<Nokogiri::XML::Attr:0x10071d8 name="compare" value="gt">] children=[#<Nokogiri::XML::Text:0x1006cec "5">]>]

I can currently work around the problem using

 c.each{|child| if !child.text? ... end } 

but c.length == 5 . This would make my life easier if someone can suggest how to exclude direct child text nodes from c, so c.length == 2

+6
source share
1 answer

You have (at least) three options from which you can choose:

+10
source

Source: https://habr.com/ru/post/908477/


All Articles