function UCTCF_AddItemDeclarationToCmpLib(&$CACFconstants, &$UCTCFconstants, &$CmpLib, &$itemListById, &$itemListByName, $itemType, SimpleXMLElement &$newXmlNode, SimpleXMLElement &$parentXmlNode, $newItemName, $newItemId)
{
    //  echo "Hello from UCTCF_AddItemDeclarationToCmpLib()\n";
    //  echo "In UCTCF_AddItemDeclarationToCmpLib(), newItemName is \"$newItemName\", newItemId is \"$newItemId\", itemType is \"$itemType\".\n";
    //  echo "In UCTCF_AddItemDeclarationToCmpLib(), itemListById is:\n";
    //  print_r($itemListById);
    /* Add new item to $itemListById array. */
    if ($itemType == "TModelLink") {
        $itemListById[$newItemId] = array("HRID" => $newItemName);
    } else {
        $itemListById[$newItemId] = $newItemName;
    }
    //  echo "In UCTCF_AddItemDeclarationToCmpLib(), after adding, itemListById is now:\n";
    //  print_r($itemListById);
    /* Choose a special comparison function when dealing with TModelLink. */
    if ($itemType == "TModelLink") {
        $compareFunc = "UCTCF_CompareByHRID";
    } else {
        $compareFunc = "strcasecmp";
    }
    /* Sort $itemListById array using non-case-sensitive sort. */
    /* Note:  Here we are using uasort() (with strcasecmp() as comparison func) to try to emulate what Altium does with ordering CmpLib href id references. */
    $rc = uasort($itemListById, $compareFunc);
    if ($rc == FALSE) {
        my_die("uasort() failed!");
    }
    /** Insert new XML node into parent XML tag. **/
    /* State the beginning of the xpath query. */
    $xpathQuery = "/TComponentSet";
    /* Construct the xpath prefix corresponding to our different item types. */
    if ($itemType == "TRequiredParameter") {
        $xpathQuery = $xpathQuery . "/RequiredParameters/TRequiredParameter";
    } else {
        if ($itemType == "TRequiredModel") {
            $xpathQuery = $xpathQuery . "/RequiredModels/TRequiredModel";
        } else {
            if ($itemType == "TModelLink") {
                $xpathQuery = $xpathQuery . "/ModelLinks/TModelLink";
            } else {
                my_die("Unsupported itemType \"{$itemType}\"!");
            }
        }
    }
    /* Flag that we have not yet seen a valid node. */
    $insertAfter = false;
    /* Loop over all item names. */
    foreach ($itemListById as $id => $itemName) {
        /* Special handling for dealing with TModelLink. */
        if ($itemType == "TModelLink") {
            $itemName = (string) $itemListById[$id]["HRID"];
        }
        //      echo "In UCTCF_AddItemDeclarationToCmpLib(), id is $id, itemName is $itemName.\n";
        /* See if this user parameter name already exists. */
        if (isset($itemListByName[$itemName])) {
            /* Flag that we now have a valid insertAfter node for future reference. */
            $insertAfter = $itemName;
        } else {
            echo "Need to insert item name \"{$itemName}\".\n";
            /* If we have already seen a valid XML node, then we can proceed to do the insert. */
            if (!is_bool($insertAfter)) {
                /** Get the XML node corresponding to the itemName stored in $insertAfter. **/
                /* Finish constructing xpath query. */
                $xpathQuery = $xpathQuery . "[HRID=\"{$insertAfter}\"]";
                //              echo "xpathQuery is \"$xpathQuery\".\n";
                /* Run the xpath query to get the TRequiredParameter node in question. */
                UCTCF_GetXmlNodeFromXpathQuery($CmpLib, $xpathQuery, $resultNode);
                $insertAfterNode =& $resultNode;
                //              echo "insertAfterNode is:\n";
                //              print_r($insertAfterNode);
                echo "Attempting to do simplexml_insert_after() operation!\n";
                $parentXmlNode->insertNodeAfterSpecifiedNode($newXmlNode, $insertAfterNode);
            } else {
                //              echo "Attempting to insert new first node!\n";
                $parentXmlNode->insertNodeFirst($newXmlNode);
            }
            /* endelse */
        }
        /* endelse */
    }
    /* end foreach */
    //  echo "parent node is now:\n";
    //  print_r($parentXmlNode);
    //  echo "var dump of parent node:\n";
    //  var_dump($parentXmlNode);
    //  echo "In UCTCF_AddItemDeclarationToCmpLib(), itemListByName is:\n";
    //  print_r($itemListByName);
    /* Add entry for new item to $itemListByName array. */
    $itemListByName[$newItemName] = array();
    $itemListByName[$newItemName]['id'] = $newItemId;
    //  echo "In UCTCF_AddItemDeclarationToCmpLib(), after adding, itemListByName is now:\n";
    //  print_r($itemListByName);
}