Re: what is the correct for xpath selector for xsi:type

On 12/08/2011 09:58 PM, Liam R E Quin wrote:
> Since libxml is stuck on XPath 1, you can use explicit xsi:type
> attributes but not type annotations that result from schema validation
> episodes - you'd need an XPath 2 engine for that, such as Saxon.
> 
>>   I eventual get a a xmlNodePtr that points to   
>>
>> <animal xsi:type="zoo:Fish">
>>       <name>tuna</name>
>>       <numberOfFins>4</numberOfFins>
>>   </animal>
>>
>> I can not seem to select the attribute. "@*" works but is not going to work long term.
>>
>> "@xsi:type" and "@type" did not work
> 
> You need to bind the prefix xsi to the right URI in the namespace
> context before evaluating the expression. Google for xpath and
> namespaces. Then you can use @xsi:type.

As Liam wrote, you'll need to register all the namespaces you intend to
use in your XPath searches with the XPath context. The code would look
something like:

xmlXPathContextPtr pContext = xmlXPathNewContext(pDoc);
....
if (xmlXPathRegisterNs(pContext,
          (xmlChar*)"xsi",
          (xmlChar*)"http://www.w3.org/2001/XMLSchema-instance") != 0)
    {
      fprintf(stderr, "Failed to register xsi\n");
    }

if (xmlXPathRegisterNs(pContext,
                     (xmlChar*)"zoo",
                     (xmlChar*)"http://www.example.org/Zoo") != 0)
    {
      fprintf(stderr, "Failed to register zoo\n");
    }

 xmlXPathObjectPtr pObj = xmlXPathEvalExpression(pzXPath, pContext);

//etc...

The pertinent libxml2 example can be found here:
http://www.xmlsoft.org/examples/xpath1.c

Hope this helps.

P

Received on Monday, 12 December 2011 12:32:04 UTC