String values from XQuery
Added by Anonymous over 15 years ago
Legacy ID: #7311396 Legacy Poster: Talmage (qnox)
Hi, I have been trying to figure out how to obtain a list of string values from an XQuery result but have not come up with a clean way to just get the string value of attributes. What I have been trying: public static void test() throws SaxonApiException { Processor proc = new Processor(false); XQueryCompiler comp = proc.newXQueryCompiler(); comp.declareNamespace("xsd", "http://www.w3.org/2001/XMLSchema"); XQueryExecutable exp = comp.compile("//ITEM/@a"); XQueryEvaluator qe = exp.load(); String data = "<data><ITEM a="Test 123">test</ITEM><ITEM b="Test 1234"></ITEM></data>"; SAXSource source = new SAXSource(new InputSource(new StringReader(data))); qe.setSource(source); XdmValue value = qe.evaluate(); ArrayList<String> values = new ArrayList<String>(); for (XdmItem currValue : value) { //values.add(value.getStringValue()) } } I would like to have my ArrayList contain the strings "Test 123" and "Test 1234" ... what is the "best" way to extract the next from the query result?
Replies (1)
RE: String values from XQuery - Added by Anonymous over 15 years ago
Legacy ID: #7314270 Legacy Poster: Michael Kay (mhkay)
You're almost there. It should be values.add(currValue.getStringValue()). Note that the XQueryEvaluator is itself an Iterable, so you could skip the call on evaluate(): qe.setSource(source); for (XdmItem currValue : qe) { values.add(currValue.getStringValue()); }
Please register to reply