有没有办法找到Nokogiri :: XML :: Element的根标签名称?
问题描述:
有没有办法让根标签名称的名称为Nokogiri::XML::Element
?在参考How do I get the root element name of an XML document using Nokogiri?之后,我尝试使用Nokogiri::XML::Element.xpath('/*').first.name
,这似乎只适用于Nokogiri::XML::Document
。有没有直接的方法来提取Nokogiri::XML::Element
的根标签的名称,而不是将其转换为Nokogiri::XML::Document
并使用上述方法?有没有办法找到Nokogiri :: XML :: Element的根标签名称?
例子:
child_element =
<<~XML
<child2>
<developer>
<name>xyz</name>
<email>[email protected]</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
提前感谢!
答
我想我想通了!
puts child_nokogiri_document.xpath('//developer').first.name #=> developer
我尝试过很多方法,但那种错过了这个速战速决。
“Nokogiri :: XML :: Element”响应'name'。也许你可以提供[mcve]? – Stefan