You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by iv...@safelayer.com on 2004/10/06 10:23:50 UTC
Problems with default namespace and XPath
Hi everybody,
I'm trying to get a DOMNode * from a valid XPath with the Xalan and Xerces
C++ apache toolkit, but I'm always getting an error telling that the node
does not exist. I figure out the problem is the default namespace, but I
don't know how to solve it.
I'm working with a XML like this:
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<QUERY xmlns="http://inti.notariado.org/XML" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance">
<REQUEST ID="REQUEST">
<HEADER>AFASFA</HEADER>
</REQUEST>
</QUERY >
If I ask for the /QUERY/REQUEST I got an error, but if I named the
namespace, the problem disappear. I mean this:
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<ns1:QUERY xmlns:ns1="http://inti.notariado.org/XML"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:REQUEST ID="REQUEST">
<ns1:HEADER>AFASFA</ns1:HEADER>
</ns1:REQUEST>
</ns1:QUERY >
Now I can ask for /ns1:QUERY/ns1:REQUEST without problems, but
this is not what I want.....
Do you know if exists an easy way where I can select a node from a
valid XPath within a default namespace?
My code that returns the node from a XPath is the following:
DOMNode* XMLUtils::getDOMNodeFromXPath( const sfly::string& xpath
) {
XercesDOMSupport xds;
XalanNode *contextNode;
XercesParserLiaison theParserLiaison;
auto_ptr<XPathEvaluator> theEvaluator( new
XPathEvaluator() );
XalanDocument *theDoc = theParserLiaison.createDocument(
parser->getDocument());
XalanDocumentPrefixResolver thePrefixResolver( theDoc );
XalanDOMString dsPath( xpath.GetData(), xpath.GetLength()
);
const XalanDOMChar *path = dsPath.c_str();
contextNode = theEvaluator->selectSingleNode( xds, theDoc,
path, thePrefixResolver );
return XalanNodeToDOMNode( contextNode );
}
Thank you very much in advance!
Best Regards,
Ivan
>-<
This email has been digitally signed. You can verify its authenticity by
installing Safelayer's Root Certificate:
http://ca.safelayer.com/install_root.html
>-<
IMPORTANT NOTICE: This communication contains information which is
confidential and may also be privileged. It is for the exclusive use of
the intended recipient(s). If you are not the intended recipient(s) please
note that any form of distribution, copying or use of this communication
or the information in it is strictly prohibited and may be unlawful. If
you have received this communication in error please return it to the
sender. The opinions expressed within this communication are not
necessarily those expressed by Safelayer Secure Communications.
Re: Problems with default namespace and XPath
Posted by Elliotte Harold <el...@metalab.unc.edu>.
One more time: You cannot use the default namespace in an XPath
expression. Unprefixed names in XPath expressions are in no namespace;
i.e. they have the namespace name of "" (the empty string) no matter
what. You *must* use prefixed names in XPath expressions to refer to
elements and attributes in namespaces, even if the document you're
querying uses the default namespace. Remember, you're matching on URIs,
not prefixes.
--
Elliotte Rusty Harold
elharo@metalab.unc.edu
---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org
Re: Problems with default namespace and XPath
Posted by iv...@safelayer.com.
Hi Elliote,
sorry but I can't understand exactly what you try to tell me. Can
you give me some kind of example about what's correct and what's not with
default namespaces and Xpaths?
For instance, imagine I want to select a node from a XML with a
default namespace using a XPath expresion.
Should be this default namespace prefixed or not? I
suspect from your response that it should be prefixed if I want to use a
XPath expression to select a node inside it, is this correct?
So, it will be not possible to select a node with XPath if the
default namespace is not prefixed. Do you agree with that?
Thank you very much again,
Ivan
Elliotte Harold <el...@metalab.unc.edu>
06/10/2004 12:41
Please respond to xalan-dev
To: xalan-dev@xml.apache.org
cc:
Subject: Re: Problems with default namespace and XPath
ivan.fontanals@safelayer.com wrote:
> If I ask for the /QUERY/REQUEST I got an error, but if I named
> the namespace, the problem disappear. I mean this:
>
The default namespace never applies within XPath expressions. You need
to use prefixed names in the XPath expressions even if you use
unprefixed names in the document. XPath matches by namespace URI, not by
prefix.
--
Elliotte Rusty Harold
elharo@metalab.unc.edu
---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org
Re: Problems with default namespace and XPath
Posted by Elliotte Harold <el...@metalab.unc.edu>.
ivan.fontanals@safelayer.com wrote:
> If I ask for the /QUERY/REQUEST I got an error, but if I named
> the namespace, the problem disappear. I mean this:
>
The default namespace never applies within XPath expressions. You need
to use prefixed names in the XPath expressions even if you use
unprefixed names in the document. XPath matches by namespace URI, not by
prefix.
--
Elliotte Rusty Harold
elharo@metalab.unc.edu
---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org