0

I am trying to figure out how the evalXPathToString method should be coded to allow me to have xpaths with text() at the end or not:

String xml = <xmlDoc><noele>junk</noele><valss>1.45</valss><other>junk2</other></xmlDoc>   

assertThat(evalXPathToString(xml, "/xmlDoc/valss"), is("1.45"));
assertThat(evalXPathToString(xml, "/xmlDoc/noele"), is("junk"));
assertThat(evalXPathToString(xml, "/xmlDoc/other/text()"), is("junk2"));

Version 2.13 of vtd-xml.

EDIT for what I have:

autoPilot.selectXPath(xpath);

if(hasMoreElements(autoPilot)) {
    int textIndex = vtdNav.getText() ;
    if(textIndex != -1) {
        return vtdNav.toNormalizedString(textIndex);
    }
}

...but the final assert doesn't pass.

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30
Cheetah
  • 13,785
  • 31
  • 106
  • 190

1 Answers1

0

When you query the text value (having text() as the last step, to be specific) directly with xpath, there is no need for additional getText() call in your assertion code. The reason is that /xmlDoc/other/text() already returns the text node or -1. If you run getText() at that node position, you will always -1 because you are trying to get the text node of a text node.

So the fixes are:

  1. run xpath query of '/xmlDoc/other', then test the assertion of is(). Your logic will work
  2. run xpath query of '/xmlDoc/other/text(),' and test the return value for -1 or check string value directly.

One additional performance-enhancing advice: there is no need for vtd index to string conversion... vtdNav has an array of functions to compare vtd tokens to a string value directly... one of which is called vtdNav.comparetokenString()

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30
  • Thanks - with regards to the performance note, I'm actually writing a method for string extraction, the code you see above is just to test my `evalXpathToString()` method. Thank you for the tips! – Cheetah Jul 23 '16 at 08:01