jdom and xqj api
Added by Anonymous almost 16 years ago
Legacy ID: #6378919 Legacy Poster: Shane Lee (shavo25)
Hi, I parse my xml as a jdom object model, and am currently using xpath from JAXP and jaxen to traverse the tree but i have more complex expressions to query the tree. I want to use saxon to query the tree and use FLOWR expressions and bring the result sequence back as JDOM elements. So in a nutshell what i want to do is wrap the JDOM tree using DocumentWrapper, and execute the query, the restult sequence should bring back an element, this element i want returned to be of type org.jdom.Element. I see there is the legacy XQuery API and XQJ API, i want to use the latter. example i have to start off with is: org.jdom.Document doc = null; JDOMParser parser = new JDOMParser(); doc = parser.parseXMLDocument(); XQDataSource ds = new SaxonXQDataSource(); XQConnection conn = ds.getConnection(); XQExpression exp = conn.createExpression(); query is: String queryString = "declare variable $docName as xs:string external;" + sep + " for $cd in doc($docName)/CATALOG/CD " + " where $cd/YEAR > 1980 " + " and $cd/COUNTRY = 'USA' " + " order by $cd/YEAR " + " return $cd"; Im getting really confused how i can set this up, should i be binding the xml file as a string to the expression or how should i pass in the JDOM document in the above expression if i wrapped it? Your help is greatly appreciated. Regards, Shane.
Replies (6)
Please register to reply
RE: jdom and xqj api - Added by Anonymous almost 16 years ago
Legacy ID: #6380421 Legacy Poster: Michael Kay (mhkay)
XQJ doesn't provide any facilities for intercepting the URI passed to the doc() function, so if you want to write your query using doc() you will have to use Saxon facilities for this. You can do something like this: SaxonXQDataSource ds = new SaxonXQDataSource(); ds.getConfiguration().setURIResolver(new URIResolver() { public Source resolve(String href, String base) { if (href.equals("inputDoc.jdom")) { return new DocumentWrapper(myJdomDocument) } else { return null; } } };) and then pass the string "inputDoc.jdom" to your query as the value of the variable $docName. However, if you are prepared to change your query so that instead of $docName you use: declare variable $doc as document-node() external; for $cd in $doc/CATALOG/CD .... then you can pass the DocumentWrapper in directly as the value of the parameter: exp.bindDocument(new QName("doc"), new DocumentWrapper(myJdomDocument), null); This works because DocumentWrapper implements NodeInfo which in turn implements Source. Michael Kay http://www.saxonica.com/
RE: jdom and xqj api - Added by Anonymous almost 16 years ago
Legacy ID: #6386603 Legacy Poster: Shane Lee (shavo25)
thanks for your reply michael. If i did my query the second way, as in bind the document to the external variable doc, when i query my expression, how can i cast the XQResultSequence as an org.jdom.Element if it is applicable. For example: return $cd/title i want to return an element or for: return $cd/title/text() i want to return a string from the result sequence. Regards, Shane.
RE: jdom and xqj api - Added by Anonymous almost 16 years ago
Legacy ID: #6386742 Legacy Poster: Michael Kay (mhkay)
Good question - XQJ isn't very good at this kind of thing. Your query will return an XQResultSequence, and you can call getItem() on this to get each item in the result sequence as an XQItem. I was hoping you could call getObject() on the resulting XQItem, but this doesn't seem to work. At present the only thing I can think of is to subclass StandardObjectConverter, override its toObject() method, and register your subclass using ((SaxonXQDataFactory)dataSource).setObjectConverter(). Perhaps you're better off using s9api? Michael Kay
RE: jdom and xqj api - Added by Anonymous almost 16 years ago
Legacy ID: #6387069 Legacy Poster: Shane Lee (shavo25)
thanks for your reply michael. If i did my query the second way, as in bind the document to the external variable doc, when i query my expression, how can i cast the XQResultSequence as an org.jdom.Element if it is applicable. For example: return $cd/title i want to return an element or for: return $cd/title/text() i want to return a string from the result sequence. Regards, Shane.
RE: jdom and xqj api - Added by Anonymous almost 16 years ago
Legacy ID: #6387093 Legacy Poster: Shane Lee (shavo25)
what would the code be like michael if i was to use this library instead? There is no compatability issues using s9api with jdk1.5 and been deployed as a jar on WAS 6.1 is there?
RE: jdom and xqj api - Added by Anonymous almost 16 years ago
Legacy ID: #6399148 Legacy Poster: Michael Kay (mhkay)
I missed a trick here in my previous response. The Saxon implementation of XQItem is SaxonXQItem, and this has a method getSaxonItem(), which returns an object which in this case should be the net.sf.saxon.jdom.NodeWrapper that wraps the original JDOM node.
Please register to reply