[jdom-interest] Two namespace-questions (revisited)

Kai Wörner herrner at gmail.com
Thu Jun 7 06:03:33 PDT 2007


Thanks - this did the biggest part of the trick - I can now useXPath-Strings the same way I can do it in other software. The returnedelements still contain a
xmlns="http://www.tei-c.org/ns/1.0"
that they don't have in the original document, tho.
Kai


2007/6/7, Michael Kay <mike at saxonica.com>:> If you want //s to deliver nodes that are in a namespace, you need to call>> xpathE.setDefaultElementNamespace(uri);>> You can of course get the URI by reading the JDOM document.>> The list returned by evaluate() contains objects of class> net.sf.saxom.om.Item. For a path expression executed against a JDOM document> these will be instances of net.sf.saxon.jdom.NodeWrapper, from which you can> extract the JDOM node using its getUnderlyingNode() method.>> Michael Kay> http://www.saxonica.com/>>>> > -----Original Message-----> > From: Kai Wörner [mailto:herrner at gmail.com]> > Sent: 07 June 2007 10:21> > To: Michael Kay> > Subject: Re: [jdom-interest] Two namespace-questions (revisited)> >> > I got the code straight out of the Saxon-JDOM-example...> > well, now I have> >> > systemId = f.toURL().toString();> > config = new Configuration();> > docw = new DocumentWrapper(jdomDoc, systemId, config);> >> > (...)> >> > XPathEvaluator xpathE = new XPathEvaluator(config);> > xpathE.createExpression(validatedText);> > XPathExpression xpath = xpathE.createExpression(validatedText);> > List sresults = xpath.evaluate(docw);> >> > (...)> >> > but apart from the fact that this list ist empty if I try> > "//s" as an XPath ("//." delivers results) this list doesn't> > seem to hold JDom-Elements  - what does it hold and how to I> > get back to JDom from there?> >> > Kai> >> >> >> >> > 2007/6/6, Michael Kay <mike at saxonica.com>:> > > On the whole I'd recommend not using the> > > net.sf.saxon.xpath.XPathEvaluator interface - it's a rather clumsy> > > mixture of JAXP and non-JAXP methods and it's not always clear how> > > they relate. The particular thing you've tripped over is that> > > setSource() doesn't set the context node - it returns a> > NodeInfo, which you can then pass to setContextNode().> > >> > > Unless you're going to stick completely to JAXP interfaces, I'd> > > recommend using net.sf.saxon.sxpath.XPathEvaluator instead> > (spot the difference).> > >> > > Also, there's no point using the JAXP dynamic factory> > method if you've> > > got compile-time references into Saxon anyway. Just do> > >> > > Configuration config = new Configuration();> > >> > > - you don't actually need a TransformerFactory.> > >> > > Michael Kay> > > http://www.saxonica.com/> > >> > > > -----Original Message-----> > > > From: Kai Wörner [mailto:herrner at gmail.com]> > > > Sent: 06 June 2007 17:26> > > > To: Michael Kay> > > > Subject: Re: [jdom-interest] Two namespace-questions (revisited)> > > >> > > > I'd definitely like to do that, but Saxon is giving me the "the> > > > context item is undefined"-thing and I can't see where I've> > > > forgotten to declare the source - can you point me into the right> > > > direction?> > > >> > > > SAXBuilder builder = new> > > > SAXBuilder("org.apache.xerces.parsers.SAXParser");> > > > try {> > > >       systemId = f.toURL().toString();> > > >       teiDoc = builder.build(f);> > > >       System.setProperty("javax.xml.transform.TransformerFactory",> > > >               "net.sf.saxon.TransformerFactoryImpl");> > > >       TransformerFactory tfactory => > TransformerFactory.newInstance();> > > >       Configuration config = ((TransformerFactoryImpl)> > > > tfactory).getConfiguration();> > > >       docw = new DocumentWrapper(teiDoc, systemId, config);> > > > (...)> > > > try {> > > >       XPathEvaluator xpath = new XPathEvaluator(docw);> > > >       Iterator iter = xpath.evaluate(validatedText).iterator();> > > >       while (iter.hasNext()) {> > > >               results.add((Element) iter.next());> > > >       }> > > > (...)> > > >> > > > Kai> > > >> > > >> > > >> > > > 2007/6/6, Michael Kay <mike at saxonica.com>:> > > > >> > > > > If you don't like the XPath 1.0 specification, why not try> > > > XPath 2.0?> > > > >> > > > > XPath 2.0 allows you to set a default namespace for> > > > unqualified names> > > > > in path expressions, and you can use that against JDOM> > > > input if you use Saxon.> > > > >> > > > > Michael Kay> > > > > http://www.saxonica.com/> > > > >> > > > > > -----Original Message-----> > > > > > From: jdom-interest-bounces at jdom.org> > > > > > [mailto:jdom-interest-bounces at jdom.org] On Behalf Of> > Kai Wörner> > > > > > Sent: 06 June 2007 15:36> > > > > > To: jdom-interest at jdom.org> > > > > > Subject: Re: [jdom-interest] Two namespace-questions> > (revisited)> > > > > >> > > > > > Hello again,> > > > > >> > > > > > I'm stumbling over this same problem again and again! Now> > > > I'd like> > > > > > to simply show parts of the same input XML file as last> > > > time inside> > > > > > a Java program based on an XPath-expression. I simply> > provide an> > > > > > input field for the XPath-string and seeing the problems that> > > > > > the users already have drafting a XPath-expression, I simply> > > > _know_ that> > > > > > they'll fail if they have to add a namespace-declaration> > > > before each> > > > > > and every element-name they use (especially since they> > > > don't have to> > > > > > do this in any of the XML-Editors they use).> > > > > > In addition, if I use an XMLOutputter to output the matched> > > > > > Elements, they come out with another load of> > > > namespace-bulk none of> > > > > > my users will understand. So in my tool I'd have to use an> > > > > > expression like:> > > > > >> > > > > > //tei:s> > > > > >> > > > > > to get a result that looks like that:> > > > > >> > > > > > <s xmlns="http://www.tei-c.org/ns/1.0" part="N" TEIform="s">> > > > > >                         <cl type="lMAI tDEC" part="N"> > > > TEIform="cl">> > > > > >> > > > > > as opposed to any XML-Editor where I'd use:> > > > > >> > > > > > //s> > > > > >> > > > > > to get:> > > > > >> > > > > >                <s>> > > > > >                     <cl type="lMAI tDEC">> > > > > >> > > > > > Is it possible /at all/ to mimic the way it works in a> > > > XML-Editor at> > > > > > all in JDom or do I have to look someplace else?> > > > > >> > > > > > Thanks again,> > > > > >> > > > > > Kai> > > > > >> > > > > >> > > > > > XML: http://files.exmaralda.org/bl.xml> > > > > >> > > > > >> > > > > >> > > > > >> > > > > >> > > > > > > There is no default namespace in XPath 1.0.> > > > > > > So you have to use a namespace prefix in your XPath> > > > > > expressions. You> > > > > > > can choose any prefix as there is no relationship between> > > > > > the prefixes> > > > > > > used in XPath and the ones in the document:> > prefixes are just> > > > > > > shortcuts and the matching is done on the actual> > namespace URIs.> > > > > > >> > > > > > > The following should work:> > > > > > > XPath xp = XPath.newInstance("/x:*"); xp.addNamespace("x",> > > > > > > "http://www.tei-c.org/ns/1.0"); xp.selectNodes(document);> > > > > > >> > > > > > _______________________________________________> > > > > > To control your jdom-interest membership:> > > > > > http://www.jdom.org/mailman/options/jdom-interest/youraddr@you> > > > > > rhost.com> > > > >> > > > >> > > >> > >> > >> >>>


More information about the jdom-interest mailing list