JavaScript method: XML.getPrefix()

This method returns a string representing the namespace value of the current element or attribute. It requires you use the other XML get methods to navigate through an XML document.

Syntax

XML.getPrefix();

Arguments

There are no arguments for this method.

Return values

A string or null.

The method returns a string representing the namespace value for the current node (element or attribute) or returns null if the current node has no namespace value.

Example

This example displays the namespace value of the elements and attributes in an XML document.

This example requires the following sample data:

  • A local XML file (for example, a file called "C:\namespace.xml" containing <?xml version="1.0" encoding="UTF-8" standalone="yes" ?><document xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns="http://servicemanager.hp.com/PWS"><http:element1 /><ns:element2 /></document>
/* Create variables to store XML objects and strings.
*  Use XMLObject to store a valid XML object such as an XML file.
*  Use sourceXMLString to store a valid XML string.
*  The script sets the content of XMLString to sourceXMLString.
*  The script assigns values to the empty variables later on */
var XMLFile = new XML();
XMLFile = XMLFile.setContent( "C:\\namespace.xml", true );
var XMLSource;
var TargetNode;
var searchResults;
var prefixResults;
var firstAttribute;
var attributeResults;

/* Create a function that searches an XML object (node) for a particular
*  element (targetElem) and returns an XML object containing the element.
*  This function assumes that there is only one instance of the target
*  element in the source data. If it finds more than one element in the
*  data it only returns the first instance of the element. */
function findTargetElement( node, targetElem )
{
 var topNodeName = node.getNodeName();
 while (node != null && node.getNodeName() != targetElem )
 {
  var childNode = node.getFirstChildElement();
  if (childNode == null)
  {
   childNode = node.getNextSiblingElement();
   while (childNode == null)
   {
    node = node.getParentNode();
    if ( node == null || topNodeName == node.getNodeName() )
    {
     return null;
    }
    childNode = node.getNextSiblingElement();
   }
   node = childNode;
  }
  else
  {
   node = childNode;
  }
 }
 return node;
}

/* Create a function to print the target element and search results */
function printFindResults( searchResult, targetNode )
{
 print( "The target element we are looking for is: " + targetNode );
 if ( searchResult != null )
 {
  var elementName = searchResult.getNodeName();
  print( "Found element " + elementName + " in: \n" + searchResult + "\n" );
  findPrefix( searchResult, targetNode );
  return searchResult
 }
 else
 {
  print( "Cannot find " + target );
  return null
 }
}

/* Create a function to find the parent node element of the target element */
function findPrefix( sourceObject, targetNode )
{
 var prefixFound = sourceObject.getPrefix();
 if ( prefixFound.length >= 1 )
 {
  print( "The namespace prefix of " + targetNode + " is: " + prefixFound );
  return prefixFound
 }
 else
 {
  print( "There is no namespace prefix of " + targetNode );
  prefixFound = null
  return prefixFound
 }
}

/* Create a function to find the first attribute of the target element */
function findFirstAttribute( sourceObject, targetNode )
{
 var startingNode = findTargetElement( sourceObject, targetNode )
 var firstAttribute = startingNode.getFirstAttribute();
 if ( firstAttribute != null )
 {
  var attributeName = firstAttribute.getNodeName();
  print( "The first attribute of " + targetNode + " is: " + attributeName );
  findPrefix( firstAttribute, targetNode );
  return firstAttribute
 }
 else
 {
  print( "There are no attributes of " + targetNode );
  return null
 }
}

/* Create a function to find the next attribute of the target element */
function findNextAttribute( sourceObject, targetNode )
{
 var startingNode = findTargetElement( sourceObject, targetNode )
 var firstAttribute = startingNode.getFirstAttribute();
 if ( firstAttribute != null )
 {
  var nextAttribute = startingNode.getNextAttribute();
  while ( nextAttribute != null )
  {
   var attributeName = nextAttribute.getNodeName();
   print( "The next attribute of " + targetNode + " is: " + attributeName );
   findPrefix( nextAttribute, targetNode );
   nextAttribute = nextAttribute.getNextAttribute();
  }
  print( "There are no more attributes of " + targetNode );
  return targetNode
 }
 else
 {
  return null
 }
}

/* Set variable values for XMLFile and TargetNode. Run functions.  */
print( "Processing XML from XMLFile... \n" );
TargetNode = "element1"
XMLSource = XMLFile
searchResults = findTargetElement( XMLSource, TargetNode );
printFindResults( searchResults, TargetNode );

/* Set variable values for XMLFile and TargetNode. Run functions.  */
print( "Processing XML from XMLFile... \n" );
TargetNode = "document"
XMLSource = XMLFile
searchResults = findTargetElement( XMLSource, TargetNode );
printFindResults( searchResults, TargetNode );
findFirstAttribute( XMLSource, TargetNode );
findNextAttribute( XMLSource, TargetNode );