"Omar" <no-> 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