You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-users@xml.apache.org by Richard Martell <rm...@galdosinc.com> on 2002/02/08 22:59:11 UTC

XPath queries: nodes in default namespace not returned

Hi,


I'm not having much success executing XPath queries against documents with
a default namespace declared (using the beta4 release). According to the
XML:DB spec, if the prefix input to the setNamespace() method is null or
empty, the default namespace is set.

e.g.
. . .
  XPathQueryService service =
             (XPathQueryService) col.getService("XPathQueryService", "1.0");
  service.setNamespace( null, defaultNamespaceURI );
. . .


But the query comes up empty having set the namespaces as follows (see
examples 1 and 2 below):

service.setNamespace( "", http://www.w3.org/2001/XMLSchema );
service.setNamespace( "xsd", "http://www.w3.org/2001/XMLSchema" );


Example 1--this works (returns the matching type definition)
============================================================
String xpath = "/xsd:schema/xsd:complexType[@name='FooType']";

where this schema is in the target collection:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.foo.net/bar"
   xmlns:bar="http://www.foo.net/bar"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   elementFormDefault="qualified">

   <xsd:element name="Foo" type="bar:FooType" />
   <xsd:complexType name="FooType">
     <xsd:sequence>
        <xsd:element name="alpha" type="xsd:string" />
     </xsd:sequence>
     <xsd:attribute name="id" type="xsd:ID" use="optional"/>
   </xsd:complexType>
</xsd:schema>


Example 2--this doesn't work (empty result set)
===============================================
String xpath = "/schema/complexType[@name='FooType']";

where this equivalent schema is also in the target collection:

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.foo.net/bar"
   xmlns:bar="http://www.foo.net/bar"
   xmlns="http://www.w3.org/2001/XMLSchema"
   elementFormDefault="qualified">

   <element name="Foo" type="bar:FooType" />
   <complexType name="FooType">
     <sequence>
        <element name="alpha" type="string" />
     </sequence>
     <attribute name="id" type="ID" use="optional"/>
   </complexType>
</schema>


Any suggestions, anyone?


- richard



RE: XPath queries: nodes in default namespace not returned

Posted by Richard Martell <rm...@galdosinc.com>.
Tim,


Thanks for your response. Unfortunately, the work-around you
suggest doesn't seem to work for me. I had populated the target
collection with multiple schemas, some declaring a default
namespace and some mapping it to a prefix. The only nodes returned
were from those schemas that bound the "http://www.w3.org/2001/XMLSchema"
namespace to a prefix.

The unbound XPath expression (/schema/complexType[@name='FooType'])
never yielded any results, despite the namespace mappings set for
the query service. The bound expression (/xsd:schema/xsd:complexType[
@name='FooType']) never retrieved nodes from schemas employing a
default namespace assignment.


-richard




At 04:11 PM 2/8/2002 -0600, you wrote:
>I tried to track this bug down a while ago: It appeared to me at the
>time that there was a problem within the Xalan implementation which is
>used for XPath evaluation. When I stepped through the SAX events that
>occur when parsing the query string, it did not appear that the prefix
>resolver was being invoked when the query path had an unqualified (i.e.
>default) element or attribute. I was never able to find the time to dig
>any deeper.
>
>The good news is that you can work around the problem pretty easily.
>Even if your schema document is stored using a default namespace (as
>shown in your second example), you can perform the query using a
>non-default query. So, you should be able to store your document as in
>Example 2. When you want to perform your query, you can use the query
>syntax from your first example. As long as you've set the namespace on
>your service as you've specified:
>
>         service.setNamespace( "xsd", "http://www.w3.org/2001/XMLSchema"
>);
>
>Then everything should work with the query
>"/xsd:schema/xsd:complexType[@name='FooType']". It does not matter if
>the "xsd" prefix is not used in the document that is stored.
>
>I still plan to look into this bug if I get a chance, but my schedule's
>been pretty tight lately.
>
>Hope that helps!
>
>- Tim


RE: XPath queries: nodes in default namespace not returned

Posted by "Timothy M. Dean" <td...@visi.com>.
I tried to track this bug down a while ago: It appeared to me at the
time that there was a problem within the Xalan implementation which is
used for XPath evaluation. When I stepped through the SAX events that
occur when parsing the query string, it did not appear that the prefix
resolver was being invoked when the query path had an unqualified (i.e.
default) element or attribute. I was never able to find the time to dig
any deeper.

The good news is that you can work around the problem pretty easily.
Even if your schema document is stored using a default namespace (as
shown in your second example), you can perform the query using a
non-default query. So, you should be able to store your document as in
Example 2. When you want to perform your query, you can use the query
syntax from your first example. As long as you've set the namespace on
your service as you've specified:

	service.setNamespace( "xsd", "http://www.w3.org/2001/XMLSchema"
);

Then everything should work with the query
"/xsd:schema/xsd:complexType[@name='FooType']". It does not matter if
the "xsd" prefix is not used in the document that is stored.

I still plan to look into this bug if I get a chance, but my schedule's
been pretty tight lately.

Hope that helps!

- Tim


> -----Original Message-----
> From: Richard Martell [mailto:rmartell@galdosinc.com] 
> Sent: Friday, February 08, 2002 3:59 PM
> To: xindice-users@xml.apache.org
> Subject: XPath queries: nodes in default namespace not returned
> 
> 
> Hi,
> 
> 
> I'm not having much success executing XPath queries against 
> documents with a default namespace declared (using the beta4 
> release). According to the XML:DB spec, if the prefix input 
> to the setNamespace() method is null or empty, the default 
> namespace is set.
> 
> e.g.
> . . .
>   XPathQueryService service =
>              (XPathQueryService) 
> col.getService("XPathQueryService", "1.0");
>   service.setNamespace( null, defaultNamespaceURI );
> . . .
> 
> 
> But the query comes up empty having set the namespaces as 
> follows (see examples 1 and 2 below):
> 
> service.setNamespace( "", http://www.w3.org/2001/XMLSchema ); 
> service.setNamespace( "xsd", "http://www.w3.org/2001/XMLSchema" );
> 
> 
> Example 1--this works (returns the matching type definition) 
> ============================================================
> String xpath = "/xsd:schema/xsd:complexType[@name='FooType']";
> 
> where this schema is in the target collection:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema targetNamespace="http://www.foo.net/bar"
>    xmlns:bar="http://www.foo.net/bar"
>    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>    elementFormDefault="qualified">
> 
>    <xsd:element name="Foo" type="bar:FooType" />
>    <xsd:complexType name="FooType">
>      <xsd:sequence>
>         <xsd:element name="alpha" type="xsd:string" />
>      </xsd:sequence>
>      <xsd:attribute name="id" type="xsd:ID" use="optional"/>
>    </xsd:complexType>
> </xsd:schema>
> 
> 
> Example 2--this doesn't work (empty result set) 
> ===============================================
> String xpath = "/schema/complexType[@name='FooType']";
> 
> where this equivalent schema is also in the target collection:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <schema targetNamespace="http://www.foo.net/bar"
>    xmlns:bar="http://www.foo.net/bar"
>    xmlns="http://www.w3.org/2001/XMLSchema"
>    elementFormDefault="qualified">
> 
>    <element name="Foo" type="bar:FooType" />
>    <complexType name="FooType">
>      <sequence>
>         <element name="alpha" type="string" />
>      </sequence>
>      <attribute name="id" type="ID" use="optional"/>
>    </complexType>
> </schema>
> 
> 
> Any suggestions, anyone?
> 
> 
> - richard
> 
> 
>