Community articleGetAttribute 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 value of an attribute in a node. For example, in the XFDL fragment shown, the mimedata node has an attribute called encoding with the value base64.
   <mimedata encoding="base64"></mimedata>


Function

   Function GetAttribute(
      theNamespaceURI As String, 
      theAttribute As String
      ) As String


Parameters

Table 1. Function parameters
ExpressionTypeDescription
theNamespaceURIStringThe namespace URI for the attribute. For example:
http://www.ibm.com/xmlns/prod/XFDL/7.7

theAttributeStringThe local name of the attribute. For example, encoding.


Returns

The attribute's value or throws an exception if an error occurs. If the attribute is empty or does not exist, the function returns null.

Usage details

Namespaces
If you refer to an attribute with a namespace prefix, getAttribute first looks for a complete match, including both prefix and attribute name. If it does not find a match, it will look for a matching attribute name that has no prefix but whose containing element has the same namespace.
For example, assume that the custom namespace and the test namespace both resolve to the same URI. In the following case, looking for the id attribute would locate the second attribute (test:id), since it has an explicit namespace declaration:
   <a xmlns:custom="ABC" xmlns:test="ABC">
      <custom:myElement id="1" test:id="2">
   </a>

However, in the next case, the id attribute does not have an explicit namespace declaration. Instead, it inherits the custom namespace. However, since the inherited namespace resolves to the same URI, the id attribute is still located:
   <custom:myElement id="1">

Special attributes
Forms generally use three special attributes that are not in an explicitly defined namespace and which require special commands to retrieve.
The first is the default namespace attribute, which looks like this:
   xmlns="http://www.ibm.com/xmlns/prod/XFDL/7.7"

To retrieve this attribute, you must use a namespace URI of null and the attribute name xmlns.
The second special attribute is a namespace declaration, which looks like this:
   xmlns:custom="http://www.ibm.com/xmlns/prod/XFDL/Custom"

To retrieve this sort of attribute, you must use the namespace URI http://www.w3.org/2000/xmlns and the appropriate attribute name, such as custom.
Finally, there is the language attribute, which looks like this:
   xml:lang="en-GB"

To retrieve this sort of attribute, you must use the namespace URI http://www.w3.org/XML/1998/namespace and the attribute name lang.

Example

The following function uses DereferenceEx to locate a node that contains an attachment. It then calls GetAttribute to retrieve the encoding type used, and returns that value.
   Function GetEncodingType(Form)
 
      Dim TempNode  ' object
      
      Set TempNode = Form
      
      ' Locate the mimedata option for the DATA1 node in PAGE3, which
      ' contains an attachment.
      
      Set TempNode = TempNode.DereferenceEx(vbNullString, _
         "PAGE3.DATA1.mimedata", 0, UFL_OPTION_REFERENCE, Nothing)
      
      ' Get the encoding type of the data node.
      
      GetEncodingType = TempNode.GetAttribute( _
         "http://www.ibm.com/xmlns/prod/XFDL/7.7", "encoding")
      
   End Function


Parent topic:
FormNodeP functions