questions using libxml2

Discussion in 'XML' started by bdecker@flowcom.com.au, Mar 2, 2004.

  1. Guest

    Hello,

    I was hoping that someone here can please answer two questions
    concerning libxml2 python module use:

    1. Is this snippet correct?
    doc = libxml2.parseDoc(readfile(filename))
    ctxt = doc.xpathNewContext()
    ctxt.xpathRegisterNs("xlink","http://www.w3.org/1999/xlink")

    .... if so, to what purpose does one register the namespace
    (xpathRegisterNs)???
    It doesn't seem to effect the outcome of the script:
    http://66.102.7.104/search?q=cache:...0/weblog/2004/Jan xlink python&hl=en&ie=UTF-8

    ....so I can't understand why it is being used.


    2. Why do I seem to get unpredictable behaviour from code like:
    doc = libxml2.parseFile("D:\\D\\XML\\system.xml")
    current_nodes = doc.xpathEval("descendant-or-self::linux")
    node = current_nodes[0]
    print node
    ctxt = node
    while node:
    node = node.xpathNextDescendant(node)
    print node

    How would I generate a list of all decendants of a node (besides
    searching for children of children)?


    3. Is there any such thing as an xlink ELEMENT (where element.ns =
    "http://www.w3.org/1999/xlink")? The only examples that I can find only
    demonstrate xlink ATTRIBUTES (where the namespace has to be declared in
    every element with xlink attributes). I don't even know the reason for
    declaring a namespace if we always do that.


    Hopefuly someone can clear up my confusion.


    Ben
     
    , Mar 2, 2004
    #1
    1. Advertising

  2. In article <>,
    <> wrote:

    % 1. Is this snippet correct?
    % doc = libxml2.parseDoc(readfile(filename))
    % ctxt = doc.xpathNewContext()
    % ctxt.xpathRegisterNs("xlink","http://www.w3.org/1999/xlink")

    It looks fine as far as it goes.

    % ... if so, to what purpose does one register the namespace
    % (xpathRegisterNs)???

    It depends on what you're doing. If you want to match against an element
    or attribute in the xlink name space, then you need to register the name
    space and use the registered prefix in your searches. For instance, you
    could look for

    //*[@xlink:href]

    to find all the elements with links from them. In the actual document,
    the name space prefix could be different, so you could match

    <note xl:href='http://www.somewhere.somewhat/etc/etc/etc'>...</note>

    % ...so I can't understand why it is being used.

    It might not be necessary.

    % 3. Is there any such thing as an xlink ELEMENT (where element.ns =

    There isn't, but why not read the overview and find out?
    http://www.w3.org/TR/xlink

    % demonstrate xlink ATTRIBUTES (where the namespace has to be declared in
    % every element with xlink attributes). I don't even know the reason for

    You might want to read up on name spaces, too. The key is to have the
    name space declaration be in scope at the point the name space is
    used. This doesn't require that it be declared in every element
    that uses it, although some tools might handle it that way rather than
    migrating the declaration up an element. Try adding the name space
    declaration to the top-level element in your document tree.



    --

    Patrick TJ McPhee
    East York Canada
     
    Patrick TJ McPhee, Mar 2, 2004
    #2
    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. Replies:
    1
    Views:
    473
    Stefan Behnel
    Jan 6, 2007
  2. Andrew Marlow
    Replies:
    2
    Views:
    404
    Andrew Marlow
    Jan 18, 2007
  3. corley

    decimal symbol problem (using libxml2)

    corley, Jul 5, 2006, in forum: C Programming
    Replies:
    2
    Views:
    415
    Keith Thompson
    Jul 5, 2006
  4. Replies:
    6
    Views:
    891
    Matej Cepl
    Apr 12, 2007
  5. sharan
    Replies:
    0
    Views:
    373
    sharan
    May 2, 2008
Loading...

Share This Page