我有以下类型的 XPath 表达式//*[@id='test-id' and @some-other-attribute='some-value’]
。我想把它转换成//*[@resourceId='android:id/test-id' and @some-other-attribute='some-value’]
.
我似乎找不到任何库函数可以做到这一点。我不想诉诸正则表达式在字符串上查找和替换 - 寻找一种结构化的方法来做到这一点。感谢任何指针。
注意:我不想针对 XML 文件评估 XPath 表达式。我希望修改 XPath 表达式本身,而不诉诸字符串替换。
Update:
根据 kjhughes@ 的推荐,我使用以下代码来获取 xpath 表达式的解析树。
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());
这会产生:
<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>
我是解析 XML 节点并重构表达式的唯一选择吗?还是有我缺少的更短的方法?
(我仍然需要弄清楚将 XML 转换回 Xpath 表达式是否很简单,或者是否涉及任何技巧。)
如果您想以编程方式重写 XPath 表达式,那么您需要研究 XPath 解析器。看https://www.w3.org/2002/11/xquery-xpath-applets/xpathApplet.html举个例子。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)