Skip to content

Commit 133f910

Browse files
committed
printer xml UPDATE short prefix for JSON attributes
1 parent e41e986 commit 133f910

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

src/printer_xml.c

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,15 @@ struct xmlpr_ctx {
5050
#define LYXML_PREFIX_REQUIRED 0x01 /**< The prefix is not just a suggestion but a requirement. */
5151
#define LYXML_PREFIX_DEFAULT 0x02 /**< The namespace is required to be a default (without prefix) */
5252

53+
static LY_ERR xml_print_node(struct xmlpr_ctx *pctx, const struct lyd_node *node);
54+
5355
/**
5456
* @brief Print a namespace if not already printed.
5557
*
56-
* @param[in] ctx XML printer context.
58+
* @param[in] pctx XML printer context.
5759
* @param[in] ns Namespace to print, expected to be in dictionary.
5860
* @param[in] new_prefix Suggested new prefix, NULL for a default namespace without prefix. Stored in the dictionary.
59-
* @param[in] prefix_opts Prefix options changing the meaning of parameters.
61+
* @param[in] prefix_opts Prefix options.
6062
* @return Printed prefix of the namespace to use.
6163
*/
6264
static const char *
@@ -105,9 +107,21 @@ xml_print_ns(struct xmlpr_ctx *pctx, const char *ns, const char *new_prefix, uin
105107
return pctx->prefix.objs[i];
106108
}
107109

110+
/**
111+
* @brief Print an opaque name (opaque node or an attribute) namespace.
112+
*
113+
* @param[in] pctx XML printer context.
114+
* @param[in] format Opaque name format.
115+
* @param[in] name Opaque name to print.
116+
* @param[in] prefix_opts Prefix options.
117+
* @return Printed prefix of the namespace to use.
118+
*/
108119
static const char *
109120
xml_print_ns_opaq(struct xmlpr_ctx *pctx, LY_VALUE_FORMAT format, const struct ly_opaq_name *name, uint32_t prefix_opts)
110121
{
122+
const struct lys_module *mod;
123+
const char *ns_prefix;
124+
111125
switch (format) {
112126
case LY_VALUE_XML:
113127
if (name->module_ns) {
@@ -116,10 +130,12 @@ xml_print_ns_opaq(struct xmlpr_ctx *pctx, LY_VALUE_FORMAT format, const struct l
116130
break;
117131
case LY_VALUE_JSON:
118132
if (name->module_name) {
119-
const struct lys_module *mod = ly_ctx_get_module_latest(pctx->ctx, name->module_name);
120-
133+
mod = ly_ctx_get_module_latest(pctx->ctx, name->module_name);
121134
if (mod) {
122-
return xml_print_ns(pctx, mod->ns, (prefix_opts & LYXML_PREFIX_DEFAULT) ? NULL : name->prefix, prefix_opts);
135+
/* do not use the JSON prefix (module name) but YANG module prefix instead, if any */
136+
ns_prefix = (name->prefix && !(prefix_opts & LYXML_PREFIX_DEFAULT)) ? mod->prefix : NULL;
137+
138+
return xml_print_ns(pctx, mod->ns, ns_prefix, prefix_opts);
123139
}
124140
}
125141
break;
@@ -268,13 +284,19 @@ xml_print_node_open(struct xmlpr_ctx *pctx, const struct lyd_node *node)
268284
xml_print_meta(pctx, node);
269285
}
270286

287+
/**
288+
* @brief Print attributes of an opaque node.
289+
*
290+
* @param[in] pctx XML printer context.
291+
* @param[in] attr First attribute in a list to print.
292+
* @return LY_ERR value.
293+
*/
271294
static LY_ERR
272-
xml_print_attr(struct xmlpr_ctx *pctx, const struct lyd_node_opaq *node)
295+
xml_print_attr(struct xmlpr_ctx *pctx, const struct lyd_attr *attr)
273296
{
274-
const struct lyd_attr *attr;
275297
const char *pref;
276298

277-
LY_LIST_FOR(node->attr, attr) {
299+
LY_LIST_FOR(attr, attr) {
278300
pref = NULL;
279301
if (attr->name.prefix) {
280302
/* print attribute namespace */
@@ -290,7 +312,6 @@ xml_print_attr(struct xmlpr_ctx *pctx, const struct lyd_node_opaq *node)
290312
ly_print_(pctx->out, " %s%s%s=\"", pref ? pref : "", pref ? ":" : "", attr->name.name);
291313
lyxml_dump_text(pctx->out, attr->value, 1);
292314
ly_print_(pctx->out, "\""); /* print attribute value terminator */
293-
294315
}
295316

296317
return LY_SUCCESS;
@@ -308,13 +329,11 @@ xml_print_opaq_open(struct xmlpr_ctx *pctx, const struct lyd_node_opaq *node)
308329
}
309330

310331
/* print attributes */
311-
LY_CHECK_RET(xml_print_attr(pctx, node));
332+
LY_CHECK_RET(xml_print_attr(pctx, node->attr));
312333

313334
return LY_SUCCESS;
314335
}
315336

316-
static LY_ERR xml_print_node(struct xmlpr_ctx *pctx, const struct lyd_node *node);
317-
318337
/**
319338
* @brief Print XML element representing lyd_node_term.
320339
*

0 commit comments

Comments
 (0)