Discussion:
Applying xPath to Xerces DOMDocument fails
o***@t-online.de
2005-11-17 09:41:12 UTC
Permalink
<font style="font-family: arial,helvetica,sans-serif;"
size="2">Hello,<br /><br />I am relatively new to Xalan and somehow it
drives me mad. I need to apply an xPath to the pre-parsed XML document.
The document is a Xerces DOMDocument instance.<br /><br />I read some
postings on how to do that, but none of them worked. I will try to
summerize my efforts below:<br /><br />I saw a post from Joerg Seidler:
http://marc.theaimsgroup.com/?l=xalan-c-users&amp;m=108385669500218&amp;w=2<br
/>and tried to do it in similar way, but with no luck.<br /><br />My
system configuration is:<br />Xerces-C 2.7.0<br />Xalan-C 1.10<br /><br
/>May be I forgot some initialization or anything else.  Below are my
explanations:<br />1. Assume that Xerces-C and XPathEvaluator
initializations succeeded. And I have successfully parsed an XML
document and have a DOMDocument instance pointer to which I need xPath
to be applied. xPath expression was successfully tested for this
document with XMLSpy and Oxygen XML Editors (internally uses Xerces-J
and Xalan-J). And the selection always delivered the desired node. I
extensively use namespaces in my document and have my own namespace
defined as default namespace. I know that xPath 1.0 does not support
default namespaces that's why I also write a prefiexed xPath to the
desired element.<br /><br /><br />My code looks as follows:<br /><br
/><br /><font size="2" style="font-family: courier
new,courier,monospace;">//xtcxsl, xtcxml are defined as follows: <br
/>/*<br />#ifdef XERCES_CPP_NAMESPACE<br />    namespace
xtcxml=XERCES_CPP_NAMESPACE;<br />#else<br />    #define xtcxml<br
/>#endif    <br /><br />#ifdef XALAN_CPP_NAMESPACE<br />    namespace
xtcxsl=XALAN_CPP_NAMESPACE;<br />#else<br />    #define xtcxsl<br
/>#endif<br /><br />*/<br /><br /><br />//assume that xPath is defined
upon as XMLCh* or char*<br /><br />xtcxsl::XMLSupportInit       
xmlSupportInit;<br /><br />xtcxsl::XercesDOMSupport     dom_support;<br
/>xtcxsl::XercesParserLiaison     parser_liaison(dom_support);<br /><br
/>parser_liaison.setBuildWrapperNodes(true);<br
/>parser_liaison.setBuildMaps(true);<br /><br
/>xtcxsl::XercesDOMWrapperParsedSource xSource(pDoc, parser_liaison,
dom_support); //xtcxml::DOMDocument* pDoc is defined upon.<br /><br
/>xtcxsl::XalanDocument&amp; xeDoc     = *xSource.getDocument();<br
/>xtcxsl::XalanElement* ns_node    = xeDoc.getDocumentElement();<br
/><br />xtcxsl::XPathEvaluator    evaluator;<br /><br
/>xtcxsl::NodeRefList result;<br />//resulting list has no elements<br
/>evaluator.selectNodeList(result, dom_support, ns_node, xPath,
ns_node);<br />//node pointer is NULL<br />xtcxsl::XalanNode*
node=evaluator.selectSingleNode(dom_support, ns_node, xPath,
ns_node);</font><br /><br /><br /><br />Using command line sample
SimpleXPathAPI with the sampe xml document and xPath returns a valid
node.<br /><br /><br />Can someone point me to what is wrong?<br /><br
/><br />Many thanks!<br />-- <br />Ovanes Markarian<br />
<br />
<br /><br />
</font>
o***@t-online.de
2005-11-17 09:46:33 UTC
Permalink
Sorry previous email was formatted as HTML and might be unreadable. Here
it is again in plain text format....
--
Ovanes Markarian

-----Original Message-----
Date: Thu, 17 Nov 2005 10:41:12 +0100
Subject: Applying xPath to Xerces DOMDocument fails
From: "***@t-online.de"
To: "Mailing List, Xalan-C"

Hello,

I am relatively new to Xalan and somehow it drives me mad. I need to
apply an xPath to the pre-parsed XML document. The document is a
Xerces DOMDocument instance.

I read some postings on how to do that, but none of them worked. I
will try to summerize my efforts below:

I saw a post from Joerg Seidler:
http://marc.theaimsgroup.com/?l=xalan-c-users&m=108385669500218&w=2
and tried to do it in similar way, but with no luck.

My system configuration is:
Xerces-C 2.7.0
Xalan-C 1.10

May be I forgot some initialization or anything else. Below are my
explanations:
1. Assume that Xerces-C and XPathEvaluator initializations succeeded.
And I have successfully parsed an XML document and have a DOMDocument
instance pointer to which I need xPath to be applied. xPath
expression was successfully tested for this document with XMLSpy and
Oxygen XML Editors (internally uses Xerces-J and Xalan-J). And the
selection always delivered the desired node. I extensively use
namespaces in my document and have my own namespace defined as
default namespace. I know that xPath 1.0 does not support default
namespaces that's why I also write a prefiexed xPath to the desired
element.

My code looks as follows:

//xtcxsl, xtcxml are defined as follows:
/*
#ifdef XERCES_CPP_NAMESPACE
namespace xtcxml=XERCES_CPP_NAMESPACE;
#else
#define xtcxml
#endif

#ifdef XALAN_CPP_NAMESPACE
namespace xtcxsl=XALAN_CPP_NAMESPACE;
#else
#define xtcxsl
#endif

*/

//assume that xPath is defined upon as XMLCh* or char*

xtcxsl::XMLSupportInit xmlSupportInit;

xtcxsl::XercesDOMSupport dom_support;
xtcxsl::XercesParserLiaison parser_liaison(dom_support);

parser_liaison.setBuildWrapperNodes(true);
parser_liaison.setBuildMaps(true);

xtcxsl::XercesDOMWrapperParsedSource xSource(pDoc, parser_liaison,
dom_support); //xtcxml::DOMDocument* pDoc is defined upon.

xtcxsl::XalanDocument& xeDoc = *xSource.getDocument();
xtcxsl::XalanElement* ns_node = xeDoc.getDocumentElement();

xtcxsl::XPathEvaluator evaluator;

xtcxsl::NodeRefList result;
//resulting list has no elements
evaluator.selectNodeList(result, dom_support, ns_node, xPath,
ns_node);
//node pointer is NULL
xtcxsl::XalanNode* node=evaluator.selectSingleNode(dom_support,
ns_node, xPath, ns_node);

Using command line sample SimpleXPathAPI with the sampe xml document
and xPath returns a valid node.

Can someone point me to what is wrong?

Many thanks!
--
Ovanes Markarian
David Bertoni
2005-11-17 19:27:22 UTC
Permalink
Post by o***@t-online.de
Hello,
I am relatively new to Xalan and somehow it drives me mad. I need to
apply an xPath to the pre-parsed XML document. The document is a Xerces
DOMDocument instance.
I read some postings on how to do that, but none of them worked. I will
http://marc.theaimsgroup.com/?l=xalan-c-users&m=108385669500218&w=2
and tried to do it in similar way, but with no luck.
Xerces-C 2.7.0
Xalan-C 1.10
May be I forgot some initialization or anything else. Below are my
1. Assume that Xerces-C and XPathEvaluator initializations succeeded.
And I have successfully parsed an XML document and have a DOMDocument
instance pointer to which I need xPath to be applied. xPath expression
was successfully tested for this document with XMLSpy and Oxygen XML
Editors (internally uses Xerces-J and Xalan-J). And the selection always
delivered the desired node. I extensively use namespaces in my document
and have my own namespace defined as default namespace. I know that
xPath 1.0 does not support default namespaces that's why I also write a
prefiexed xPath to the desired element.
...
Post by o***@t-online.de
Using command line sample SimpleXPathAPI with the sampe xml document and
xPath returns a valid node.
Can someone point me to what is wrong?
It's hard to say without seeing some actual data. Can you post a small
sample document with namespaces, and the XPath expression you're using?

You might also try just modifying the SimpleXPathAPI samples program to
use the XercesDOMSupport and XercesParserLiaison classes, instead of
writing your own code. That might help determine if the problem is in
your code, your input data, or a bug in Xalan-C++.
o***@t-online.de
2005-11-17 21:28:44 UTC
Permalink
David,
thanks for your answer. The sample code and text documents are at work.
What I can remember until now is that I stepped into the xPath
evaluation function and set breakpoint for the namespace resolution.
Somehow the default resolver used by Xalan always delivered null for the
prefix. But my root element definitely has some specified namespaces.


