I have XPath expressions of the type //*[@id='test-id' and @some-other-attribute='some-value’]
. I want to convert it into //*[@resourceId='android:id/test-id' and @some-other-attribute='some-value’]
.
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.
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.
Update:
Following kjhughes@ recommendation, I got to the following code to get a parse tree of the xpath expression.
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());
This produces:
<filterExpression>
<slash simple-step="true">
<root/>
<axis name="descendant" nodeTest="element()"/>
</slash>
<operator op="and">
<operator op="=" cardinality="one-to-one">
<data>
<axis name="attribute" nodeTest="attribute(Q{}id)"/>
</data>
<literal value="test-id" type="xs:string"/>
</operator>
<operator op="=" cardinality="one-to-one">
<data>
<axis name="attribute" nodeTest="attribute(Q{}some-other-attribute)"/>
</data>
<literal value="some-value" type="xs:string"/>
</operator>
</operator>
</filterExpression>
Is my only option to parse the XML nodes and recompose the expression? Or is there a shorter way that I am missing?
(I still have to figure out if converting the XML back to Xpath expression is trivial or if it will involve any trickery.)