2

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?

M A
  • 71,713
  • 13
  • 134
  • 174
Sumanth Shastry
  • 1,139
  • 1
  • 19
  • 28

1 Answers1

2

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.

Florent Georges
  • 2,190
  • 1
  • 15
  • 24
  • That works like charm. Thanks for the solution. The bonus tip you mentioned doesnot work here since startQuery is in same namespace. But, thanks for the tip. – Sumanth Shastry Jun 03 '15 at 17:43
  • That's why "if ... was" is in bold: I am not saying it applies here, I just explain the general idea :-) – Florent Georges Jun 03 '15 at 18:09
  • yeah i understood what you are saying. i will try to remember it in the future. Thanks a lot – Sumanth Shastry Jun 03 '15 at 18:17
  • Go to vtd website and download the vtdnav.java, then compile the lib. This problem should go away. It will ignore the XPath without namespace declaration. – vtd-xml-author Jun 04 '15 at 03:52
  • @vtd-xml-author i am having the latest vtd-xml 2.11.jar only. This is the last version they have released. So how can i get latest vtdNav.java file and please tell me the steps. – Sumanth Shastry Jun 04 '15 at 10:14
  • does this help? http://stackoverflow.com/questions/22360885/how-to-ingore-namespace-prefixes-on-vtd-xpath-lookup – vtd-xml-author Jun 05 '15 at 00:05