Search code examples
javaxmlxpathxml-namespacesvtd-xml

XPath Evaluation fails while using with AutoPilot in VTD-XML


I have a bunch of xml's which contains elements's with namespaces and i am using VTD-XML to parse that xml. But when i tries to use an xpath to get a namespaced element. It will not fetch at all? Here is the xml sample

<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>

But when i use the xpath as

AutoPilot ap = new AutoPilot(nav);
ap.selectXPath("//HiddenValues");

it works correctly but, when i use this xpath

ap.selectXPath("//XQL");

it will not work.

Please help me how i can ignore namespaces while evaluating XPath?


Solution

  • I do not know VTD-XML, but it looks like you need to declare the namespace of the element XQL. So after a quick loko at http://vtd-xml.sourceforge.net/javadoc/com/ximpleware/AutoPilot.html, I would say:

    AutoPilot ap = new AutoPilot(nav);
    ap.declareXPathNameSpace("xql", "someNameSpace");
    ap.selectXPath("//xql:XQL");
    

    Or if you want to avoid the descendant axis:

    AutoPilot ap = new AutoPilot(nav);
    ap.declareXPathNameSpace("xql", "someNameSpace");
    ap.selectXPath("/startQuery/xql:XQL");
    

    Bonus tip: if startQuery was in another namespace, just declare several prefixes (they do not have to match with the prefixes in the input, the URI they are bound to have to match):

    AutoPilot ap = new AutoPilot(nav);
    ap.declareXPathNameSpace("h", "urn:hypotheticalNamespace");
    ap.declareXPathNameSpace("xql", "someNameSpace");
    ap.selectXPath("/h:startQuery/xql:XQL");
    

    Note: not tested.