Closed lflucasferreira closed 4 years ago
Hi, thanks for asking this question, I'll try to help.
I'd like to note that this is a general XPath question, and not a Nokogiri-specific question. A bit of googling led me to https://stackoverflow.com/questions/40934644/xpath-for-element-whose-attribute-value-ends-with-a-specific-string
Nokogiri's version of libxml2 implements XPath 1.0 (and not XPath 2.0) so you'll have to use the more complicated query:
#! /usr/bin/env ruby
require 'nokogiri'
xml = <<EOF
<root>
<TagFoo>asdf</TagFoo>
<TagBar>qwer</TagBar>
<TagBarBaz>zxcv</TagBarBaz>
</root>
EOF
doc = Nokogiri::XML.parse(xml)
results = doc.xpath("//*[substring(name(), string-length(name()) - string-length('Bar')+1) = 'Bar']")
puts results.to_xml
# => <TagBar>qwer</TagBar>
I'll note that there are a few other ways you could have done this, depending on how sensitive you are to performance.
I hope this helps.
Hi there!
I googled but didn't find a way to search for a partial XPath in an XML file.
I need to find everything ending with Status word, for example: \<diskSizeStatus>1\</diskSizeStatus>.
I am using Nokogiri::XML and tried xml.xpath('//@*search', search: 'Status') but was unsuccessful.
Anyone could help me with this, please?