Re: [xml] XPath and DTD default values



Hi,

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

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 
sorry...

Consider the following document:

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

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);
      }
   }
   xmlXPathFreeObject(xpathObjPtr);
   xmlXPathFreeContext(xpathContextPtr);
}

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.

But.
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.

[1] http://xmlsoft.org/html/libxml-parser.html#xmlReadFile
[2] http://xmlsoft.org/html/libxml-parser.html#xmlParserOption

Regards,

Kasimier



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