Project

Profile

Help

Returning result of Saxon Xpath as a string

Added by Anonymous about 13 years ago

Legacy ID: #10025780 Legacy Poster: https://me.yahoo.com/a/4Igw2EEk ()

Hi! I'm currently converting some xml manipulation code to use Saxon and got a problem in returning the result of Saxon9 Xpath in a form of a string. [b]Example XML input[/b] [code] 1 2 3 4 5 6 [/code] [b]Xpath used[/b]: /Foo/Bar [b]Below code:[/b] [code]Configuration config = new Configuration(); config.setLineNumbering(true); XPathEvaluator xpath = new XPathEvaluator(); try { StringReader strRead = new StringReader("123456"); List results = (List) xpath.evaluate("/Foo/Bar", new StreamSource(strRead), XPathConstants.NODESET); for (int i=0; i<results.size(); i++) { net.sf.saxon.tinytree.TinyNodeImpl object = (net.sf.saxon.tinytree.TinyNodeImpl) results.get(i); System.out.println(object.getNodeKind() + " " + object.getDisplayName()); // Some code go here } } catch (Exception ex) { ex.printStackTrace(); } finally { }[/code] We need to get the output as a string. 1 2 3 4 5 6 Thank you very much for the help. Sincerely, Gerald.


Replies (3)

Please register to reply

RE: Returning result of Saxon Xpath as a string - Added by Anonymous about 13 years ago

Legacy ID: #10031445 Legacy Poster: Michael Kay (mhkay)

First, I would suggest you cast to net.sf.saxon.om.NodeInfo rather than to net.sf.saxon.tinytree.TinyNodeImpl - NodeInfo is a much more stable and public interface. The simplest interface to serialize a NodeInfo object as XML is the static method net.sf.saxon.query.QueryResult.serialize(NodeInfo node). This sets the serialization options to indent=yes, omit-xml-declaration=yes, which appears to be what you are looking for. If you want more control, there is also a four-argument serialize() method in the same class.

RE: Returning result of Saxon Xpath as a string - Added by Anonymous about 13 years ago

Legacy ID: #10034154 Legacy Poster: https://me.yahoo.com/a/4Igw2EEk ()

Michael Thank you very much for the help it solve my problem. Sincerely, Gerald

RE: Returning result of Saxon Xpath as a string - Added by Anonymous about 13 years ago

Legacy ID: #10034707 Legacy Poster: https://me.yahoo.com/a/4Igw2EEk ()

Hi Michael, Got another question in returning the values of an attribute, is there a way for Saxon to return the values of an attribute in an XPath? [b]XML Input[/b] [code] 1 2 3 4 5 6 [/code] [b]Xpath:[/b] /Foo/Bar/Baz/@sample [b]Below Code:[/b] [code]Configuration config = new Configuration(); config.setLineNumbering(true); config.setAllNodesUntyped(true); XPathEvaluator xpath = new XPathEvaluator(); try { StringReader strRead = new StringReader("<Baz sample="id1">123456"); List results = (List) xpath.evaluate("/Foo/Bar/Baz/@sample", new StreamSource(strRead), XPathConstants.NODESET); for (int i=0; i<results.size(); i++) { NodeInfo object = (NodeInfo) results.get(i); Properties props = new Properties(); props.setProperty(OutputKeys.METHOD, "text"); props.setProperty(OutputKeys.ENCODING, "UTF-8"); props.setProperty("method", "xml"); props.setProperty(OutputKeys.INDENT, "yes"); props.setProperty(OutputKeys.STANDALONE, "true"); props.setProperty(OutputKeys.MEDIA_TYPE, "text"); props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); props.setProperty(SaxonOutputKeys.WRAP, "yes"); StringWriter sw = new StringWriter(); QueryResult.serialize(object, new StreamResult(sw), props); System.out.println(sw.toString()); System.out.println(""); } } catch (Exception ex) { ex.printStackTrace(); } finally { }[/code] [b]Exception [/b] [code]net.sf.saxon.event.NoOpenStartTagException: Cannot create an attribute node (sample) whose parent is a document node at net.sf.saxon.event.NoOpenStartTagException.makeNoOpenStartTagException(NoOpenStartTagException.java:49) at net.sf.saxon.event.TreeReceiver.attribute(TreeReceiver.java:189) at net.sf.saxon.tinytree.TinyAttributeImpl.copy(TinyAttributeImpl.java:185) at net.sf.saxon.event.TreeReceiver.append(TreeReceiver.java:295) at net.sf.saxon.query.QueryResult.serializeSequence(QueryResult.java:187) at net.sf.saxon.query.QueryResult.serialize(QueryResult.java:116) at com.ti.itg.peit.core.xml.util.XMLUtil.main(XMLUtil.java:110)[/code] Thank you very much. Sincerely, Gerald

    (1-3/3)

    Please register to reply