* xmlNewNs: * @node: the element carrying the namespace (optional) * @href: the URI associated * @prefix: the prefix for the namespace (optional) * * Create a new namespace. For a default namespace, @prefix should be * NULL. The namespace URI in @href is not checked. You should make sure * to pass a valid URI. * * If @node is provided, it must be an element node. The namespace will *
| 719 | * the prefix is already in use or a memory allocation failed. |
| 720 | */ |
| 721 | xmlNsPtr |
| 722 | xmlNewNs(xmlNodePtr node, const xmlChar *href, const xmlChar *prefix) { |
| 723 | xmlNsPtr cur; |
| 724 | |
| 725 | if ((node != NULL) && (node->type != XML_ELEMENT_NODE)) |
| 726 | return(NULL); |
| 727 | |
| 728 | /* |
| 729 | * Allocate a new Namespace and fill the fields. |
| 730 | */ |
| 731 | cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs)); |
| 732 | if (cur == NULL) |
| 733 | return(NULL); |
| 734 | memset(cur, 0, sizeof(xmlNs)); |
| 735 | cur->type = XML_LOCAL_NAMESPACE; |
| 736 | |
| 737 | if (href != NULL) { |
| 738 | cur->href = xmlStrdup(href); |
| 739 | if (cur->href == NULL) |
| 740 | goto error; |
| 741 | } |
| 742 | if (prefix != NULL) { |
| 743 | cur->prefix = xmlStrdup(prefix); |
| 744 | if (cur->prefix == NULL) |
| 745 | goto error; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Add it at the end to preserve parsing order ... |
| 750 | * and checks for existing use of the prefix |
| 751 | */ |
| 752 | if (node != NULL) { |
| 753 | if (node->nsDef == NULL) { |
| 754 | node->nsDef = cur; |
| 755 | } else { |
| 756 | xmlNsPtr prev = node->nsDef; |
| 757 | |
| 758 | if ((xmlStrEqual(prev->prefix, cur->prefix)) && |
| 759 | (prev->href != NULL)) |
| 760 | goto error; |
| 761 | while (prev->next != NULL) { |
| 762 | prev = prev->next; |
| 763 | if ((xmlStrEqual(prev->prefix, cur->prefix)) && |
| 764 | (prev->href != NULL)) |
| 765 | goto error; |
| 766 | } |
| 767 | prev->next = cur; |
| 768 | } |
| 769 | } |
| 770 | return(cur); |
| 771 | |
| 772 | error: |
| 773 | xmlFreeNs(cur); |
| 774 | return(NULL); |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * xmlSetNs: |
no test coverage detected