You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by "McNeill, Keith D." <mc...@iconverse.com> on 2001/02/15 23:09:02 UTC

XPathAPI & a document with a default namespace

I have several documents (not created by myself) that have just default
namespaces....for example:
 
<adoc xmlns="http://adoc.com">
    <parent>
        <child/>
    </parent>
</adoc>
 
Is there anyway that I can tell the XPathAPI to use a namespace prefix
(let's say I pick xmlns:foo=" http://adoc.com <http://adoc.com> ") for
the namespace so that I can use the XPathAPI to search the doc.  For
example:  /foo:adoc/foo:parent etc....
 
The only way that we've thought of to get around this is to re-parent
the above document with another document that declares the namespace
prefix...for example create a document like:
 
    <mother xmlns:foo=" http://adoc.com <http://adoc.com/> ">
        <adoc xmlns="http://adoc.com">
            <parent>
               <child/>
            </parent>
        </adoc>
    </mother>
 
There seems like there must be a better way...
 
Thanks,
 
Keith

Re: XPathAPI & a document with a default namespace

Posted by Gary L Peskin <ga...@firstech.com>.
"McNeill, Keith D." wrote:
> 
> I have several documents (not created by myself) that have just default
> namespaces....for example:
> 
> <adoc xmlns="http://adoc.com">
>     <parent>
>         <child/>
>     </parent>
> </adoc>
> 
> Is there anyway that I can tell the XPathAPI to use a namespace prefix
> (let's say I pick xmlns:foo=" http://adoc.com <http://adoc.com> ") for
> the namespace so that I can use the XPathAPI to search the doc.  For
> example:  /foo:adoc/foo:parent etc....
> 
> The only way that we've thought of to get around this is to re-parent
> the above document with another document that declares the namespace
> prefix...for example create a document like:
> 
>     <mother xmlns:foo=" http://adoc.com <http://adoc.com/> ">
>         <adoc xmlns="http://adoc.com">
>             <parent>
>                <child/>
>             </parent>
>         </adoc>
>     </mother>
> 
> There seems like there must be a better way...

There is.  How are you using XPathAPI?  If you're using the three
argument flavors of the select... methods, you specify a namespaceNode
as the third argument.  This node is used to resolve namespace
references in your XPath argument, which is the second argument. 
Remember, it's the namespace URI that matters, not the actual prefix. 
So, just pick a prefix, use it in your XPath expression, and make sure
that the node specified as the third argument to your select... method
declares that prefix as belonging to that namespace URI.

HTH,
Gary