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]
# 1
yes it will be tricky given the different spellings of refere{n}ce
dmbdmba at 2007-7-16 22:27:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
> I tried many options either i am able to get a nodelist or only one stringHave you considered doing a tiny bit of work? Take each node in the nodelist and convert it to a string. Put commas between the nodes.
DrClapa at 2007-7-16 22:27:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

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...

KartikVa at 2007-7-16 22:27:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

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();

NikolayMalevannya at 2007-7-16 22:27:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
xpath that will give me an String output such as "doc1,doc2,doc3,doc4,doc5."XPath is used to select single nodes or a nodel list, not output a String created from mulitple nodes.
dvohra09a at 2007-7-16 22:27:12 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...