Project

Profile

Help

how to return a node from extension function?

Added by Anonymous over 18 years ago

Legacy ID: #3365898 Legacy Poster: Kevin Rodgers (notorious_kev)

Thanks for the suggestion on how to handle relative URIs (by passing the base URI explicitly). That works just fine. My next question is how to construct and return a document node, given the names of the document element, its subelements, their attributes, values, and content? I'm guessing I should declare my extension function to return DocumentInfo, and construct a TinyDocumentImpl object as the actual return value. Am I on the right track? Thanks, again! -- Kevin


Replies (8)

Please register to reply

RE: how to return a node from extension funct - Added by Anonymous over 18 years ago

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

I would think it's much easier to implement such a function in XSLT or XQuery than in Java, but yes, if you want to do it in Java, that's probably the best way. You can also construct DOM, JDOM, or XOM trees and return a wrapper around the document node.

RE: how to return a node from extension funct - Added by Anonymous over 18 years ago

Legacy ID: #3366095 Legacy Poster: Kevin Rodgers (notorious_kev)

Easier is better :-) But I need to access an unknown set of data that's only available from Java. I could easily return a String from Java that looks like an XML document, if only there were a Saxon extension to the XSLT document() function that could parse a string directly (instead of requiring a URI argument). Thanks, -- Kevin

RE: how to return a node from extension funct - Added by Anonymous over 18 years ago

Legacy ID: #3366105 Legacy Poster: Kevin Rodgers (notorious_kev)

I am really confused by the TinyTree API. I expected to be able to do something like this, but the Tiny*Impl class constructors all require a TinyTree tree and an int nodNr. How do I translate this pseudo-code into working code? DocumentInfo d = new TinyDocumentImpl("document-name"); // NamespaceInfo n = new TinyNamespaceImpl("http://example.com"); // d.addNameSpace(n); for (Iterator i = magic.iterator(); i.hasNext();) { // make element node: NodeInfo e = new TinyElementImpl(i.next()); AttributeInfo a = new TinyAttributeImpl("attribute-name", "attribute-value"); e.addAttribute(a); result.addElement(e); } return d; Thanks! -- Kevin

RE: how to return a node from extension funct - Added by Anonymous over 18 years ago

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

There is such a function - saxon:parse() http://www.saxonica.com/documentation/extensions/functions/parse.html That's not to say that building the document in the Java application is necessarily the wrong thing to do; it's just a bit of effort.

RE: how to return a node from extension funct - Added by Anonymous over 18 years ago

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

Don't try and use the TinyTree interfaces directly. Create a TinyBuilder (there's a method in the Controller/Transformer to do this), and pass it Receiver events. Even that is pretty low-level. The cleanest way (least use of internal APIs) is probably to create a JAXP identity transformer as a TransformerHandler, give it the Builder as the result object, and feed it SAX events as the input.

RE: how to return a node from extension funct - Added by Anonymous over 18 years ago

Legacy ID: #3367453 Legacy Poster: Kevin Rodgers (notorious_kev)

Hmmm... Can I call saxon:parse() from Java, or must I pass the string from Java back to XSLT and call saxon:parse() in the stylesheet? I suspect the latter because there's no method in the net.sf.saxon.functions.Parse class that takes a String as an argument and returns a DocumentInfo. Thanks, -- Kevin

RE: how to return a node from extension funct - Added by Anonymous over 18 years ago

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

The simplest way to parse source XML and construct a DocumentInfo from java is to create an XPathEvaluator and call its build() method. saxon:parse is designed to do the same job from within XPath. Michael Kay

RE: how to return a node from extension funct - Added by Anonymous over 18 years ago

Legacy ID: #3367850 Legacy Poster: Kevin Rodgers (notorious_kev)

Indeed, it's apparently as simple as: StreamSource xmlSource = new StreamSource(new StringReader(xmlString)); XPathEvaluator xpe = new XPathEvaluator(xmlSource); return (DocumentInfo) xpe.evaluateSingle("/"); Thanks! -- Kevin

    (1-8/8)

    Please register to reply