Thank you very much! The code is exactly what I needed. Thanks again, Scott
-----Original Message----- From: Paul Brown [mailto:[EMAIL PROTECTED]] Sent: Monday, December 03, 2001 11:10 AM To: [EMAIL PROTECTED] Subject: RE: XPathAPI help needed > [Scott.Moore at netDecide.com] > Subject: XPathAPI help needed > Everything works fine unless the XML has namespaces and I try to > reference them in my XPath expression. You have two choices: 1) You need to create a dummy prefix resolver that knows about your namespaces; you can do this by implementing the relevant interface (org.apache.xml.utils.PrefixResolver). 2) You can use a PrefixResolverDefault (org.apache.xml.utils.PrefixResolverDefault) constructed for the context node in your expression. (It only has one constructor.) Choice #1 is much better than choice #2 because choice #2 means that you're dependent on the prefixes declared in the document. Choice #1 means that you're dependent on the URIs. Tell you what, I'll contribute some code... It follows: import java.util.HashMap; import java.util.Map; import org.w3c.dom.Node; import org.apache.xml.utils.PrefixResolver; /** * This class implements the org.apache.xml.utils.PrefixResolver * interface to support execution of XPath expressions without * dependence on document-delcared namespace prefixes. */ public class StaticPrefixResolverImpl implements PrefixResolver { /** * Runtime storage for prefixes and URIs. */ private HashMap _prefixes; private static final String EMPTY_STRING = ""; /** * The URI for the "xml" namespace. */ public static final String S_XMLNAMESPACEURI = "http://www.w3.org/XML/1998/namespace"; public static final String XML_PREFIX = "xml"; /** * The URI for namespace declarations. */ public static final String S_XMLNSNAMESPACEURI = "http://www.w3.org/2000/xmlns/"; public static final String XMLNS_PREFIX = "xmlns"; /** * Create a new StaticPrefixResolverImpl using the supplied map of prefixes * and URIs. The constructor creates a copy of the supplied map. * * @param prefixMap a map of the prefixes and URIs to be used by the instance. */ public StaticPrefixResolverImpl(Map prefixMap) { if (prefixMap != null) { _prefixes = new HashMap(prefixMap); } else { _prefixes = new HashMap(); } _prefixes.put(XML_PREFIX,S_XMLNAMESPACEURI); _prefixes.put(XMLNS_PREFIX,S_XMLNSNAMESPACEURI); } /** * Return the base identifier for the document. */ public String getBaseIdentifier() { return (String) _prefixes.get(EMPTY_STRING); } /** * Retrieve the URI corresponding to the supplied prefix. * * @param prefix the prefix to match to a URI. */ public String getNamespaceForPrefix(String prefix) { return (String) _prefixes.get(prefix); } /** * Retrieve a namespace in context. Note that this is meaningless for our * implementation, as the prefixes are static for the entire document and * independent of any local declarations. * * @param prefix the prefix to query * @param n the context Node (ignored) */ public String getNamespaceForPrefix(String prefix, Node n) { return getNamespaceForPrefix(prefix); } } -- Paul
