Please help extract attributes from root node.

Discussion in 'XML' started by RichardHatcher.com@gmail.com, Jun 8, 2006.

  1. Guest

    I am writing C code using xpath to extract some data from a XML
    document. The file uses namespaces, but the name spaces are all listed
    in the root node of the file as attributes. I want to extract all the
    attributes, so that I can register the namespaces. The document looks
    like:

    <?xml version="1.0" encoding="UTF-8" ?>
    <ConfigDataFile xmlns:SRI="SRI" xmlns:SIW="SIW" xmlns:QBQ="QBQ"
    xmlns:VLD="VLD" xmlns:xsi="http://www.mstsc.com/schema"
    xsi:schemaLocation="birdflu.xsd">
    <SIW:SIW>
    <SIW:herewelive apt="yes" pets="none">
    <SIW:color>RED</SIW:color>
    </SIW:herewelive>
    </SIW:SIW>
    <QDB:QDB>
    <QDB:herewelive apt="NO" crackhouse="no">
    <QDB:color>Blue</SIW:color>
    </QDB:herewelive>
    </QDB:QDB>
    </ConfigDataFile>

    My code is:

    xmlDocPtr doc = NULL;
    xmlNodePtr node_ptr = NULL;
    xmlAttrPtr attr_ptr = NULL;
    xmlXPathContextPtr xpathCtx = NULL;
    xmlXPathObjectPtr xpathObj = NULL;

    xmlInitParser();

    doc = xmlParseFile("test.xml");
    xpathCtx = xmlXPathNewContext(doc);
    xpathObj = xmlXPathEvalExpression("//ConfigDataFile", xpathCtx);
    node_ptr = xpathObj->nodesetval->nodeTab[0];

    attr_ptr = node_ptr->properties;
    while (attr_ptr != NULL)
    {
    printf("The Node name is %s\n", node_ptr->name);
    printf("Attribute name: %s\n",attr_ptr->name);
    printf("Attribute value: %s\n",xmlGetProp(node_ptr,
    attr_ptr->name));

    attr_ptr = attr_ptr->next;

    }
    return 1;
    }

    The problem is that when I run this, the output is only:

    The Node name is ConfigDataFile
    Attribute name: schemaLocation
    Attribute value: birdflu.xsd

    All the other attributes are not there. I have used my debugger to
    look at the entire node, and the other attributes are just not there.
    If I change the eval expression to any other node, I can print all the
    attributes of that node. The only node that refuses to act how I
    desire is the ConfigDataFile node.

    Thank you.
     
    , Jun 8, 2006
    #1
    1. Advertising

  2. In XPath, namespace nodes are namespace nodes, *not* attribute nodes. If
    you want to see them, you need to use the namespace:: axis... which
    includes all inherited namespaces as well as those on this specific node.

    (XPath 2.0 is changing how it handles namespace information -- long
    story -- but the attributes axis will still not include namespace
    declarations.)
     
    Joe Kesselman, Jun 8, 2006
    #2
    1. Advertising

  3. Guest

    Bless you Mr. Kesselman.

    Joe Kesselman wrote:
    > In XPath, namespace nodes are namespace nodes, *not* attribute nodes. If
    > you want to see them, you need to use the namespace:: axis... which
    > includes all inherited namespaces as well as those on this specific node.
    >
    > (XPath 2.0 is changing how it handles namespace information -- long
    > story -- but the attributes axis will still not include namespace
    > declarations.)
     
    , Jun 8, 2006
    #3
    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:
    0
    Views:
    1,470
  2. KK
    Replies:
    2
    Views:
    596
    Big Brian
    Oct 14, 2003
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,440
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    1
    Views:
    566
    Martin Honnen
    May 8, 2007
  5. Slafs

    XML root node attributes

    Slafs, Nov 17, 2009, in forum: Python
    Replies:
    3
    Views:
    1,498
    Slafs
    Nov 18, 2009
Loading...

Share This Page