Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath with different namespaces problem

Reply
Thread Tools

XPath with different namespaces problem

 
 
sobczyk.wojciech@gmail.com
Guest
Posts: n/a
 
      02-26-2007
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"

 
Reply With Quote
 
 
 
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      02-26-2007
On Feb 26, 8:32 am, (E-Mail Removed) wrote:
> <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']

--
Pavel Lepin

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath: selecting on namespaces kj XML 3 03-27-2010 10:23 AM
XPath: Problem with namespaces reyesvsn@gmail.com XML 1 10-20-2008 10:56 AM
XPath and namespaces... Stefan Franke XML 6 01-03-2005 06:55 PM
XPath performance w/ Namespaces Jim XML 2 01-29-2004 10:58 PM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments