Community articleUFLGetNamespaceURIFromPrefix function
Added by IBM contributorIBM on July 26, 2013
Rate this article 1 starsRate this article 2 starsRate this article 3 starsRate this article 4 starsRate this article 5 stars

Description

This function returns the namespace URI that corresponds to a specific prefix.
Each namespace is defined in the form by a namespace declaration, as shown:
   xmlns:xfdl="http://www.ibm.com/xmlns/prod/XFDL/7.5" 
   xmlns:custom="http://www.ibm.com/xmlns/prod/XFDL/Custom"

Each namespace declaration defines both a prefix and a URI for the namespace. In this sample, the prefix for the XFDL namespace is xfdl and the URI is http://www.ibm.com/xmlns/prod/XFDL/7.5.
Tags within the form are assigned specific namespaces by using the defined prefix. For example, to declare that an option was in the custom namespace you would use the prefix custom as shown:
   <field sid="testField">
      <custom:custom_option>value</custom:custom_option>
   </field>


Function

   r_error UFLGetNamespaceURIFromPrefix(
      formNodeP theFormNode,
      r_charP thePrefix,
      r_charP *theURI
   );


Parameters

Table 1. Function parameters
ExpressionTypeDescription
theFormNode formNodePAny node in the form that either declares or inherits the namespace in question.
thePrefixr_charPThe namespace prefix. For example, xfdl.
theURI r_charP*A pointer that will contain the namespace URI. For example:
   http://www.ibm.com/xmlns/prod/XFDL/7.5

If the namespace URI is not declared, the pointer is set to NULL. Note that this string must be freed by the caller.


Returns

OK on success or an error code on failure.

Example

The following function copies a custom option from one form to another. The function assumes that you know the prefix for the custom namespace, but not the URI. First, the function uses UFLGetNamespaceURIFromPrefix to get the URI for the custom namespace in the first form. Next, it adds that namespace to the second form as a globally available namespace. It then locates the custom node in the first form and the global item node in the second form. Finally, it copies the custom node to the second form as a child of the global item node.
   r_error copyCustomInfo(formNodeP form1, formNodeP form2)
   {
      formNodeP tempNode, duplicateNode, globalNode;
      r_charP theURI;
 
      /* Get the URI for the custom namespace in form 1. */
 
      if (UFLGetNamespaceURIFromPrefix(form1, "custom", &theURI) != OK)
      {
         fprintf(stderr, "Could not get URI.");
         return(NOTOK);
      }
 
      /* Check to see if the URI is NULL. */
 
      if (theURI == NULL)
      {
         fprintf(stderr, "Could not get namespace URI.");
         return(NOTOK);
      }
 
      /* Create a custom namespace in form 2 using that URI. */
 
      if (UFLAddNamespace(form2, theURI, "custom") != OK)
      {
         fprintf(stderr, "Could not add namespace to second form.");
         return(NOTOK);
      }
 
      /* Locate the custom Status node in form 1. */
 
      if ((tempNode = UFLDereferenceEx(form1, NULL, 
         "global.global.custom:Status", 0, UFL_OPTION_REFERENCE | 
         UFL_SEARCH, NULL)) == NULL)
      {
         fprintf(stderr, "Could not locate custom Status node.");
         return(NOTOK);
      }
 
      /* Locate the global item in form 2. */
 
      if ((globalNode = UFLDereferenceEx(form2, NULL, "global.global", 0, 
         UFL_ITEM_REFERENCE | UFL_SEARCH, NULL)) == NULL)
      {
         fprintf(stderr, "Could not locate global item in second form.");
         return(NOTOK);
      }
 
      /* Copy the custom node from form 1 and insert it as a child of 
         the global item in form 2. */
 
      if ((duplicateNode = UFLDuplicate(tempNode, globalNode, 
         UFL_APPEND_CHILD, NULL)) == NULL)
      {
         fprintf(stderr, "Could not duplicate node.");
         return(NOTOK);
      }
 
      /* Free memory. */
 
      cp_free(theURI);
 
      return(OK);
   }


Parent topic:
FormNodeP functions