Community articleGetPrefixFromNamespaceURI 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 prefix for a specific namespace URI.You can call this function from any node in the form, as long as that node either declares or inherits the namespace in question.
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 is in the custom namespace, use the prefix custom as shown:
   <field sid="testField">
      <custom:custom_option>value</custom:custom_option>
   </field>


Function

   Function GetPrefixFromNamespaceURI(
      theURI As String
      ) As String


Parameters

Table 1. Function parameters
ExpressionTypeDescription
theURIStringThe namespace URI. For example:
http://www.ibm.com/xmlns/prod/XFDL/7.5



Returns

The namespace prefix or throws an exception if an error occurs. If the namespace URI is not declared, the result is null.

Example

The following function adds some custom user information to the form, but assumes that the prefix used for the custom namespace is unknown. The function first uses GetPrefixFromNamespaceURI to determine which prefix is used for the custom namespace. It then concatenates that prefix with the tag ":User" to create the name for a new node. Next, it uses DereferenceEx to locate the global item in the global page. Finally, it uses Create to add a custom option called "User" to the form.
   Sub AddUserInfo(Form, UserName)
 
      Dim XFDL, TempNode  ' objects
      Dim ThePrefix, NewName  ' Strings
 
      Set TempNode = Form
 
      ' Retrieve the prefix for the custom namespace
 
      ThePrefix = TempNode.GetPrefixFromNamespaceURI( _
         "http://www.ibm.com/xmlns/prod/XFDL/Custom")
 
      ' Create a name for a new node by concatenating the prefix with ":User"
      NewName = ThePrefix & ":User"
      ' Locate the global item in the global page so we can add a global
      ' option
 
      Set TempNode = TempNode.DereferenceEx(vbNullString, "global.global", _
         0, UFL_ITEM_REFERENCE, Nothing)
 
      ' Create an XFDL object so we can use the create function
 
      Set XFDL = CreateObject("PureEdge.xfdl_XFDL")
 
      ' Create new node in the custom namespace that represents the user of
      ' the form and give it a value of "TJones"
 
      Set TempNode = XFDL.Create(TempNode, UFL_APPEND_CHILD, vbNullString, _
         UserName , vbNullString, NewName)
 
   End Sub


Parent topic:
FormNodeP functions