Can you get the text() of a jxpath element or does it not work?
given some nice xml:
<?xml version="1.0" encoding="UTF-8"?>
<AXISWeb xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="AXISWeb.xsd">
<Action>
<Transaction>PingPOS</Transaction>
<PingPOS>
<PingStep>To POS</PingStep>
<PingDate>2012-11-15</PingDate>
<PingTime>16:35:57</PingTime>
</PingPOS>
<PingPOS>
<PingStep>POS.PROCESSOR18</PingStep>
<PingDate>2012-11-15</PingDate>
<PingTime>16:35:57</PingTime>
</PingPOS>
<PingPOS>
<PingStep>From POS</PingStep>
<PingDate>2012-11-15</PingDate>
<PingTime>16:35:57</PingTime>
</PingPOS>
</Action>
</AXISWeb>
//Does not work:
jxpc.getValue("/AXISWeb/Action/PingPOS[1]/PingStep/text()");
//Does not work:
jxpc.getValue("/action/pingPOS[1]/PingStep/text()");
//Does not work:
jxpc.getValue("/action/pingPOS[1]/PingStep[text()]");
I know I can get the text from using
jxpc.getValue("/action/pingPOS[1]/PingStep");
But that’s not the point.
Shouldn’t text() work? I could find no examples….
P.S. It’s also very very picky about case and capitalization. Can you turn that off somehow?
Thanks,
-G
/AXISWeb/Action/PingPOS[1]/PingStep/text()is valid XPath for your documentBut, from what I can see from the user guide of jxpath (note: I don’t know jxpath at all),
getValue()is already supposed to return the textual content of a node, so you don’t need to use the XPathtext()at all.So you may use the following:
Extracted from the user guide:
Now about case insensitive query on tag names, if you are using XPath 2 you can use
lower-case()andnode()but this is not really recommended, you may better use correct names.or if using XPath 1, you may use
translate()but it gets even worse:All in all, try to ensure that you use correct query, you know it is case sensitive, so it’s better to pay attention to it. As you would do in Java,
fooandfOoare not the same variables.Edit:
As I said, XML and thus XPath is case sensitive, so
pingStepcannot matchPingStep, use the correct name to find it.Concerning
text(), it is part of XPath 1.0, there is no need for XPath 2 to use it. The JXPathgetValue()is already doing the call totext()for you. If you want to do it yourself you will have to useselectSingleNode("//whatever/text()")that will returns anObjectof typeTextElement(depending on the underlying parser).So to sum up, the method
JXPathContext.getValue()already does the work to select the node’s text content for you, so you don’t need to do it yourself and explicitly call XPath’stext().