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 "Nageswar, Jaya" <Ja...@ca.com> on 2007/10/08 14:05:18 UTC

xpath expression with namespace

Whenever I have a default namespace in my xml file, xpath expression is
not returning any nodeset.

 

I have my xml file as follows.

 

Sample.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/">

 

    <message name="HelpRequestMsg">

            <part name="body" element="Help"/>

    </message>

    <message name="HelpResponseMsg">

            <part name="body" element="HelpResponse"/>

    </message>

    <message name="ItemSearchRequestMsg">

            <part name="body" element="ItemSearch"/>

    </message>

 

</definitions>

 

I am trying to use xpath expression as "//message". My code is as
follows. I am using xerces as well as xalan in my application.

 

XMLPlatformUtils::Initialize();

XPathEvaluator::initialize();

 

XercesDOMParser *parser = new XercesDOMParser;

      

parser->setDoNamespaces(true);

parser->setDoSchema(true);

parser->setValidationSchemaFullChecking(true);

parser->setCreateEntityReferenceNodes(true);

 

XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;

 

parser->parse(m_xmlfile);

doc = parser->getDocument();

 

std::string xpath = "//message";  //xpath expression

      

XercesDOMSupport dom_support;

XercesParserLiaison parser_liaison;

XalanDocument *xalan_document = parser_liaison.createDocument(doc,
false, true);

XalanNode * root_context_node = xalan_document;

XalanElement *namespace_node = xalan_document->getDocumentElement();

 

 

const XalanDOMString expression(xpath.c_str());

XPathEvaluator evaluator;

NodeRefList nodelist;

                  

                  

                  evaluator.selectNodeList( nodelist,

                                            dom_support,

                                            root_context_node,

                                            expression.c_str(),

                                            namespace_node);

 

                          

NodeRefList::size_type length = nodelist.getLength();

 

 

Since I have default namespace xmlns=http://schemas.xmlsoap.org/wsdl/ in
my input xml file I'm getting length as zero.

If I remove the default namespace them I am getting length as 3 which is
correct for my xpath expression.

 

Please help me how xpath expression needs to be modified whenever we
have default namespace. 

Any help regarding xpath expressions whenever there are namespaces in
the xml file are appreciated.

 

 

Thanks & regards,

Jaya Nageswar.

 


Re: xpath expression with namespace

Posted by David Bertoni <db...@apache.org>.
Nageswar, Jaya wrote:
> Hi David,
> 
> 	Can you please give an example/ code snippet for implementing
> PrefixResolver and using it with.XpathEvaluator. It will be a great help
> to me.
Please don't send postings to my private email address -- that's what the 
mailing list is for.

I don't generally have time to provide examples for everyone that asks. 
Again, I would remind you that there is plenty of code for you to look at, 
including the SimpleXPathAPI sample, which uses the existing 
XalanDocumentPrefixResolver class.

Dave

Re: xpath expression with namespace

Posted by David Bertoni <db...@apache.org>.
Nageswar, Jaya wrote:
> Hi Dave,
> 
> 	Thanks for the reply.  Do you mean to say that we need to give
> prefix to the default namespace in the input xml file? Is that what you
> mean by mapping a prefix to the default namespace? 
> 
> Is there anyway with out modifying my input xml file?
If you read my original reply, you will see that I answered your question:

"You need to use a namespace prefix in your XPath expressions:

"//ns:message"

You'll also need to implement a PrefixResolver derivative and pass an 
instance of that into the XPathEvaluator, so the XPath implementation can 
map the namespace prefix to the proper namespace URI.  You can look at the 
existing PrefixResolver instances to see what's involved."

You have the source code, so please use it!  In particular, you might see 
what happens when you supply a XalanElement node in the call to evaluate 
the XPath expression.

Dave

RE: xpath expression with namespace

Posted by da...@us.ibm.com.
>Do you mean to say that we need to give
>prefix to the default namespace in the input xml file?

No, the input XML has already assigned the elements to a namespace.
In a path expression, the element names must be assigned to that
same namespace in order to match. But in an XPath 1.0 expression,
you can't have a default (i.e., assumed) namespace for element names,
meaning that a prefix is the only way to set the namespace on an
element name in the expression. See
http://www.ibm.com/developerworks/xml/library/x-nmspace.html
http://www.ibm.com/developerworks/xml/library/x-nmspace2.html
for more extensive information about namespaces.
.................David Marston

RE: xpath expression with namespace

