XPath with different namespaces problem

S

sobczyk.wojciech

I have XML:

<xml xmlns="http://abc">
<A t="ttt"></A>
<B xmlns="http://qwerty"><X>aaaa</X></B>
</xml>


How to point node "B" with XPath without any changes in this xml?

node A is pointed like this without any problem: /xml/A

but /xml/B points nothing. When remove xmlns="http://qwerty" it works!
Unfortunately I can't do any modification in this xml and add
namespace alias: xmlns:alias1="http://qwerty"
 
P

p.lepin

<xml xmlns="http://abc">
<A t="ttt"></A>
<B xmlns="http://qwerty"><X>aaaa</X></B>
</xml>

xml is a reserved name. From W3C's XML 1.0 FE, 2.3:

Names beginning with the string "xml", or with any string
which would match (('X'|'x') ('M'|'m') ('L'|'l')), are
reserved for standardization in this or future versions
of this specification.
How to point node "B" with XPath without any changes in
this xml?

node A is pointed like this without any problem: /xml/A

but /xml/B points nothing. When remove
xmlns="http://qwerty" it works! Unfortunately I can't do
any modification in this xml and add namespace alias:
xmlns:alias1="http://qwerty"

First of all, I suspect your XPath engine is broken. As far
as I can tell /xml/A should not match the node that is in a
namespace. At least libxslt and Saxon-8B seem to agree with
me.

Your XPath API should expose a method allowing you to
define namespace prefixes. Pseudocode:

xpathContext = new xpathContext ( whatever ) ;
xpathContext .
declareNsPrefix ( 'prefix' , 'http://qwerty' ) ;
xpathExpr =
xpathContext . makeXpathExpr ( '/xml/prefix:B' ) ;
nodesetFoo = xpathExpr . evaluate ( ) ;

In case your XPath API is so brain-dead it doesn't have
this feature, you can emulate the functionality with
namespace-uri() and local-name(). Note that this is ugly
and harmful for your mental well-being. You have been
warned.

Untested:

/xml/*
[namespace-uri()='http://qwerty' and local-name()='B']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top