Suzanne,
Are you always aware of the namespace involved? I.e. is your problem that you know about namespace "urn:xmethods-Temperature", you just don't know what local prefix it maps to?
I've had a similar problem and (I am ashamed to admit) I took a very easy path out. I simply add the namespace to the document at run-time (using DOM calls) under the document root, using my own prefix. Then I do my XPath expressions using my own prefix and all works well.
It's a *very ugly* approach as it requires modification of the source document.
(I can do this as my documents are Xerces DOM to start with.)
David - I wonder if there is a case here for expanding the default prefix resolver so that it can be "pre-seeded" with known namespaces?
In my case I have a document fragment that looks like :
<doc xmlns:ds="http://www.w3.org/the-dsig-path> <ds:Signature> ... </ds:Signature> </doc>
And my XPath expression needs to find all nodes under <ds:Signature>. The problem is I always know the URI of the namespace, but I don't know the prefix - this was arbitrarily set by the writer of the doc.
Do I "pre-seed" the document with
<doc xmlns:ds="http://www.w3.org/the-dsig-path xmlns:berin-dsig="http://www.w3.org/the-dsig-path> <ds:Signature> ... </ds:Signature> </doc>
and then use "berin-dsig" in my XPath expression.
Very very ugly - I run the risk of re-defining berin-dsig, but I really don't want to search through the document for the ds namespace as it could get redefined at any point. (And I can always do the //namespace::* trick to validate the berin-dsig is not otherwise used.)
So having a way to pre-seed the resolver would be quite useful for stand-alone XPath evaluation.
Cheers, Berin
From: David N Bertoni/Cambridge/IBM <[EMAIL PROTECTED]> To: [email protected] Subject: Re: prefix resolving Date: Mon, 17 Mar 2003 13:16:58 -0800
Hi Suzanne,
There is no out-of-the-box functionality to do this. Since there is no reason why two prefixes cannot be bound to different namespaces in the source document, there may be conflicts in the bindings. Also, the default namespace will be a problem, because it is not bound to any prefix.
However, a quick-and-dirty approach would be to use XPath itself to find all of the namespaces in the source document. The XPath expression "//namespace::*" will select all of the namespace nodes, if evaluated using any node in the source document as the context. You can then construct a PrefixResolver that simply consults the node-set when searching for prefixes, by matching the prefix to to the string returned by XalanNode::getLocalName().
You might also consider programmatically constructing and evaluating XPath expressions within your PrefixResolver. For example, if the call to PrefixResolver::getNamespaceForPrefix() passes in the prefix "foo", you construct the string "//namespace::foo", evaluate the expression and use the resulting node-set. You might also want to cache the result, in case the PrefixResolver gets called again with the same prefix. You should also detect the case where there is more than one binding for "foo" and recover accordingly.
I am considering enhancing the SimpleXPathAPI sample to do something like this for the 1.6 release.
Dave
Suzanne Dirkers
<[EMAIL PROTECTED] To: [email protected]
com> cc: (bcc: David N Bertoni/Cambridge/IBM)
Subject: prefix resolving
03/17/2003 01:02
PM
Hi all,
In an xml file like this:
<?xml version="1.0"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema"> <SOAP-ENV:Body> <m:getTemp xmlns:m="urn:xmethods-Temperature" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <zipcode xsi:type="xsd:string">94041</zipcode> </m:getTemp> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
how does one gather up all the prefixes so that they are all known when you want to evaluate an xpath expression against this file using standalone xpath ( ie, no stylesheet)? I am not an xpath expert and I've only begun to try using Xalan. I suppose for one thing I don't understand how to use the PrefixResolver class to accomplish anything useful. I don't like having to use something like XPathEvaluator.evalute() on all nodes down to the one I"m interested in and ignoring exceptions received for unresolved prefixes down to the one that might have it ( ie, the node I'm on, as in the m:getTemp node), successively. That's why I'm wondering if there is a way to just gather up all the prefixes, make them 'known' for the whole document, and then from then on the xpath expressions someone tries against this file should not be 'not found' or cause exception based on unresolved prefixes.
Thanks, Suzanne
_________________________________________________________________
MSN Instant Messenger now available on Australian mobile phones.�Go to http://ninemsn.com.au/mobilecentral/hotmail_messenger.asp
