You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by Suzanne Dirkers <di...@us.ibm.com> on 2003/03/17 22:02:34 UTC

prefix resolving




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


Re: prefix resolving

Posted by David N Bertoni/Cambridge/IBM <da...@us.ibm.com>.



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                                                                                    
                      <dirkers@us.ibm.         To:      xalan-c-users@xml.apache.org                                     
                      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