Logo Search packages:      
Sourcecode: libupnp4 version File versions  Download package

EXPORT_SPEC int ixmlElement_setAttributeNS ( IXML_Element element,
const DOMString  namespaceURI,
const DOMString  qualifiedName,
const DOMString  value 
)

Adds a new attribute to an Element using the local name and namespace URI.

If another attribute matches the same local name and namespace, the prefix is changed to be the prefix part of the qualifiedName and the value is changed to value.

Returns:
An integer representing one of the following:
  • IXML_SUCCESS: The operation completed successfully.
  • IXML_INVALID_PARAMETER: Either element, namespaceURI, qualifiedName, or value is NULL.
  • IXML_INVALID_CHARACTER_ERR: qualifiedName contains an invalid character.
  • IXML_NAMESPACE_ERR: Either the qualifiedName or namespaceURI is malformed. Refer to the DOM2-Core for possible reasons.
  • IXML_INSUFFICIENT_MEMORY: Not enough free memory exist to complete the operation.
  • IXML_FAILED: The operation could not be completed.
Parameters:
element  [in] The Element on which to set the attribute.
namespaceURI  [in] The namespace URI of the new attribute.
qualifiedName  [in] The qualified name of the attribute.
value  [in] The new value for the attribute.

Definition at line 411 of file element.c.

References _IXML_Node::firstAttr, ixmlAttr_free(), ixmlDocument_createAttributeNSEx(), ixmlElement_setAttributeNodeNS(), ixmlNode_init(), _IXML_Node::localName, _IXML_ATTR::n, _IXML_Element::n, _IXML_Node::namespaceURI, _IXML_Node::nextSibling, _IXML_Node::nodeName, _IXML_Node::nodeValue, _IXML_Node::ownerDocument, Parser_freeNodeContent(), Parser_isValidXmlName(), Parser_setNodePrefixAndLocalName(), and _IXML_Node::prefix.

{
      IXML_Node *attrNode = NULL;
      IXML_Node newAttrNode;
      IXML_Attr *newAttr;
      int rc;

      if (element == NULL || namespaceURI == NULL || qualifiedName == NULL ||
          value == NULL) {
            return IXML_INVALID_PARAMETER;
      }

      if (Parser_isValidXmlName(qualifiedName) == FALSE) {
            return IXML_INVALID_CHARACTER_ERR;
      }

      ixmlNode_init(&newAttrNode);
      newAttrNode.nodeName = strdup(qualifiedName);
      if (newAttrNode.nodeName == NULL) {
            return IXML_INSUFFICIENT_MEMORY;
      }

      rc = Parser_setNodePrefixAndLocalName(&newAttrNode);
      if (rc != IXML_SUCCESS) {
            Parser_freeNodeContent(&newAttrNode);
            return rc;
      }

      /* see DOM 2 spec page 59 */
      if ((newAttrNode.prefix != NULL && namespaceURI == NULL) ||
          (strcmp(newAttrNode.prefix, "xml") == 0 &&
           strcmp(namespaceURI, "http://www.w3.org/XML/1998/namespace") != 0) ||
          (strcmp(qualifiedName, "xmlns") == 0 &&
           strcmp(namespaceURI, "http://www.w3.org/2000/xmlns/") != 0)) {
            Parser_freeNodeContent( &newAttrNode );
            return IXML_NAMESPACE_ERR;
      }

      attrNode = element->n.firstAttr;
      while (attrNode != NULL) {
            if (strcmp(attrNode->localName, newAttrNode.localName) == 0 &&
                strcmp(attrNode->namespaceURI, namespaceURI) == 0) {
                  /* Found it */
                  break;
            } else {
                  attrNode = attrNode->nextSibling;
            }
      }
      if (attrNode != NULL) {
            if (attrNode->prefix != NULL) {
                  /* Remove the old prefix */
                  free(attrNode->prefix);
            }
            /* replace it with the new prefix */
            attrNode->prefix = strdup( newAttrNode.prefix );
            if (attrNode->prefix == NULL) {
                  Parser_freeNodeContent(&newAttrNode);
                  return IXML_INSUFFICIENT_MEMORY;
            }

            if (attrNode->nodeValue != NULL) {
                  free(attrNode->nodeValue);
            }
            attrNode->nodeValue = strdup(value);
            if (attrNode->nodeValue == NULL) {
                  free(attrNode->prefix);
                  Parser_freeNodeContent(&newAttrNode);
                  return IXML_INSUFFICIENT_MEMORY;
            }
      } else {
            /* Add a new attribute */
            rc = ixmlDocument_createAttributeNSEx(
                  (IXML_Document *)element->n.ownerDocument,
                  namespaceURI,
                  qualifiedName,
                  &newAttr);
            if (rc != IXML_SUCCESS) {
                  return rc;
            }
            newAttr->n.nodeValue = strdup(value);
            if (newAttr->n.nodeValue == NULL) {
                  ixmlAttr_free(newAttr);
                  return IXML_INSUFFICIENT_MEMORY;
            }
            if (ixmlElement_setAttributeNodeNS(element, newAttr, NULL) != IXML_SUCCESS) {
                  ixmlAttr_free(newAttr);
                  return IXML_FAILED;
            }
      }
      Parser_freeNodeContent(&newAttrNode);

      return IXML_SUCCESS;
}


Generated by  Doxygen 1.6.0   Back to index