From: Robert Klemme Date: 2008-08-12T01:42:44+09:00 Subject: Re: Using XPath to retrieve an XML element which contains a given text On 11.08.2008 17:11, Robert Klemme wrote: > I believe "contains" is the wrong function as it does a textual > comparison and I have no idea whether a node is actually allowed as > input. I believe the correct XPath expression is this: Wait, change "correct" to "more appropriate". > "//dataformat[descendant::fileidentifier[text()='SPPT']]" > > Here are some expressions that you may want to try: Here are even more that yield the result you want (or so I believe): [ "//dataformat[descendant::fileidentifier[text()='SPPT']]", "//dataformat[fileidentifiers/fileidentifier[text()='SPPT']]", "//dataformat[descendant::fileidentifier[contains(text(),'SPPT')]]", "//dataformat[fileidentifiers/fileidentifier[contains(text(),'SPPT')]]", "//dataformat[descendant::fileidentifier[starts-with(text(),'SPPT')]]", "//dataformat[fileidentifiers/fileidentifier[starts-with(text(),'SPPT')]]", "//dataformat[descendant::fileidentifier[ends-with(text(),'SPPT')]]", "//dataformat[fileidentifiers/fileidentifier[ends-with(text(),'SPPT')]]", ].each do |xpath| printf "\nXPath: %p\n\n", xpath XPath.each doc, xpath do |elm| puts elm end end Interestingly ends-with() does not seem to work. Maybe we hit a REXML bug. XPath nicely fits Ruby because of TIMTOWTDI. :-) Kind regards robert