A possible sample document could be the following:
<root xmlns="http://my_unique_url_1" xmlns:tcs="http://my_unique_url_1"
xmlns:cf="http://my_unique_url_2" xmlns:gen="http://my_unique_url_3"
xmlns:xsi="schema-default-instance-which-I-currently-do-not-remember"
xmlns:schemaLocation="http://my_unique_url_1 ./schema1.xsd etc..."
att1="some text" att2="some text2">
<sub1 tcs:name="state1">
<cf:sub11 tcs:id="123"/>
<cf:sub11 tcs:id="1234"/>
</sub1>
</root>

xPath expression is:
/tcs:root/tcs:sub1/cf:sub[@tcs:id='123']

This is fine with your sample. But if I parse it with a non-validating
Xerces-C 2.7.0 parser and pass the DOM as described below I receive
NULL-Pointer.



Many Thanks,
--
Ovanes Markarian

-----Original Message-----
Date: Thu, 17 Nov 2005 20:27:22 +0100
Subject: Re: Applying xPath to Xerces DOMDocument fails
From: David Bertoni
Post by o***@t-online.de
Hello,
I am relatively new to Xalan and somehow it drives me mad. I need to
apply an xPath to the pre-parsed XML document. The document is a Xerces
DOMDocument instance.
I read some postings on how to do that, but none of them worked. I will
http://marc.theaimsgroup.com/?l=xalan-c-users&m=108385669500218&w=2
and tried to do it in similar way, but with no luck.
Xerces-C 2.7.0
Xalan-C 1.10
May be I forgot some initialization or anything else. Below are my
1. Assume that Xerces-C and XPathEvaluator initializations succeeded.
And I have successfully parsed an XML document and have a DOMDocument
instance pointer to which I need xPath to be applied. xPath expression
was successfully tested for this document with XMLSpy and Oxygen XML
Editors (internally uses Xerces-J and Xalan-J). And the selection always
delivered the desired node. I extensively use namespaces in my
document
Post by o***@t-online.de
and have my own namespace defined as default namespace. I know that
xPath 1.0 does not support default namespaces that's why I also write a
prefiexed xPath to the desired element.
...
Post by o***@t-online.de
Using command line sample SimpleXPathAPI with the sampe xml document and
xPath returns a valid node.
Can someone point me to what is wrong?
It's hard to say without seeing some actual data. Can you post a small
sample document with namespaces, and the XPath expression you're using?

You might also try just modifying the SimpleXPathAPI samples program to
use the XercesDOMSupport and XercesParserLiaison classes, instead of
writing your own code. That might help determine if the problem is in
your code, your input data, or a bug in Xalan-C++.
o***@t-online.de
2005-11-17 21:34:51 UTC
Permalink
Sorry the xpath expression is wrong. The correct one should be:
/tcs:root/tcs:sub1/cf:sub11[@tcs:id='123']
--
Ovanes Markarian

-----Original Message-----
Date: Thu, 17 Nov 2005 22:28:44 +0100
Subject: Re: Applying xPath to Xerces DOMDocument fails
From: "***@t-online.de"
To: xalan-c-***@xml.apache.org

David,
thanks for your answer. The sample code and text documents are at work.
What I can remember until now is that I stepped into the xPath
evaluation function and set breakpoint for the namespace resolution.
Somehow the default resolver used by Xalan always delivered null for the
prefix. But my root element definitely has some specified namespaces.


A possible sample document could be the following:

xmlns:cf="http://my_unique_url_2" xmlns:gen="http://my_unique_url_3"
xmlns:xsi="schema-default-instance-which-I-currently-do-not-remember"
xmlns:schemaLocation="http://my_unique_url_1 ./schema1.xsd etc..."
att1="some text" att2="some text2">






xPath expression is:
/tcs:root/tcs:sub1/cf:sub[@tcs:id='123']

This is fine with your sample. But if I parse it with a non-validating
Xerces-C 2.7.0 parser and pass the DOM as described below I receive
NULL-Pointer.



Many Thanks,

--
Ovanes Markarian

