1
votes

I should have originally posted my question by stating that our code was using an embedded saxon extension function - saxon:parse($xml) which returned the root element/node of the xml. However, in Saxon-HE that extension is no longer available - so I am trying to write an Integrated extension that parses an xml string into a document and returns the root element.

I am using Saxon-HE 9.5.1.6 - I am trying to write an Integrated Extension Function that returns the ROOT Node of a Document. The function receives an xml string - creates a document and needs to return the root node to the xslt for it to then use xpath to find a specific element. The call() method of the ExtensionFunctionCall class/types return a Sequence type - how do I return a NodeSequence or NodeType? How do I construct the NodeSequence from my Document?

I can step and debug and confirm the function receives the correct xml - parses this into a document, but so far I am unable to determine how to construct the NodeSequence with my RootElement.

I have other Integrated Extension Functions that return a StringValue - and those work great, but I can't glean from the class methods available how to return anything other than simple (numerica/alpha/item) types from the ExtensionFunctionCall

Thank you.

2
Can you show us (the relevant part of) your code?LarsH
private static class SurveyParseCall extends ExtensionFunctionCall{MichaelT
private static class SurveyParseCall extends ExtensionFunctionCall{ @Override public Sequence call(XPathContext context, Sequence[] arguments)throws XPathException{ Sequence seq = arguments[0]; String xml = ((StringValue)seq.head()).getStringValue(); DocumentBuilderFactory = DocumentBuilderFactory.newInstance(); //the following goes in a try catch - omitting that here DocumentBuilder builder = factory.newDocumentBuilder; Document document = builder.parse(new InputSource(new StringReader(xml))); //here I need to return document.getDocumentElement();MichaelT
Rather than posting code in comments (which do not preserve formatting) it is better to edit the original question and add the code in there. You can always edit your own posts, and will be able to edit other people's once you have accumulated some more reputation points.Ian Roberts
The main point to note here is that if you only want to parse lexical XML to return a document tree, then using Saxon's tree implementation is up to 10 times faster than using the DOM. (The function is also available as saxon:parse() or fn:parse-xml(), but sadly not in Saxon-HE.)Michael Kay

2 Answers

4
votes

The class DocumentInfo implements Sequence, so if you return a DocumentInfo, that will satisfy the interface. You can construct a DocumentInfo using

context.getConfiguration().buildDocument()

If you want to construct your document using some external object model such as DOM or JDOM2, you will need to take the root node of that external document and wrap it in the appropriate kind of Saxon DocumentWrapper to make it into a DocumentInfo.

3
votes

For anyone reading - following this I was able to get this working with Michael Kay's help - my solution is the following:

Source source = new StreamSource(new StringReader(myXMLparam));
DocumentInfo docInfo = context.getConfiguration().buildDocument(source);
return docInfo;