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 Michael Hughes <mi...@gf-x.com> on 2003/06/03 19:09:27 UTC

Namespace support

I want to configure support for namespaces when querying nodes from a
XalanDocument (this isn't strictly correct, but you should get the
idea):-


SelectSingleNode( const std::string & xPathExpression, XalanNode *
Context ) 
{
    TheEvaluator->selectSingleNode( *TheDOMSupport,
                                    Context,
                                    XalanDOMString(
xPathExpression.c_str() ).c_str() ) ;
  
}

Problem: This call causes a core dump whenever I request something like
this:-

SelectSingleNode ( "prefix:ElementName/prefix:SubElement/text()" ,
CurrentContext ) ;


How are you meant to configure support for namespaces in Xpath queries?

I had a look at the example applications but didn't find anything simple
that helped. 

As a side point, how can the documentation be improved, I find it
somewhat lacking in detail ;-)


Re: Namespace support

Posted by Berin Lautenbach <be...@ozemail.com.au>.
Michael,

The easiest thing to do may be to map another prefix to the default 
namespace and then do the XPath expression with that prefix.

so :

<documentElement  xmlns="http://www.michael_namespace.com/schema/"
		  xmlns:wmnc="http://www.michael_namespace.com/schema/"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.michael_namespace.com/schema/
michaelSchema.xsd">

    <GreatGrandParent>
       <GrandParent name="FFA">
          <Parent name="default">
             <Child xsi:type="Child_A_Type" location="xyz" />
         ...
    </GreatGrandParent>

The use "wmnc" as the prefix when searching for nodes in the default 
namespace.  (E.g. "//wmnc:GreatGrandParent")

You could also use the derivative of PrefixResolver to define wmnc.

Cheers,
	Berin

Michael Hughes wrote:
> Thanks David, I tried that and it worked ok.
> 
> Next problem :-)
> 
> My document looks something like this:-
> 
> <documentElement  xmlns="http://www.michael_namespace.com/schema/"
>                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>  
> xsi:schemaLocation="http://www.michael_namespace.com/schema/
> michaelSchema.xsd">
> 
>    <GreatGrandParent>  
>       <GrandParent name="FFA">
>          <Parent name="default">
>             <Child xsi:type="Child_A_Type" location="xyz" />      
>         ...
>    </GreatGrandParent>
> 
> Now I can query using 'xsi' prefix for the relevant attribute and that
> works fine. My next problem is that I can't seem to access the stuff in
> the default namespace.
> 
> Any help much appreciated.
> 
> Thanks in advance.
> 
> 
> -----Original Message-----
> From: david_n_bertoni@us.ibm.com [mailto:david_n_bertoni@us.ibm.com] 
> Sent: 03 June 2003 18:54
> To: xalan-c-users@xml.apache.org
> Subject: Re: Namespace support
> 
> 
> 
> 
> 
> 
> 
>>I want to configure support for namespaces when querying nodes from a 
>>XalanDocument (this isn't strictly correct, but you should get the
>>idea):-
>>
>>
>>SelectSingleNode( const std::string & xPathExpression, XalanNode * 
>>Context ) {
>>    TheEvaluator->selectSingleNode( *TheDOMSupport,
>>                                    Context,
>>                                    XalanDOMString(
>>xPathExpression.c_str() ).c_str() ) ;
>>
>>}
>>
>>Problem: This call causes a core dump whenever I request something 
>>like
>>this:-
>>
>>SelectSingleNode ( "prefix:ElementName/prefix:SubElement/text()" , 
>>CurrentContext ) ;
> 
> 
> I suspect you're getting a core dump because an exception is being
> thrown which you're not catching.  The documentation for XPathEvaluator
> looks like
> this:
> 
>        * @param domSupport An instance of the corresponding
> DOMSupport-derived for the DOM implementation being used.
>        * @param contextNode The source tree context node
>        * @param xpathString The XPath expression to evaluate
>        * @param namespaceNode A node to use for namespace prefix
> resolution.
> 
> So you can see the last parameter, which you're omitting, can be an
> instance of a XalanElement, which the XPathEvaluator instance will use
> to resolve prefixes.  If the namespace declaration for "prefix" occurs
> on the document element, you can do the following:
> 
>      TheEvaluator->selectSingleNode( *TheDOMSupport,
>                                      Context,
>  
> XalanDOMString(xPathExpression.c_str()
> ).c_str(),
> 
> Context->getOwnerDocument()->getDocumentElement() ) ;
> 
> Otherwise, you can create your own derivative of PrefixResolver and
> return whatever string you want to bind to "prefix."
> 
> If you download the lastest CVS code, you'll find a new class called
> XalanDocumentPrefixResolver, which searches the entire document for
> namespace declarations.  This won't work in the case where "prefix" is
> bound to multiple namespace URIs, but it does work for the simpler ones.
> If you want to use prefixes which are not defined in the instance
> document, you'll need your own derivate of PrefixResolver.
> 
> 
>>As a side point, how can the documentation be improved, I find it 
>>somewhat lacking in detail ;-)
> 
> 
> It can be improved by people volunteering to work on it.  ;-)
> 
> Dave
> 
> 


