From this XML source :
The problem with what you've got:
...
is that it assumes elementName is only a single element's name. If it's an arbitrary XPath expression, the test will fail.
The second problem you'll run into (or probably already have) is that attribute value templates are not allowed in select clauses, so you can't do something simple like this:
What you need is something that will dynamically create the XPath expression to the element you're looking for, and then dynamically evaluate that expression.
For a solution, I turned to EXSLT's evaluate() function, in the dynamic library. I had to use it twice: once to build up the entire XPath expression representing the query, and once to evaluate that query. The advantage of this approach is that you get access to evaluate's full XPath parsing and execution capabilities.
...
where the dyn namespace is declared up top as http://exslt.org/dynamic. Figuring out where to quote here is tricky and took me several tries to get right.
Using these instead of your elementName and value-of expressions, I get:
Test
Foo Bar
Fizz
testBar
testFizz
testBar2
testFizz2
which is what I think you're looking for.
Unfortunately, I'm not versed in MSXML, so I can't tell you whether your specific XSLT processor supports this extension or something similar.