-----Original Message-----
Date: Thu, 17 Nov 2005 20:27:22 +0100
Subject: Re: Applying xPath to Xerces DOMDocument fails
From: David Bertoni
Post by o***@t-online.de
Hello,
I am relatively new to Xalan and somehow it drives me mad. I need to
apply an xPath to the pre-parsed XML document. The document is a Xerces
DOMDocument instance.
I read some postings on how to do that, but none of them worked. I will
http://marc.theaimsgroup.com/?l=xalan-c-users&m=108385669500218&w=2
and tried to do it in similar way, but with no luck.
Xerces-C 2.7.0
Xalan-C 1.10
May be I forgot some initialization or anything else. Below are my
1. Assume that Xerces-C and XPathEvaluator initializations succeeded.
And I have successfully parsed an XML document and have a DOMDocument
instance pointer to which I need xPath to be applied. xPath expression
was successfully tested for this document with XMLSpy and Oxygen XML
Editors (internally uses Xerces-J and Xalan-J). And the selection always
delivered the desired node. I extensively use namespaces in my
document
Post by o***@t-online.de
and have my own namespace defined as default namespace. I know that
xPath 1.0 does not support default namespaces that's why I also write a
prefiexed xPath to the desired element.
...
Post by o***@t-online.de
Using command line sample SimpleXPathAPI with the sampe xml document and
xPath returns a valid node.
Can someone point me to what is wrong?
It's hard to say without seeing some actual data. Can you post a small
sample document with namespaces, and the XPath expression you're using?

You might also try just modifying the SimpleXPathAPI samples program to
use the XercesDOMSupport and XercesParserLiaison classes, instead of
writing your own code. That might help determine if the problem is in
your code, your input data, or a bug in Xalan-C++.
David Bertoni
2005-11-18 07:22:06 UTC
Permalink
Post by o***@t-online.de
David,
thanks for your answer. The sample code and text documents are at work.
What I can remember until now is that I stepped into the xPath
evaluation function and set breakpoint for the namespace resolution.
Somehow the default resolver used by Xalan always delivered null for the
prefix. But my root element definitely has some specified namespaces.
<root xmlns="http://my_unique_url_1" xmlns:tcs="http://my_unique_url_1"
xmlns:cf="http://my_unique_url_2" xmlns:gen="http://my_unique_url_3"
xmlns:xsi="schema-default-instance-which-I-currently-do-not-remember"
xmlns:schemaLocation="http://my_unique_url_1 ./schema1.xsd etc..."
att1="some text" att2="some text2">
<sub1 tcs:name="state1">
<cf:sub11 tcs:id="123"/>
<cf:sub11 tcs:id="1234"/>
</sub1>
</root>
This is fine with your sample. But if I parse it with a non-validating
Xerces-C 2.7.0 parser and pass the DOM as described below I receive
NULL-Pointer.
If it works with the sample application, but not with the Xerces-C DOM
you build, then you should verify you are building a namespace-aware
DOM. Insert a call to setDoNamespaces(true) on the parsrer instance,
before you call the parse() member function.

Dave
o***@t-online.de
2005-11-18 09:16:10 UTC
Permalink
David,

you are the best! I really had previously set:
if (pBuilder->canSetFeature(xtcxml::XMLUni::fgDOMNamespaces, false))
pBuilder->setFeature(xtcxml::XMLUni::fgDOMNamespaces, false);

This bloody setting cost me really much time! Thank you very much for
your assumption which was true.

Hope this post will help others to identify their possible errors.

Many thanks,
--
Ovanes Markarian

-----Original Message-----
Date: Fri, 18 Nov 2005 08:22:06 +0100
Subject: Re: Applying xPath to Xerces DOMDocument fails
From: David Bertoni
Post by o***@t-online.de
David,
thanks for your answer. The sample code and text documents are at work.
What I can remember until now is that I stepped into the xPath
evaluation function and set breakpoint for the namespace resolution.
Somehow the default resolver used by Xalan always delivered null for the
prefix. But my root element definitely has some specified namespaces.
xmlns:cf="http://my_unique_url_2" xmlns:gen="http://my_unique_url_3"
xmlns:xsi="schema-default-instance-which-I-currently-do-not-remember"
xmlns:schemaLocation="http://my_unique_url_1 ./schema1.xsd etc..."
att1="some text" att2="some text2">
This is fine with your sample. But if I parse it with a non-validating
Xerces-C 2.7.0 parser and pass the DOM as described below I receive
NULL-Pointer.
If it works with the sample application, but not with the Xerces-C DOM
you build, then you should verify you are building a namespace-aware
DOM. Insert a call to setDoNamespaces(true) on the parsrer instance,
before you call the parse() member function.

Dave
Loading...