RE: Namespace support

Posted by da...@us.ibm.com.



Hi Michael,

Your question about the default namespace is not Xalan-specific.  See the
FAQ:

   http://xml.apache.org/xalan-j/faq.html#faq-N101DC

This is also detailed in Dave Pawson's XSL FAQ:

   http://www.dpawson.co.uk/xsl/xslfaq.html

Dave



                                                                                                                                          
                      "Michael Hughes"                                                                                                    
                      <michael.hughes@         To:      <xa...@xml.apache.org>                                                    
                      gf-x.com>                cc:      (bcc: David N Bertoni/Cambridge/IBM)                                              
                                               Subject: RE: Namespace support                                                             
                      06/04/2003 04:46                                                                                                    
                      AM                                                                                                                  
                                                                                                                                          



Thanks David, I tried that and it worked ok.

Next problem :-)

My document looks something like this:-

<documentElement  xmlns="http://www.michael_namespace.com/schema/"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.michael_namespace.com/schema/
michaelSchema.xsd">

   <GreatGrandParent>
      <GrandParent name="FFA">
         <Parent name="default">
            <Child xsi:type="Child_A_Type" location="xyz" />
        ...
   </GreatGrandParent>

Now I can query using 'xsi' prefix for the relevant attribute and that
works fine. My next problem is that I can't seem to access the stuff in
the default namespace.

Any help much appreciated.

Thanks in advance.


-----Original Message-----
From: david_n_bertoni@us.ibm.com [mailto:david_n_bertoni@us.ibm.com]
Sent: 03 June 2003 18:54
To: xalan-c-users@xml.apache.org
Subject: Re: Namespace support






> I want to configure support for namespaces when querying nodes from a
> XalanDocument (this isn't strictly correct, but you should get the
> idea):-
>
>
> SelectSingleNode( const std::string & xPathExpression, XalanNode *
> Context ) {
>     TheEvaluator->selectSingleNode( *TheDOMSupport,
>                                     Context,
>                                     XalanDOMString(
> xPathExpression.c_str() ).c_str() ) ;
>
> }
>
> Problem: This call causes a core dump whenever I request something
> like
> this:-
>
> SelectSingleNode ( "prefix:ElementName/prefix:SubElement/text()" ,
> CurrentContext ) ;

I suspect you're getting a core dump because an exception is being
thrown which you're not catching.  The documentation for XPathEvaluator
looks like
this:

       * @param domSupport An instance of the corresponding
DOMSupport-derived for the DOM implementation being used.
       * @param contextNode The source tree context node
       * @param xpathString The XPath expression to evaluate
       * @param namespaceNode A node to use for namespace prefix
resolution.

So you can see the last parameter, which you're omitting, can be an
instance of a XalanElement, which the XPathEvaluator instance will use
to resolve prefixes.  If the namespace declaration for "prefix" occurs
on the document element, you can do the following:

     TheEvaluator->selectSingleNode( *TheDOMSupport,
                                     Context,

XalanDOMString(xPathExpression.c_str()
).c_str(),

Context->getOwnerDocument()->getDocumentElement() ) ;

Otherwise, you can create your own derivative of PrefixResolver and
return whatever string you want to bind to "prefix."

If you download the lastest CVS code, you'll find a new class called
XalanDocumentPrefixResolver, which searches the entire document for
namespace declarations.  This won't work in the case where "prefix" is
bound to multiple namespace URIs, but it does work for the simpler ones.
If you want to use prefixes which are not defined in the instance
document, you'll need your own derivate of PrefixResolver.

> As a side point, how can the documentation be improved, I find it
> somewhat lacking in detail ;-)

It can be improved by people volunteering to work on it.  ;-)

Dave




RE: Namespace support

Posted by Michael Hughes <mi...@gf-x.com>.
Thanks David, I tried that and it worked ok.

Next problem :-)

My document looks something like this:-

<documentElement  xmlns="http://www.michael_namespace.com/schema/"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
xsi:schemaLocation="http://www.michael_namespace.com/schema/
michaelSchema.xsd">

   <GreatGrandParent>  
      <GrandParent name="FFA">
         <Parent name="default">
            <Child xsi:type="Child_A_Type" location="xyz" />      
        ...
   </GreatGrandParent>

Now I can query using 'xsi' prefix for the relevant attribute and that
works fine. My next problem is that I can't seem to access the stuff in
the default namespace.

Any help much appreciated.

Thanks in advance.


-----Original Message-----
From: david_n_bertoni@us.ibm.com [mailto:david_n_bertoni@us.ibm.com] 
Sent: 03 June 2003 18:54
To: xalan-c-users@xml.apache.org
Subject: Re: Namespace support






> I want to configure support for namespaces when querying nodes from a 
> XalanDocument (this isn't strictly correct, but you should get the
> idea):-
>
>
> SelectSingleNode( const std::string & xPathExpression, XalanNode * 
> Context ) {
>     TheEvaluator->selectSingleNode( *TheDOMSupport,
>                                     Context,
>                                     XalanDOMString(
> xPathExpression.c_str() ).c_str() ) ;
>
> }
>
> Problem: This call causes a core dump whenever I request something 
> like
> this:-
>
> SelectSingleNode ( "prefix:ElementName/prefix:SubElement/text()" , 
> CurrentContext ) ;

I suspect you're getting a core dump because an exception is being
thrown which you're not catching.  The documentation for XPathEvaluator
looks like
this:

       * @param domSupport An instance of the corresponding
DOMSupport-derived for the DOM implementation being used.
       * @param contextNode The source tree context node
       * @param xpathString The XPath expression to evaluate
       * @param namespaceNode A node to use for namespace prefix
resolution.

So you can see the last parameter, which you're omitting, can be an
instance of a XalanElement, which the XPathEvaluator instance will use
to resolve prefixes.  If the namespace declaration for "prefix" occurs
on the document element, you can do the following:

     TheEvaluator->selectSingleNode( *TheDOMSupport,
                                     Context,
 
XalanDOMString(xPathExpression.c_str()
).c_str(),

Context->getOwnerDocument()->getDocumentElement() ) ;

Otherwise, you can create your own derivative of PrefixResolver and
return whatever string you want to bind to "prefix."

If you download the lastest CVS code, you'll find a new class called
XalanDocumentPrefixResolver, which searches the entire document for
namespace declarations.  This won't work in the case where "prefix" is
bound to multiple namespace URIs, but it does work for the simpler ones.
If you want to use prefixes which are not defined in the instance
document, you'll need your own derivate of PrefixResolver.

> As a side point, how can the documentation be improved, I find it 
> somewhat lacking in detail ;-)

It can be improved by people volunteering to work on it.  ;-)

Dave


Re: Namespace support

Posted by da...@us.ibm.com.



> I want to configure support for namespaces when querying nodes from a
> XalanDocument (this isn't strictly correct, but you should get the
> idea):-
>
>
> SelectSingleNode( const std::string & xPathExpression, XalanNode *
> Context )
> {
>     TheEvaluator->selectSingleNode( *TheDOMSupport,
>                                     Context,
>                                     XalanDOMString(
> xPathExpression.c_str() ).c_str() ) ;
>
> }
>
> Problem: This call causes a core dump whenever I request something like
> this:-
>
> SelectSingleNode ( "prefix:ElementName/prefix:SubElement/text()" ,
> CurrentContext ) ;

I suspect you're getting a core dump because an exception is being thrown
which you're not catching.  The documentation for XPathEvaluator looks like
this:

       * @param domSupport An instance of the corresponding
DOMSupport-derived for the DOM implementation being used.
       * @param contextNode The source tree context node
       * @param xpathString The XPath expression to evaluate
       * @param namespaceNode A node to use for namespace prefix
resolution.

So you can see the last parameter, which you're omitting, can be an
instance of a XalanElement, which the XPathEvaluator instance will use to
resolve prefixes.  If the namespace declaration for "prefix" occurs on the
document element, you can do the following:

     TheEvaluator->selectSingleNode( *TheDOMSupport,
                                     Context,
                                     XalanDOMString(xPathExpression.c_str()
).c_str(),

Context->getOwnerDocument()->getDocumentElement() ) ;

Otherwise, you can create your own derivative of PrefixResolver and return
whatever string you want to bind to "prefix."

If you download the lastest CVS code, you'll find a new class called
XalanDocumentPrefixResolver, which searches the entire document for
namespace declarations.  This won't work in the case where "prefix" is
bound to multiple namespace URIs, but it does work for the simpler ones.
If you want to use prefixes which are not defined in the instance document,
you'll need your own derivate of PrefixResolver.

> As a side point, how can the documentation be improved, I find it
> somewhat lacking in detail ;-)

It can be improved by people volunteering to work on it.  ;-)

Dave