The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

OrBee (61) [Avatar] Offline
Page 171 , gives the following:

#[xpath('/expense/id/text()] which I tried below but didn't work for me using my example.

I tried this other one on the same page for my example.

#[xpath('/expense/id').text] but didn't work for my example.

I tried some other expression I saw online, and that's what currently in the expressions I show.

It seems the xpath expression has many variants as used in the book. However, I am confused as which to use in the following scenario for the data sample below :
<jdbc:connector name="oJdbcConnector" dataSource-ref="dataSource" doc:name="Database">
<jdbc:query key="TestSample" value=" <br /> INSERT INTO Samples ( <br /> type, <br /> batch, <br /> quantity, <br /> location, <br /> labId <br /> ) VALUES ( <br /> <br /> #[Long.valueOf(xpath('/data/type'.text()').text] <br /> #[xpath('/data/batch/text()').text], <br /> #[xpath('/data/quantity/text()').text], <br /> #[xpath('/data/Location/text()').text], <br /> #[Long.valueOf(xpath("/data/results/Lab[text()='WSA']/../Result/text()"smilie.text))] <br /> ) <br /> "></jdbc:query>


Some of the fields don't resolve to the correct values even though I am able to get the values using XPath expressions in Java code. Also, if some of the data are missing the XPath throws a NullPointer exception. I have tried various expressions from the book but they don't seem to work. Is there a way to use like a ternary kind of expression, especially for retrieving the Long value? What should be the right type of Xpath to retrieve the values?
David Dossot (233) [Avatar] Offline
Re: xpath expression confusion
> Page 171

Authors don't have pages numbers in our edition tool, which doesn't inline source code samples either. So page numbers are all dependent of the MEAP version.

Can you please specify the example number? If none, please provide the section number.

OrBee (61) [Avatar] Offline
Re: xpath expression confusion
Section 4.4 and 5.4
Chapter 4: example xpath.xml example XPathTestCase
Chapter 5 example: routeHighPriorityOrdersWithXPath.xml and its corresponding test case.

The XML file examples are similar to the snippet I included but I either get a NullPointerException or null depnding on the xpath expression I use.

if I use the #[xpath('/data/type').text], I get a NullPointerException even with a value in the element. If I use #[xpath('/data/type/text()')] I get null. I have also tried starting the node as //data/type/text() but they don't work either.
David Dossot (233) [Avatar] Offline
Re: xpath expression confusion
Thank you very much: indeed listing 5.4 has a mistake in it. It should read:

<logger message="Error processing expense report:#[xpath('/expense/id').text]" level="ERROR" />

Concerning your issue, the XPath you're using, ie:


works perfectly fine as soon as you fix the XML input. Indeed, what you've pasted above is not XML, it looks like it but it is wrongly structured, it misses closing tags, etc... Once you fix it to become XML, your XPath works great.