XPath and namespaces : a newbie question

Discussion in 'XML' started by Omar, Jun 16, 2005.

  1. Omar

    Omar Guest

    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.
     
    Omar, Jun 16, 2005
    #1
    1. Advertising

  2. "Omar" <> 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
     
    David Carlisle, Jun 16, 2005
    #2
    1. Advertising

  3. In article <42b11335$0$11533$>,
    Omar <> 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
     
    Patrick TJ McPhee, Jun 17, 2005
    #3
  4. Omar

    Omar Guest

    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
     
    Omar, Jun 17, 2005
    #4
  5. In article <42b30fdb$0$13585$>,
    Omar <> 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
     
    Patrick TJ McPhee, Jun 21, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim
    Replies:
    2
    Views:
    480
    Richard Tobin
    Jan 29, 2004
  2. kj
    Replies:
    3
    Views:
    22,804
    sandhya
    Mar 27, 2010
  3. Stefan Franke

    XPath and namespaces...

    Stefan Franke, Jan 2, 2005, in forum: XML
    Replies:
    6
    Views:
    1,112
    Martin Honnen
    Jan 3, 2005
  4. Replies:
    1
    Views:
    508
    Romin
    Apr 20, 2005
  5. yawnmoth

    namespaces and xpath queries

    yawnmoth, Mar 31, 2010, in forum: XML
    Replies:
    3
    Views:
    3,000
    Joe Kesselman
    Mar 31, 2010
Loading...

Share This Page