# Parameterizing REXML::XPath expressions

**URL:** <https://www.ruby-forum.com/t/parameterizing-rexml-xpath-expressions/107628>\
**Category:** Ruby\
**Created:** [June 24, 2007, 10:41am UTC](https://www.ruby-forum.com/t/parameterizing-rexml-xpath-expressions/107628 "2007-06-24T10:41:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SySySy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@SySySy](https://www.ruby-forum.com/u/SySySy)\
**Post date:** [June 24, 2007, 10:41am UTC](https://www.ruby-forum.com/t/parameterizing-rexml-xpath-expressions/107628/1 "2007-06-24T10:41:35Z")

</div>

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”]/  
@version’).to\_s

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.

---

<div class="post-metadata">

**Author:** ![SySySy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/848f3c/32.png) [@SySySy](https://www.ruby-forum.com/u/SySySy)\
**Post date:** [June 24, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/parameterizing-rexml-xpath-expressions/107628/2 "2007-06-24T13:50:51Z")

</div>

I think currently  
version = REXML::XPath.match(@doc, ‘//[@pn=partnum]/  
@version’).to\_s

Is looking for an element with pn=“partnum”.

What you need is the value of partnum:

version = REXML::XPath.match(@doc, “//[@pn=’#{partnum}’]/  
@version”).to\_s

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.

Regards,  
Emil I.
