Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath and namespaces : a newbie question

Reply
Thread Tools

XPath and namespaces : a newbie question

 
 
Omar
Guest
Posts: n/a
 
      06-16-2005
Hi,
I would use your help parsing an XML file. I am new to XPath, and I
would like to use it parsing an XML file generated with dia, the file looks
like :
<?xml version="1.0" encoding="UTF-8"?>
<dia:diagram xmlns:dia="http://www.lysator.liu.se/~alla/dia/">
<dia:diagramdata>
<dia:attribute name="background">
<dia:color val="#ffffff"/>
</dia:attribute>
<dia:attribute name="pagebreak">
<dia:color val="#000099"/>
</dia:attribute>
</dia:diagramdata>
</dia:diagram>

I would need to match all the "attribute" nodes. I don't know wether the
XPath expression i'm using is wrong or whether the API (libxml2) i'm using
is loosy ...
Trying "//attribute" matches nothing, "//dia:attribute" gives the following
libxml error : "XPath error : Undefined namespace prefix" and the wierdest
thing is that :
"//*" matches all nodes with no errors... "/*" matches the "diagram" node
with no errors but ... "/diagram" matches nothing and neither does
"/dia:diagram" which actually gives the "XPath error : Undefined namespace
prefix". Can anyone help me !

Thanks in advance.


 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      06-16-2005
"Omar" <(E-Mail Removed)> writes:

> Hi,
> I would use your help parsing an XML file. I am new to XPath, and I
> would like to use it parsing an XML file generated with dia, the file looks
> like :
> <?xml version="1.0" encoding="UTF-8"?>
> <dia:diagram xmlns:dia="http://www.lysator.liu.se/~alla/dia/">
> <dia:diagramdata>
> <dia:attribute name="background">
> <dia:color val="#ffffff"/>
> </dia:attribute>
> <dia:attribute name="pagebreak">
> <dia:color val="#000099"/>
> </dia:attribute>
> </dia:diagramdata>
> </dia:diagram>
>
> I would need to match all the "attribute" nodes. I don't know wether the
> XPath expression i'm using is wrong or whether the API (libxml2) i'm using
> is loosy ...
> Trying "//attribute" matches nothing,

It selects elements called attribute in no namespace, there are no such elements


"//dia:attribute" gives the following
> libxml error : "XPath error : Undefined namespace prefix" and the wierdest
> thing is that :

This is what you need, but you need to bind the prefix (which doesn;t
need to be teh same prefix as used in the source) to the namespace
http://www.lysator.liu.se/~alla/dia/
If the xpath is inside XSLT you bind a prefix using
xmlns:dia="http://www.lysator.liu.se/~alla/dia/"
but if calling an XPath API directly it will have alternative calls to
set up these bindings before evaluating the Xpath.


> "//*" matches all nodes with no errors...


That selects all elements in any namespace


> "/*" matches the "diagram" node


That selects the top level element bode.

> with no errors but ... "/diagram" matches nothing


That selects the top level element if it is called diagram in no
namespace

> and neither does
> "/dia:diagram" which actually gives the "XPath error : Undefined namespace
> prefix". Can anyone help me !


As above, this would work if you bind the dia prefix first.

>
> Thanks in advance.


If it is inconvenient to bind namespace prefixes in your API then you
have the alternative of
//*[local-name()='attribute']
for example. Which selects all elements with local name 'attribute' in
any namespace.

David
 
Reply With Quote
 
 
 
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      06-17-2005
In article <42b11335$0$11533$(E-Mail Removed)>,
Omar <(E-Mail Removed)> wrote:

[...]
% Trying "//attribute" matches nothing, "//dia:attribute" gives the following
% libxml error : "XPath error : Undefined namespace prefix" and the wierdest

If you're using libxml directly, use xmlXPathRegisterNs() to register
a namespace, something like

xmlXPathRegisterNs(xpc, "dia", "http://www.lysator.liu.se/~alla/dia/");

(where xpc is the xpath context pointer). The prefix (dia in this case)
doesn't have to be the same as the prefix used in your xml file, but
the URL has to match the name space definition in the xml file exactly.
--

Patrick TJ McPhee
North York Canada
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Omar
Guest
Posts: n/a
 
      06-17-2005
Hi,
xmlXPathRegisterNs works quite fine. //*[local-name()='attribute'] :
works fine too, but I'm not quite convinced that this last one is convinient
from performance standpoint. I'm anyway a little surprised to see that the
parser does not automatically perform this binding. But I suppose this is an
API specific question, and out of scope of this newsgroup.

Thanks for your help.

Omar


 
Reply With Quote
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      06-21-2005
In article <42b30fdb$0$13585$(E-Mail Removed)>,
Omar <(E-Mail Removed)> wrote:

% from performance standpoint. I'm anyway a little surprised to see that the
% parser does not automatically perform this binding. But I suppose this is an
% API specific question, and out of scope of this newsgroup.

You shouldn't be surprised. The name-space prefix is just a place-holder
for the name-space uri. The parser has no way of knowing what URI your
name-space prefix refers to unless you perform the binding. In a lot of
cases (e.g., any document with a default name space), the XPath expression
has to use a different name-space prefix than the one used in the
source document.
--

Patrick TJ McPhee
North York Canada
(E-Mail Removed)
 
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
namespaces and xpath queries yawnmoth XML 3 03-31-2010 10:27 PM
XPath: selecting on namespaces kj XML 3 03-27-2010 10:23 AM
XPath expressions having namespaces gimme_this_gimme_that@yahoo.com XML 1 04-20-2005 05:03 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



Advertisments