PostGIS  3.4.0dev-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 228 of file lwgeom_in_gml.c.

229 {
230  char *id;
231  xmlNsPtr *ns, *n;
232  xmlXPathContext *ctx;
233  xmlXPathObject *xpath;
234  xmlNodePtr node, ret_node;
235  xmlChar *href, *p, *node_id;
236 
237  href = xmlGetNsProp(xnode, (xmlChar *)"href", (xmlChar *) XLINK_NS);
238  id = lwalloc((xmlStrlen(xnode->ns->prefix) * 2 + xmlStrlen(xnode->name)
239  + xmlStrlen(href) + sizeof("//:[@:id='']") + 1));
240  p = href;
241  p++; /* ignore '#' first char */
242 
243  /* XPath pattern look like: //gml:point[@gml:id='p1'] */
244  sprintf(id, "//%s:%s[@%s:id='%s']", (char *) xnode->ns->prefix,
245  (char *) xnode->name,
246  (char *) xnode->ns->prefix,
247  (char *) p);
248 
249  ctx = xmlXPathNewContext(xnode->doc);
250  if (ctx == NULL)
251  {
252  xmlFree(href);
253  lwfree(id);
254  return NULL;
255  }
256 
257  /* Handle namespaces */
258  ns = xmlGetNsList(xnode->doc, xnode);
259  for (n=ns ; *n; n++) xmlXPathRegisterNs(ctx, (*n)->prefix, (*n)->href);
260  xmlFree(ns);
261 
262  /* Execute XPath expression */
263  xpath = xmlXPathEvalExpression((xmlChar *) id, ctx);
264  lwfree(id);
265  if (xpath == NULL || xpath->nodesetval == NULL || xpath->nodesetval->nodeNr != 1)
266  {
267  xmlFree(href);
268  xmlXPathFreeObject(xpath);
269  xmlXPathFreeContext(ctx);
270  return NULL;
271  }
272  ret_node = xpath->nodesetval->nodeTab[0];
273  xmlXPathFreeObject(xpath);
274  xmlXPathFreeContext(ctx);
275 
276  /* Protection against circular calls */
277  for (node = xnode ; node != NULL ; node = node->parent)
278  {
279  if (node->type != XML_ELEMENT_NODE) continue;
280  node_id = gmlGetProp(node, (xmlChar *) "id");
281  if (node_id != NULL)
282  {
283  if (!xmlStrcmp(node_id, p))
284  gml_lwpgerror("invalid GML representation", 2);
285  xmlFree(node_id);
286  }
287  }
288 
289  xmlFree(href);
290  return ret_node;
291 }
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, xmlChar *prop)
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: