我有一堆xml,其中包含带有名称空间的元素,我使用VTD-XML来解析该xml。但是,当我尝试使用xpath获取名称空间元素时。根本拿不来吗?以下是xml示例
<startQuery logLinkID="797ca6afb7a32458:1619f4f5:14d8fa72127:-7ff5" modelName="SampleModel" tracerString="tracer:: std.Page1.Pane1">
<xql:XQL xmlns:xql="someNameSpace" cellCountThreshold="1000000" dumpOptions="" enableThresholdDrillDown="false" maxOverridenValueArraySize="" returnFullPath="true" returnNormalizedXql="true">
<HiddenValues errors="true" nulls="false" zeros="false" />
<CellAttributes cellsAsAttachment="true" editUEVInfo="false" formattingInfo="false" uevInfo="false" writeBackInfo="true" /></xql:XQL>
</startQuery>但是当我使用xpath作为
AutoPilot ap = new AutoPilot(nav);
ap.selectXPath("//HiddenValues");它工作正常,但是,当我使用这个xpath时
ap.selectXPath("//XQL");这是行不通的。
请帮助我在计算XPath时如何忽略名称空间?
发布于 2015-06-03 15:18:33
我不知道VTD,但看起来您需要声明元素XQL的命名空间。因此,在http://vtd-xml.sourceforge.net/javadoc/com/ximpleware/AutoPilot.html的快速洛科会议之后,我想说:
AutoPilot ap = new AutoPilot(nav);
ap.declareXPathNameSpace("xql", "someNameSpace");
ap.selectXPath("//xql:XQL");或者,如果您想避免后代轴:
AutoPilot ap = new AutoPilot(nav);
ap.declareXPathNameSpace("xql", "someNameSpace");
ap.selectXPath("/startQuery/xql:XQL");额外提示:如果 startQuery 是另一个名称空间中的,只需声明几个前缀(它们不必与输入中的前缀匹配,它们必须匹配的URI ):
AutoPilot ap = new AutoPilot(nav);
ap.declareXPathNameSpace("h", "urn:hypotheticalNamespace");
ap.declareXPathNameSpace("xql", "someNameSpace");
ap.selectXPath("/h:startQuery/xql:XQL");注:未测试。
https://stackoverflow.com/questions/30623681
复制相似问题