[xsl] Re:XPathAPI: How to associate namespaces

Subject: [xsl] Re:XPathAPI: How to associate namespaces
From: Joseph Kesselman <keshlam@xxxxxxxxxx>
Date: Tue, 6 May 2003 17:14:05 -0400
(Apologies if this is a repost; I've been having some trouble convincing 
my mail system not to send HTML.)


1) XPath 1.0's syntax doesn't support default namespaces. To indicate a 
namespaced node, you *must* use a prefix in your XPath. A name without a 
prefix is taken as meaning you wand to find nodes which are in _no_ 
namespace. (There is a proposal to fix this in XPath 2.0.)

2) You also have to provide a binding from those prefixes to the namespace 
URIs they represent. This is not standardized in XPath 1.0. (There is a 
proposal to fix this in XPath 2.0.) Xalan's XPathAPI class offers three 
options, via different versions of the function calls:

a) If you just pass in context node and the XPath, we will attempt to 
resolve prefixes in terms of namespaces known at the context node. As 
noted above, this does not include the default namespace; you'd need to 
have an explicit prefix bound to that same URI. (This can be considered a 
shorthand for option b.)

b) If you pass in context node, XPath, and a "namespace context node", we 
will attempt to resolve prefixes in terms of namespaces known at the 
namespace context node. Same issues as above.

c) You can write a class implementing the PrefixResolver interface, and 
pass it into XPathAPI in place of the namespace context node. In fact, the 
above two are implemented using an internal implementation of that 
interface. Again, this doesn't free you from needing to use a prefix in 
your XPath, but it does give you more flexibility in deciding how to 
handle it. You could base your solution on ours, or just look in a static 
table of prefix-to-URI bindings, or do whatever else seems appropriate.

Note that this lookup should occur only *once*. If your default prefix 
changes five times along the course of your XPath, you may in fact need 
five separate prefixes to express that.


See http://xml.apache.org/xalan-j/apidocs/org/apache/xpath/XPathAPI.html 
for details of how to use these Xalan features.

______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more. 
"may'ron DaroQbe'chugh vaj bIrIQbej"  ("Put down the squeezebox and nobody 
gets hurt.")


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread