Added top-level namespaces when pruning XML tree for client rpc calls and restconf GET

Added new xmlns_set_all()
This commit is contained in:
Olof hagsand 2022-08-26 13:29:06 +02:00
parent 9be83d6c7e
commit ad7232d1ad
13 changed files with 83 additions and 23 deletions

View file

@ -500,6 +500,7 @@ clicon_rpc_get_config(clicon_handle h,
uint32_t session_id;
int ret;
yang_stmt *yspec;
cvec *nscd = NULL;
if (session_id_check(h, &session_id) < 0)
goto done;
@ -554,12 +555,20 @@ clicon_rpc_get_config(clicon_handle h,
}
}
if (xt && xd){
/* Sync namespaces, ie explicitly set all xmlns attributes to xd */
if (xml_nsctx_node(xd, &nscd) < 0)
goto done;
if (xml_rm(xd) < 0)
goto done;
if (xmlns_set_all(xd, nscd) < 0)
goto done;
xml_sort(xd); /* Ensure attr is first */
*xt = xd;
}
retval = 0;
done:
if (nscd)
cvec_free(nscd);
if (cb)
cbuf_free(cb);
if (xerr)
@ -858,7 +867,7 @@ clicon_rpc_get(clicon_handle h,
cvec *nsc, /* namespace context for filter */
netconf_content content,
int32_t depth,
char *defaults,
char *defaults,
cxobj **xt)
{
int retval = -1;
@ -871,6 +880,7 @@ clicon_rpc_get(clicon_handle h,
uint32_t session_id;
int ret;
yang_stmt *yspec;
cvec *nscd = NULL;
if (session_id_check(h, &session_id) < 0)
goto done;
@ -932,12 +942,20 @@ clicon_rpc_get(clicon_handle h,
}
}
if (xt && xd){
/* Sync namespaces, ie explicitly set all xmlns attributes to xd */
if (xml_nsctx_node(xd, &nscd) < 0)
goto done;
if (xml_rm(xd) < 0)
goto done;
if (xmlns_set_all(xd, nscd) < 0)
goto done;
xml_sort(xd); /* Ensure attr is first */
*xt = xd;
}
retval = 0;
done:
if (nscd)
cvec_free(nscd);
if (cb)
cbuf_free(cb);
if (xerr)
@ -993,6 +1011,7 @@ clicon_rpc_get_pageable_list(clicon_handle h,
uint32_t session_id;
int ret;
yang_stmt *yspec;
cvec *nscd = NULL;
if (datastore == NULL){
clicon_err(OE_XML, EINVAL, "datastore not given");
@ -1072,12 +1091,20 @@ clicon_rpc_get_pageable_list(clicon_handle h,
}
}
if (xt && xd){
/* Sync namespaces, ie explicitly set all xmlns attributes to xd */
if (xml_nsctx_node(xd, &nscd) < 0)
goto done;
if (xml_rm(xd) < 0)
goto done;
if (xmlns_set_all(xd, nscd) < 0)
goto done;
xml_sort(xd); /* Ensure attr is first */
*xt = xd;
}
retval = 0;
done:
if (nscd)
cvec_free(nscd);
if (cb)
cbuf_free(cb);
if (xerr)

View file

@ -77,6 +77,7 @@
#include "clixon_handle.h"
#include "clixon_yang.h"
#include "clixon_xml.h"
#include "clixon_xml_nsctx.h"
#include "clixon_xml_vec.h"
#include "clixon_data.h"
#include "clixon_text_syntax_parse.h"

View file

@ -597,6 +597,42 @@ xmlns_set(cxobj *x,
return retval;
}
/*! Given an xml node x and a namespace context, add namespace xmlns attributes to x
*
* As a side-effect, the namespace cache is set
* Check if already there
* @param[in] x XML tree
* @param[in] nsc Namespace context
* @note you need to do an xml_sort(x) after the call
*/
int
xmlns_set_all(cxobj *x,
cvec *nsc)
{
int retval = -1;
char *ns;
char *pf;
cg_var *cv = NULL;
while ((cv = cvec_each(nsc, cv)) != NULL){
pf = cv_name_get(cv);
/* Check already added */
if (pf != NULL) /* xmlns:<prefix>="<uri>" */
ns = xml_find_type_value(x, "xmlns", pf, CX_ATTR);
else{ /* xmlns="<uri>" */
ns = xml_find_type_value(x, NULL, "xmlns", CX_ATTR);
}
if (ns)
continue;
ns = cv_string_get(cv);
if (ns && xmlns_set(x, pf, ns) < 0)
goto done;
}
retval = 0;
done:
return retval;
}
/*! Get prefix of given namespace recursively
* @param[in] xn XML node
* @param[in] namespace Namespace