xpath query ..looks tricky
Hi ,
The following is the xml file that i am using.
I want to generate an xpath that will give me an String output such as
"doc1,doc2,doc3,doc4,doc5."
I tried many options either i am able to get a nodelist or only one string (the first doc -
doc1) as output
Can you please tell me how to go about this.
<information>
<reference>
<docid>doc1</docid>
</reference>
<reference>
<docid>doc2</docid>
</reference>
<reference>
<docid>doc3</docid>
</reference>
<referece>
<docid>doc4</docid>
</reference>
<reference>
<docid>doc5</docid>
</reference>
</information>
Thanks for your time
[864 byte] By [
KartikVa] at [2007-10-2 8:26:57]

Many thanks.. i actually iterated through the node list and got it done..but now i am stuck with a stream.closed error.. my code is as follows..i tried resetting the buffer..and creating a new InputSource.. but yet i get the same error..
ByteArrayInputStream baIs = new ByteArrayInputStream(strr.getBytes());
BufferedReader bfr = new BufferedReader(new InputStreamReader(baIs));
InputSource ins = new InputSource(bfr);
String ipcCountText = "count(//foreign-reference/ipc)";
String ipcCount = (String) xpathEvaluator.evaluate(ipcCountText, ins,
XPathConstants.STRING);
Integer intIpcCount = new Integer(ipcCount);
int ipcCnt = intIpcCount.intValue();
StringBuffer sb= new StringBuffer();
String xp = null;
for(int i=1;i<(ipcCnt+1);i++)
{
xp = "//foreign-reference[" + i + "]/ipc";
sb.append((String) xpathEvaluator.evaluate(xp, ins, XPathConstants.STRING));
sb.append(", ");
}
System.out.println("[OUTPUT] = " + sb.toString());
output
--
java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.read(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.skipString(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
Can you please suggest what problem this could be.. i have been struggling with it for some time... hope its not something really silly that i have missed out....
Thanks a lot for your help...
You can't use same InputSream two ore more times.
Better solution is to use complex XPath expression that returns NodeList.
final InputSource source = new InputSource(baIs);
final XPathExpression pathExpression = xPath.compile(
"xpath1 | xpath2");
final NodeList nodeList = (NodeList) pathExpression.evaluate(source, XPathConstants.NODESET);
val0 = nodeList.item(0).getTextContent();
val1 = nodeList.item(1).getTextContent();