Posted by "Nageswar, Jaya" <Ja...@ca.com>.
Hi Dave,

	Thanks for the reply.  Do you mean to say that we need to give
prefix to the default namespace in the input xml file? Is that what you
mean by mapping a prefix to the default namespace? 

Is there anyway with out modifying my input xml file?

	

-----Original Message-----
From: David Bertoni [mailto:dbertoni@apache.org] 
Sent: Tuesday, October 09, 2007 10:40 PM
To: xalan-c-users@xml.apache.org
Subject: Re: xpath expression with namespace

Nageswar, Jaya wrote:
> Hi Dave,
> 
> If I have namespace like xmlns:ns = "http://....." in my xmal file
> (sample.xml) then I can use the xpath expression as you suggested
> ("//ns:message").
Yes, because you are using the root element as the prefix resolver.
Note 
this technique won't work if the namespace declaration doesn't occur on
the 
root element.  It also won't work if there is more than one mapping for
the 
namespace declaration, but that's a different problem.

> 
> This xpath expression is able to retrieve desired results with the
> following xpathevaluator with out using prefixresolver.
> 
> 	evaluator.selectNodeList( nodelist,
> 	                                          dom_support,
> 				       root_context_node,
> 	                                          expression.c_str(),
> 	                                          namespace_node);
> 
> But my question is how do we need to give xpath expression if we have
> default namespace(xmlsns="http://....).
As I said, you have to map a prefix to the namespace and use that prefix
in 
your XPath expressions.  There is nothing magical about the default 
namespace, so you can map any prefix to the same namespace URI.
Similarly, 
you can map two different prefixes to the same namespace URI and use
either 
of them.

Dave


Re: xpath expression with namespace

Posted by David Bertoni <db...@apache.org>.
Nageswar, Jaya wrote:
> Hi Dave,
> 
> If I have namespace like xmlns:ns = "http://....." in my xmal file
> (sample.xml) then I can use the xpath expression as you suggested
> ("//ns:message").
Yes, because you are using the root element as the prefix resolver.  Note 
this technique won't work if the namespace declaration doesn't occur on the 
root element.  It also won't work if there is more than one mapping for the 
namespace declaration, but that's a different problem.

> 
> This xpath expression is able to retrieve desired results with the
> following xpathevaluator with out using prefixresolver.
> 
> 	evaluator.selectNodeList( nodelist,
> 	                                          dom_support,
> 				       root_context_node,
> 	                                          expression.c_str(),
> 	                                          namespace_node);
> 
> But my question is how do we need to give xpath expression if we have
> default namespace(xmlsns="http://....).
As I said, you have to map a prefix to the namespace and use that prefix in 
your XPath expressions.  There is nothing magical about the default 
namespace, so you can map any prefix to the same namespace URI.  Similarly, 
you can map two different prefixes to the same namespace URI and use either 
of them.

Dave

RE: xpath expression with namespace

Posted by "Nageswar, Jaya" <Ja...@ca.com>.
Hi Dave,

If I have namespace like xmlns:ns = "http://....." in my xmal file
(sample.xml) then I can use the xpath expression as you suggested
("//ns:message").

This xpath expression is able to retrieve desired results with the
following xpathevaluator with out using prefixresolver.

	evaluator.selectNodeList( nodelist,
	                                          dom_support,
				       root_context_node,
	                                          expression.c_str(),
	                                          namespace_node);

But my question is how do we need to give xpath expression if we have
default namespace(xmlsns="http://....).

-----Original Message-----
From: David Bertoni [mailto:dbertoni@apache.org] 
Sent: Monday, October 08, 2007 11:47 PM
To: xalan-c-users@xml.apache.org
Subject: Re: xpath expression with namespace

Nageswar, Jaya wrote:
> Whenever I have a default namespace in my xml file, xpath expression
is 
> not returning any nodeset.
> 
>  
>
..

> 
> I am trying to use xpath expression as "//message". My code is as 
> follows. I am using xerces as well as xalan in my application.
>
..
> Since I have default namespace xmlns=http://schemas.xmlsoap.org/wsdl/
in 
> my input xml file I'm getting length as zero.
> 
> If I remove the default namespace them I am getting length as 3 which
is 
> correct for my xpath expression.
> 
> Please help me how xpath expression needs to be modified whenever we 
> have default namespace.
> 
> Any help regarding xpath expressions whenever there are namespaces in 
> the xml file are appreciated.
You need to use a namespace prefix in your XPath expressions:

"//ns:message"

You'll also need to implement a PrefixResolver derivative and pass an 
instance of that into the XPathEvaluator, so the XPath implementation
can 
map the namespace prefix to the proper namespace URI.  You can look at
the 
existing PrefixResolver instances to see what's involved.

Dave


Re: xpath expression with namespace

Posted by David Bertoni <db...@apache.org>.
Nageswar, Jaya wrote:
> Whenever I have a default namespace in my xml file, xpath expression is 
> not returning any nodeset.
> 
>  
>
..

> 
> I am trying to use xpath expression as “//message”. My code is as 
> follows. I am using xerces as well as xalan in my application.
>
..
> Since I have default namespace xmlns=http://schemas.xmlsoap.org/wsdl/ in 
> my input xml file I’m getting length as zero.
> 
> If I remove the default namespace them I am getting length as 3 which is 
> correct for my xpath expression.
> 
> Please help me how xpath expression needs to be modified whenever we 
> have default namespace.
> 
> Any help regarding xpath expressions whenever there are namespaces in 
> the xml file are appreciated.
You need to use a namespace prefix in your XPath expressions:

"//ns:message"

You'll also need to implement a PrefixResolver derivative and pass an 
instance of that into the XPathEvaluator, so the XPath implementation can 
map the namespace prefix to the proper namespace URI.  You can look at the 
existing PrefixResolver instances to see what's involved.

Dave

Re: xpath expression with namespace

Posted by Stéphane Peillon <st...@meteo.fr>.
You must define a prefix for your default namespace (for exemple with a 
XalanNamespacesStack). XPath expression can't be apply to a default 
namespace.

Nageswar, Jaya a écrit :
> Whenever I have a default namespace in my xml file, xpath expression is
> not returning any nodeset.
>
>  
>
> I have my xml file as follows.
>
>  
>
> Sample.xml
>
>  
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <definitions xmlns="http://schemas.xmlsoap.org/wsdl/">
>
>  
>
>     <message name="HelpRequestMsg">
>
>             <part name="body" element="Help"/>
>
>     </message>
>
>     <message name="HelpResponseMsg">
>
>             <part name="body" element="HelpResponse"/>
>
>     </message>
>
>     <message name="ItemSearchRequestMsg">
>
>             <part name="body" element="ItemSearch"/>
>
>     </message>
>
>  
>
> </definitions>
>
>  
>
> I am trying to use xpath expression as "//message". My code is as
> follows. I am using xerces as well as xalan in my application.
>
>  
>
> XMLPlatformUtils::Initialize();
>
> XPathEvaluator::initialize();
>
>  
>
> XercesDOMParser *parser = new XercesDOMParser;
>
>       
>
> parser->setDoNamespaces(true);
>
> parser->setDoSchema(true);
>
> parser->setValidationSchemaFullChecking(true);
>
> parser->setCreateEntityReferenceNodes(true);
>
>  
>
> XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc = 0;
>
>  
>
> parser->parse(m_xmlfile);
>
> doc = parser->getDocument();
>
>  
>
> std::string xpath = "//message";  //xpath expression
>
>       
>
> XercesDOMSupport dom_support;
>
> XercesParserLiaison parser_liaison;
>
> XalanDocument *xalan_document = parser_liaison.createDocument(doc,
> false, true);
>
> XalanNode * root_context_node = xalan_document;
>
> XalanElement *namespace_node = xalan_document->getDocumentElement();
>
>  
>
>  
>
> const XalanDOMString expression(xpath.c_str());
>
> XPathEvaluator evaluator;
>
> NodeRefList nodelist;
>
>                   
>
>                   
>
>                   evaluator.selectNodeList( nodelist,
>
>                                             dom_support,
>
>                                             root_context_node,
>
>                                             expression.c_str(),
>
>                                             namespace_node);
>
>  
>
>                           
>
> NodeRefList::size_type length = nodelist.getLength();
>
>  
>
>  
>
> Since I have default namespace xmlns=http://schemas.xmlsoap.org/wsdl/ in
> my input xml file I'm getting length as zero.
>
> If I remove the default namespace them I am getting length as 3 which is
> correct for my xpath expression.
>
>  
>
> Please help me how xpath expression needs to be modified whenever we
> have default namespace. 
>
> Any help regarding xpath expressions whenever there are namespaces in
> the xml file are appreciated.
>
>  
>
>  
>
> Thanks & regards,
>
> Jaya Nageswar.
>
>  
>
>
>