Re: [xml] XPath and DTD default values


On Mon, 2005-06-20 at 00:44 +0200, Jonas Holmberg wrote:

I have a question about the XPath implementation.
I have tried to get the default attributes from the DTD:s to work with the 
XPath lookups.
I am very new to libxml, so if there is a very well known answer, I'm 

Consider the following document:

<?xml version="1.0"?>
<!DOCTYPE root SYSTEM "root.dtd" [
<!ATTLIST elem test CDATA "nothing">
<!ELEMENT root (elem)>
 <elem test="something">data3</elem>

with this code:

void doXPath(xmlDocPtr doc,xmlChar* xpathExpr)
   xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
   xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xpathExpr,xpathCtx);
   xmlNodeSetPtr nodes = xpathObj->nodesetval;
   for (int i = 0; i < nodes->nodeNr; i++) {
      if (nodes->nodeTab[i]->type == XML_ELEMENT_NODE) {
         xmlNodePtr node = nodes->nodeTab[i];
         xmlChar* pProp = xmlGetProp(node,BAD_CAST "test");
         xmlChar* pContent = xmlNodeGetContent(node);
         std::cout << "Node: " << node->name << ", Data: " << pContent << ", 
test = " << pProp << endl;
         xmlFree((void*) pContent);
         xmlFree((void*) pProp);

doXPath(doc,(xmlChar*) "//root/*");
gives me:
Node: elem, Data: data1, test = nothing
Node: elem, Data: data2, test = nothing
Node: elem, Data: data3, test = something

So, xmlGetProp works great, giving me the default value of "nothing"

doXPath(doc,(xmlChar*) "//root/*[ test=\"something\"");
gives me:
Node: elem, Data: data3, test = something

Still, as expected.

doXPath(doc,(xmlChar*) "//root/*[ test=\"nothing\"");
gives me no results...

Am I wrong in expecting xpath to work with default values, or can anybody 
help me out?

Functions like xmlReadFile [1] take an @options [2] argument. The
XML_PARSE_DTDATTR option-flag will create default attributes
of the DTD in the resulting node tree, thereafter they will be
visible to XPath.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]