To get a Saxon NodeInfo that wraps a JDOM element you first have to create a DocumentWrapper for the JDOM Document, and then you can use net.sf.saxon.jdom.DocumentWrapper.wrapNode() to wrap the element node. It's best to reuse the same DocumentWrapper if you are wrapping different elements in the same document, otherwise they will be regarded as coming from different documents.
 
There shouldn't be any problems with the namespace context of wrapped nodes.
 
When you refer to "anonymous namespaces" I guess you mean the handling of unprefixed names in path expressions? In XSLT 2.0 you can control this using the xpath-default-namespace attribute. In S9API you can do it as part of the XPathCompiler (which defines the static context for XPath expressions):
 
XPathCompiler xpc;
xpc.declareNamespace("", "http://myDefaultNamespace.uri/");
 
Michael Kay
http://www.saxonica.com/


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of martin.me.roberts@bt.com
Sent: 04 July 2008 13:05
To: saxon-help@lists.sourceforge.net
Subject: Re: [saxon] S9 API and JDOM

Michael,
  Thanks for your humble response.  There is a add on question - can you pass in Elements from a JDOM document in a similar way?  Quite a lot of what I do is based upon passing an Element rather than Documents?  Also linked to this is do you have to explicitly define the Namespace context or does the API recognise the context from the document passed in?
 
Further to this is the issue of anonymous namespaces.  In XSLT you can not have anonymous namespaces and also the same applies to JAXEN Xpath- with s9 do I need to allocate a namespace prefix to any anonymous namespaces.
 
Martin


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Michael Kay
Sent: 04 July 2008 12:21
To: 'Mailing list for the SAXON XSLT and XQuery processor'
Subject: Re: [saxon] S9 API and JDOM

Slightly embarrassing this one: it was supposed to work using DocumentBuilder.wrap(Object), but that's not working in the JDOM case in Saxon 9.1. Also, there's no convenient way to unwrap the XdmNode that's return (which is a wrapper around a NodeInfo which is a wrapper around a JDOM Element) to get the JDOM element. So I had to do it rather clumsily as follows (you can plug this into the S9APIExamples.java program):
 
    /**
      * Demonstrate use of an XPath expression against a JDOM source document. For this
      * sample, saxon9-jdom.jar must be on the classpath
      */
 
     private static class XPathJDOM implements S9APIExamples.Test {
         public String name() {
             return "XPathJDOM";
         }
         public boolean isSchemaAware() {
             return false;
         }
         public void run() throws SaxonApiException {
             // Build the JDOM document
             org.jdom.input.SAXBuilder jdomBuilder = new SAXBuilder();
             File file = new File("data/books.xml");
             org.jdom.Document doc;
             try {
                 doc = jdomBuilder.build(file);
             } catch (JDOMException e) {
                 throw new SaxonApiException(e);
             } catch (IOException e) {
                 throw new SaxonApiException(e);
             }
             Processor proc = new Processor(false);
             DocumentBuilder db = proc.newDocumentBuilder();
             //XdmNode xdmDoc = db.wrap(doc);
             DocumentInfo saxonDoc = new net.sf.saxon.jdom.DocumentWrapper(
                     doc, file.toURI().toString(), proc.getUnderlyingConfiguration());
             XdmNode xdmDoc = db.wrap(saxonDoc);
             XPathCompiler xpath = proc.newXPathCompiler();
             XPathExecutable xx = xpath.compile("//ITEM");
             XPathSelector selector = xx.load();
             selector.setContextItem(xdmDoc);
             for(XdmItem item : selector) {
                 NodeInfo saxonNode = ((XdmNode)item).getUnderlyingNode();
                 org.jdom.Element element = (org.jdom.Element)((VirtualNode)saxonNode).getUnderlyingNode();
                 System.out.println(element.toString());
             }
         }
 
     }
I'll treat this as a bug.
 
Michael Kay
http://www.saxonica.com/


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of martin.me.roberts@bt.com
Sent: 04 July 2008 11:42
To: saxon-help@lists.sourceforge.net
Subject: [saxon] S9 API and JDOM

Michael,
  Do you have any examples of using the S9 API Xpath part against a JDOM document?
 
Martin