title: Modify XPath expressions via Java?
body: <p>I have XPath expressions of the type <code>//*[@id='test-id' and @some-other-attribute='some-value’]</code>. I want to convert it into <code>//*[@resourceId='android:id/test-id' and @some-other-attribute='some-value’]</code>.</p>
  
  <p>I can't seem to find any library function to do that. I don't want to resort to regex find and replace on the string - looking for a structured way to do it. Appreciate any pointer.</p>
  
  <p>NOTE: I am not looking to evaluate an XPath expression against an XML file. I am looking to modify the XPath expression itself, without resorting to string replace.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Following kjhughes@ recommendation, I got to the following code to get a parse tree of the xpath expression. </p>
  
  <pre><code>Processor proc = new Processor(false);
  XPathCompiler p = new XPathCompiler(proc);
  XPathExecutable exec = p.compile("//*[@id='test-id' and @some-other-attribute='some-value']");
  exec.getUnderlyingExpression().getInternalExpression().explain(new StandardLogger());
  </code></pre>
  
  <p>This produces:</p>
  
  <pre><code>&lt;filterExpression&gt;
     &lt;slash simple-step="true"&gt;
        &lt;root/&gt;
        &lt;axis name="descendant" nodeTest="element()"/&gt;
     &lt;/slash&gt;
     &lt;operator op="and"&gt;
        &lt;operator op="=" cardinality="one-to-one"&gt;
           &lt;data&gt;
              &lt;axis name="attribute" nodeTest="attribute(Q{}id)"/&gt;
           &lt;/data&gt;
           &lt;literal value="test-id" type="xs:string"/&gt;
        &lt;/operator&gt;
        &lt;operator op="=" cardinality="one-to-one"&gt;
           &lt;data&gt;
              &lt;axis name="attribute" nodeTest="attribute(Q{}some-other-attribute)"/&gt;
           &lt;/data&gt;
           &lt;literal value="some-value" type="xs:string"/&gt;
        &lt;/operator&gt;
     &lt;/operator&gt;
  &lt;/filterExpression&gt;
  </code></pre>
  
  <p>Is my only option to parse the XML nodes and recompose the expression? Or is there a shorter way that I am missing?</p>
  
  <p>(I still have to figure out if converting the XML back to Xpath expression is trivial or if it will involve any trickery.)</p>