0
votes

Is there a way to get the name of root tag's name of a Nokogiri::XML::Element? After referring to How do I get the root element name of an XML document using Nokogiri? I tried using Nokogiri::XML::Element.xpath('/*').first.name which seems to work only for Nokogiri::XML::Document. Is there a direct way to extract the name of the root tag of a Nokogiri::XML::Element other than converting it into a Nokogiri::XML::Document and use the above way?

Example:

child_element =
  <<~XML
      <child2>
        <developer>
          <name>xyz</name>
          <email>xyz@abc</email>
          <url>url</url>
          <roles>
            <role>owner</role>
            <role>developer</role>
          </roles>
        </developer>
        <name>Child2</name>
        <qualification>Qualification2</qualification>
      </child2>
  XML

child_nokogiri_document = Nokogiri::XML(child_element, &:noblanks)

puts child_nokogiri_document.xpath('//developer').xpath('/*').first.name #=> child2

Thanks in advance!

1
A Nokogiri::XML::Element responds to name. Maybe you could provide a minimal reproducible example? - Stefan

1 Answers

0
votes

I think I figured it out!

puts child_nokogiri_document.xpath('//developer').first.name #=> developer

I tried many ways but kind of missed out this quick fix.