You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Dmitri Plotnikov <dm...@apache.org> on 2004/12/30 23:09:40 UTC

Re: JXPath and namespaces

Edwin,

I hope you don't mind my posting this exchange on the mailing lists, as it 
could be important to a lot of JXPath users.

You are making a very good point!

At this point I cannot change API that has already been released.  However 
the issue is real and needed to be addressed, so what I have done instead is 
allow for the namespace mappings to be inheritable via child contexts, just 
like extension functions, locale etc.

So, the way to reuse mappings will be as follows:

1. You create a reusable "parent" context (which BTW does not even have to 
have a context node).

JXPathContext pmap = JXPathContext.newContext(null);

2. Register namespaces:

pmap.registerNamespace("foo", "http://foomatic.bar/foo")

3. Use this context as a parent every time you create a new JXPathContext:

JXPathContext context = JXPathContext.newContext(pmap, xmlDocument)

4. Use the mapped prefixes in your xpaths:

String baz = (String) context.getValue("foo:bar/foo:baz");

In order to take advantage of this new functionality, you will need to use 
the HEAD branch from CVS (or the nightly build starting tomorrow).

I hope this helps,

Thank you for the insightful suggestion,

Happy New Year,

- Dmitri

----- Original Message ----- 
From: "Edwin de Jong" <ed...@emaxx.nl>
To: <dm...@apache.org>
Sent: Wednesday, December 22, 2004 11:20 AM
Subject: JXPath and namespaces


> Hi Dmitri,
>
> Recently we have begun to incorporate the new version of JXPath 1.2 into 
> our
> current products. One of the (in my eyes bennevicient) changes is related 
> to
> the stricter use of namespaces.
>
> Nevertheless, I feel a design flaw has been made in implementing them. 
> Right
> now the prime location of changing namespaces is in the instance of the
> JXPathContext class. However, these namespace changes are applied to the
> supplied JXPaths. So instead of applying a namespace to a context, it 
> should
> be applied to the search string (or have additional methods which allow an
> instance of a class that maps from prefixes to URI's). This way, binding 
> can
> be seperated from context and search criteria.
>
> Example code snippet:
>
> --- cut here ->8---------------------------
>
> JXPrefixMap pmap = JXPrefixMap.getInstance();
> pmap.add("foo", "http://foomatic.bar/foo");
> // or JXPrefixMap pmap = xmlContainerInstance.getPrefixMap();
>
> JXPathContext context = JXPathContext.newContext(xmlDocument);
>
> // either:
> String baz = (String) context.getValue("foo:bar/foo:baz", pmap);
>
> // or:
> String baz = (String) context.getValue(new 
> JXPathExpression("foo:bar/foo:baz",
> pmap));
>
> System.out.println("Hello world: "+baz+"!");
>
> --- cut here ->8---------------------------
>
> Hope this makes some sense. It will make refactoring our code much easier
> since we don't always use JXPathContext's as our leading datasource
> (sometimes the context changes, but the query stays the same).
>
> Regards,
>
> Edwin de Jong
> -- 
> Edwin de Jong
>
>
> eMAXX B.V.
> Kopersteden 22-4
> P.O. Box 157
> 7500 AD Enschede
> The Netherlands
> tel. +31 53 484 83 21
> fax. +31 53 484 83 23
>
> http://www.emaxx.nl
>
> ****************************************************************************
> This email and any files transmitted with it are confidential and
> intended solely for the use of the individual or entity to whom they
> are addressed. If you have received this email in error please notify
> the system manager.
> www.emaxx.nl (hostmaster@emaxx.nl)
> ****************************************************************************



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org