Forum: Ruby Parameterizing REXML::XPath expressions

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
16f2cad79a9b9c14d60c5252a7ee18fe?d=identicon&s=25 Sy (Guest)
on 2007-06-24 10:41
(Received via mailing list)
Hi there,

I'm struggling a bit with what I think should be straightforward.

I basically want to pull information out of an XML document.

The working example I have is as follows...

version = REXML::XPath.match(@doc, '//[@pn="123-4567-89"]/

However, I don't want to hardcode the value of the part number (pn).
I want to ref it via a variable.

e.g. if I have a variable 'partnum' which is set to "123-4567-89"

then I was expecting the expression to be something like...

version = REXML::XPath.match(@doc, '//[@pn=partnum]/
@version').to_s     # no match

I've tried some variations but with same no match result.
But basically I'm just guessing which is bad.

All of the REXML tutorials always hardcode their examples.
Any help would be appreciated.
73d6d07f2590212606d34c4ad45b3f78?d=identicon&s=25 Емил Иванов / Emil Ivanov (Guest)
on 2007-06-24 13:50
(Received via mailing list)
I think currently
version = REXML::XPath.match(@doc, '//[@pn=partnum]/

Is looking for an element with pn="partnum".

What you need is the value of partnum:

version = REXML::XPath.match(@doc, "//[@pn='#{partnum}']/

Note the quotes around the string and those around the #{}.

This is called string interpolation.

I'm not 100% sure it will work, but it should do.

Hope that helps.

Emil Ivanov
This topic is locked and can not be replied to.