PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ get_xlink_node()

static xmlNodePtr get_xlink_node ( xmlNodePtr  xnode)
static

Return a xmlNodePtr on a node referenced by a XLink or NULL otherwise.

Definition at line 258 of file lwgeom_in_gml.c.

259 {
260  char *id;
261  xmlNsPtr *ns, *n;
262  xmlXPathContext *ctx;
263  xmlXPathObject *xpath;
264  xmlNodePtr node, ret_node;
265  xmlChar *href, *p, *node_id;
266 
267  href = xmlGetNsProp(xnode, (xmlChar *)"href", (xmlChar *) XLINK_NS);
268  p = href;
269  p++; /* ignore '#' first char */
270 
271  if (xnode->ns)
272  {
273  id = lwalloc((xmlStrlen(xnode->ns->prefix) * 2 + xmlStrlen(xnode->name) +
274  xmlStrlen(href) + sizeof("//:[@:id='']") + 1));
275  /* XPath pattern look like: //gml:point[@gml:id='p1'] */
276  sprintf(id, "//%s:%s[@%s:id='%s']",
277  (char *) xnode->ns->prefix,
278  (char *) xnode->name,
279  (char *) xnode->ns->prefix,
280  (char *) p);
281  }
282  else
283  {
284  id = lwalloc((xmlStrlen(xnode->name) +
285  xmlStrlen(href) + sizeof("//:[@:id='']") + 1));
286  /* XPath pattern look like: //gml:point[@gml:id='p1'] */
287  sprintf(id, "//%s[@id='%s']",
288  (char *) xnode->name,
289  (char *) p);
290  }
291 
292  ctx = xmlXPathNewContext(xnode->doc);
293  if (ctx == NULL)
294  {
295  xmlFree(href);
296  lwfree(id);
297  return NULL;
298  }
299 
300  /* Handle namespaces */
301  ns = xmlGetNsList(xnode->doc, xnode);
302  for (n=ns ; *n; n++) xmlXPathRegisterNs(ctx, (*n)->prefix, (*n)->href);
303  xmlFree(ns);
304 
305  /* Execute XPath expression */
306  xpath = xmlXPathEvalExpression((xmlChar *) id, ctx);
307  lwfree(id);
308  if (xpath == NULL || xpath->nodesetval == NULL || xpath->nodesetval->nodeNr != 1)
309  {
310  xmlFree(href);
311  xmlXPathFreeObject(xpath);
312  xmlXPathFreeContext(ctx);
313  return NULL;
314  }
315  ret_node = xpath->nodesetval->nodeTab[0];
316  xmlXPathFreeObject(xpath);
317  xmlXPathFreeContext(ctx);
318 
319  /* Protection against circular calls */
320  for (node = xnode ; node != NULL ; node = node->parent)
321  {
322  if (node->type != XML_ELEMENT_NODE) continue;
323  node_id = gmlGetProp(node, "id");
324  if (node_id != NULL)
325  {
326  if (!xmlStrcmp(node_id, p))
327  gml_lwpgerror("invalid GML representation", 2);
328  xmlFree(node_id);
329  }
330  }
331 
332  xmlFree(href);
333  return ret_node;
334 }
void lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define XLINK_NS
Definition: lwgeom_in_gml.c:75
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)
Definition: lwgeom_in_gml.c:81

References gml_lwpgerror(), gmlGetProp(), lwalloc(), lwfree(), and XLINK_NS.

Referenced by parse_gml_coll(), parse_gml_curve(), parse_gml_data(), parse_gml_line(), parse_gml_linearring(), parse_gml_mcurve(), parse_gml_mline(), parse_gml_mpoint(), parse_gml_mpoly(), parse_gml_msurface(), parse_gml_point(), parse_gml_polygon(), parse_gml_psurface(), parse_gml_surface(), parse_gml_tin(), and parse_gml_triangle().

Here is the call graph for this function:
Here is the caller graph for this function: