libxml2复制不带名称空间的节点或替换/删除名称空间 [英] libxml2 Copy node without namespace or replace/remove namespace

查看:113
本文介绍了libxml2复制不带名称空间的节点或替换/删除名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些xml

<node xmlns="path/to/namespace">
  <grammar prop1="bla-bla-bla" prop2="etc">
    <!-- ... -->
  </grammar>
</node>

,我想获取语法标签,设置我的命名空间并保存所有属性(prop1,prop2),子节点等等.我只是转到语法标签,然后调用 xmlNodePtr copyed = xmlCopyNode(node,1); .之后,我删除一些属性,添加新属性,以此类推(以复制形式).之后,我想将名称空间"/path/to/namespace" 替换为"/path/to/namespace2" .没有像 xmlRemoveNs xmlReplaceNs 这样的功能,所以我只是释放命名空间并设置新的内容.

and I want to get grammar-tag, set my namespace and save all properties(prop1, prop2), children nodes and so on. I just move to grammar-tag and call xmlNodePtr copied = xmlCopyNode(node, 1);. After that I remove some properties, add new and so on(in copied). After that I want to replace namespace "/path/to/namespace" to "/path/to/namespace2". There is no function like xmlRemoveNs or xmlReplaceNs, so I just free namespace and set new.

if (copied->ns)
{
  xmlFree((void*)copied->ns->href);
  copied->ns->href = xmlStrdup((const xmlChar *)"/path/to/namespace2");
}

但是看起来很奇怪,有点可怕.是否可以替换名称空间,不使用名称空间复制或删除名称空间并设置新名称?

but it looks weird and a little awful. Is there way to replace namespace, copy without namespace or delete namespace and set new?

推荐答案

函数 xmlFree()仅释放某些库函数分配的内存,而这不是您要查找的内容.

The function xmlFree() only free the memory allocated by some library function and that is not what are you searching for.

尝试使用例如 xmlSetNsProp():

xmlAttrPtr xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar * name, const xmlChar * value)

设置(或重置)节点携带的属性.ns结构必须在范围内,未选中

Set (or reset) an attribute carried by a node. The ns structure must be in scope, this is not checked

节点:节点

ns:名称空间定义

名称:属性名称

value:属性值

返回:属性指针.

您将在此处找到更多信息: http://xmlsoft.org/html/libxml-tree.html 我认为您可以找到最适合您需求的功能.

You will find more information here: http://xmlsoft.org/html/libxml-tree.html and I think you can find the function that best suits your needs.

在源代码中,似乎名称空间旨在用作 ns-> href :

In the source code it seems that the namespace is intended as ns->href :

/**
 * xmlSetNsProp:
 * @node:  the node
 * @ns:  the namespace definition
 * @name:  the attribute name
 * @value:  the attribute value
 *
 * Set (or reset) an attribute carried by a node.
 * The ns structure must be in scope, this is not checked
 *
 * Returns the attribute pointer.
 */
xmlAttrPtr xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, const xmlChar *value)
    {
    xmlAttrPtr prop;

    if(ns && (ns->href == NULL))
        return (NULL);
    prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
    if(prop != NULL)
        {
        /*
         * Modify the attribute's value.
         */
        if(prop->atype == XML_ATTRIBUTE_ID)
            {
            xmlRemoveID(node->doc, prop);
            prop->atype = XML_ATTRIBUTE_ID;
            }
        if(prop->children != NULL)
            xmlFreeNodeList(prop->children);
        prop->children = NULL;
        prop->last = NULL;
        prop->ns = ns;
        if(value != NULL)
            {
            xmlNodePtr tmp;

            if(!xmlCheckUTF8(value))
                {
                xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr)node->doc,
                NULL);
            if (node->doc != NULL)
            node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
            }
        prop->children = xmlNewDocText(node->doc, value);
        prop->last = NULL;
        tmp = prop->children;
        while(tmp != NULL)
            {
            tmp->parent = (xmlNodePtr)prop;
            if(tmp->next == NULL)
                prop->last = tmp;
            tmp = tmp->next;
            }
        }
    if(prop->atype == XML_ATTRIBUTE_ID)
        xmlAddID(NULL, node->doc, value, prop);
    return (prop);
    }
/*
 * No equal attr found; create a new one.
 */
return (xmlNewPropInternal(node, ns, name, value, 0));
}

这篇关于libxml2复制不带名称空间的节点或替换/删除名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