How to do group capture in xpath
===

I am looking for something in xpath can do: //foo[@n="$1"]//bar[@n="$2"]
which can return me $1 and $2.  Or at least return me both <foo> and <bar>

Here is more detail, I have a xml document:

    <xml>
      <foo>
        <a n="1">
          <b n="1"/>
          <b n="2"/>
        </a>
      </foo>
      <a n="2">
        <b n="1"/>
      </a>
      <a n="3">
        <b n="1"/>
        <foo>
          <b n="2"/>
        </foo>
        <b n="3"/>
      </a>
    </xml>

I want generate a string base on the n attribute in <a> and <b>
So I have xpath: //a[@n]//b[@n]
Then for every result I get back, I use: ./@n and ./ancestor::a/@n to get the info I want.

This is working fine, but I need something more intelligent, because I have a lot structure like this, and need auto generate the xpath.


