Return a xmlNodePtr on a node referenced by a XLink or NULL otherwise.
252{
253 char *id;
254 xmlNsPtr *ns, *n;
255 xmlXPathContext *ctx;
256 xmlXPathObject *xpath;
257 xmlNodePtr node, ret_node;
258 xmlChar *href, *p, *node_id;
259
260 href = xmlGetNsProp(xnode, (xmlChar *)
"href", (xmlChar *)
XLINK_NS);
261 p = href;
262 p++;
263
264 if (xnode->ns)
265 {
266 id =
lwalloc((xmlStrlen(xnode->ns->prefix) * 2 + xmlStrlen(xnode->name) +
267 xmlStrlen(href) + sizeof("//:[@:id='']") + 1));
268
269 sprintf(id, "//%s:%s[@%s:id='%s']",
270 (char *) xnode->ns->prefix,
271 (char *) xnode->name,
272 (char *) xnode->ns->prefix,
273 (char *) p);
274 }
275 else
276 {
277 id =
lwalloc((xmlStrlen(xnode->name) +
278 xmlStrlen(href) + sizeof("//:[@:id='']") + 1));
279
280 sprintf(id, "//%s[@id='%s']",
281 (char *) xnode->name,
282 (char *) p);
283 }
284
285 ctx = xmlXPathNewContext(xnode->doc);
286 if (ctx == NULL)
287 {
288 xmlFree(href);
290 return NULL;
291 }
292
293
294 ns = xmlGetNsList(xnode->doc, xnode);
295 for (n=ns ; *n; n++) xmlXPathRegisterNs(ctx, (*n)->prefix, (*n)->href);
296 xmlFree(ns);
297
298
299 xpath = xmlXPathEvalExpression((xmlChar *) id, ctx);
301 if (xpath == NULL || xpath->nodesetval == NULL || xpath->nodesetval->nodeNr != 1)
302 {
303 xmlFree(href);
304 xmlXPathFreeObject(xpath);
305 xmlXPathFreeContext(ctx);
306 return NULL;
307 }
308 ret_node = xpath->nodesetval->nodeTab[0];
309 xmlXPathFreeObject(xpath);
310 xmlXPathFreeContext(ctx);
311
312
313 for (node = xnode ; node != NULL ; node = node->parent)
314 {
315 if (node->type != XML_ELEMENT_NODE) continue;
317 if (node_id != NULL)
318 {
319 if (!xmlStrcmp(node_id, p))
321 xmlFree(node_id);
322 }
323 }
324
325 xmlFree(href);
326 return ret_node;
327}
void * lwalloc(size_t size)
static xmlChar * gmlGetProp(xmlNodePtr xnode, const char *charProp)
Retrieve a GML property from a node or NULL otherwise Respect namespaces if presents in the node elem...
static void gml_lwpgerror(char *msg, __attribute__((__unused__)) int error_code)