function generate_nuds($row)
{
    global $stylesheet;
    global $deities;
    $recordId = 'price.' . $row['Price no.'];
    if ($row['Material'] != 'vacat') {
        $doc = new XMLWriter();
        //$doc->openUri('php://output');
        $doc->openUri('nuds/' . $recordId . '.xml');
        $doc->setIndent(true);
        //now we need to define our Indent string,which is basically how many blank spaces we want to have for the indent
        $doc->setIndentString("    ");
        $doc->startDocument('1.0', 'UTF-8');
        $doc->startElement('nuds');
        $doc->writeAttribute('xmlns', 'http://nomisma.org/nuds');
        $doc->writeAttribute('xmlns:xs', 'http://www.w3.org/2001/XMLSchema');
        $doc->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
        $doc->writeAttribute('recordType', 'conceptual');
        //control
        $doc->startElement('control');
        $doc->writeElement('recordId', $recordId);
        $doc->writeElement('publicationStatus', 'approved');
        $doc->startElement('maintenanceAgency');
        $doc->writeElement('agencyName', 'American Numismatic Society');
        $doc->endElement();
        $doc->writeElement('maintenanceStatus', 'derived');
        //maintenanceHistory
        $doc->startElement('maintenanceHistory');
        $doc->startElement('maintenanceEvent');
        $doc->writeElement('eventType', 'derived');
        $doc->startElement('eventDateTime');
        $doc->writeAttribute('standardDateTime', date(DATE_W3C));
        $doc->text(date(DATE_RFC2822));
        $doc->endElement();
        $doc->writeElement('agentType', 'machine');
        $doc->writeElement('agent', 'PHP');
        $doc->writeElement('eventDescription', 'Generated from CSV fro Google Drive.');
        $doc->endElement();
        $doc->endElement();
        //semanticDeclaration
        $doc->startElement('semanticDeclaration');
        $doc->writeElement('prefix', 'dcterms');
        $doc->writeElement('namespace', 'http://purl.org/dc/terms/');
        $doc->endElement();
        $doc->startElement('semanticDeclaration');
        $doc->writeElement('prefix', 'nmo');
        $doc->writeElement('namespace', 'http://nomisma.org/ontology#');
        $doc->endElement();
        //rightsStmt
        $doc->startElement('rightsStmt');
        $doc->writeElement('copyrightHolder', 'American Numismatic Society');
        $doc->startElement('license');
        $doc->writeAttribute('xlink:type', 'simple');
        $doc->writeAttribute('xlink:href', 'http://opendatacommons.org/licenses/odbl/');
        $doc->endElement();
        $doc->endElement();
        $doc->endElement();
        //descMeta
        $doc->startElement('descMeta');
        //generate title
        $title = "Price {$row['Price no.']}";
        $doc->startElement('title');
        $doc->writeAttribute('xml:lang', 'en');
        $doc->text($title);
        $doc->endElement();
        /***** TYPEDESC *****/
        $doc->startElement('typeDesc');
        //objectType
        $doc->startElement('objectType');
        $doc->writeAttribute('xlink:type', 'simple');
        $doc->writeAttribute('xlink:href', 'http://nomisma.org/id/coin');
        $doc->text('Coin');
        $doc->endElement();
        //manufacture
        $doc->startElement('manufacture');
        $doc->writeAttribute('xlink:type', 'simple');
        $doc->writeAttribute('xlink:href', 'http://nomisma.org/id/struck');
        $doc->text('Struck');
        $doc->endElement();
        if (strlen($row['Material']) > 0) {
            $vals = explode('|', $row['Material']);
            foreach ($vals as $val) {
                if (substr($val, -1) == '?') {
                    $uri = 'http://nomisma.org/id/' . substr($val, 0, -1);
                    $uncertainty = true;
                    $content = processUri($uri);
                } else {
                    $uri = 'http://nomisma.org/id/' . $val;
                    $uncertainty = false;
                    $content = processUri($uri);
                }
                $doc->startElement($content['element']);
                $doc->writeAttribute('xlink:type', 'simple');
                $doc->writeAttribute('xlink:href', $uri);
                $doc->text($content['label']);
                $doc->endElement();
            }
        }
        if (strlen($row['Denomination']) > 0) {
            $vals = explode('|', $row['Denomination']);
            foreach ($vals as $val) {
                $uri = 'http://nomisma.org/id/' . $val;
                $uncertainty = false;
                $content = processUri($uri);
                $doc->startElement($content['element']);
                $doc->writeAttribute('xlink:type', 'simple');
                $doc->writeAttribute('xlink:href', $uri);
                $doc->text($content['label']);
                $doc->endElement();
            }
        }
        if (is_numeric($row['From Date']) && is_numeric($row['To Date'])) {
            if ($row['From Date'] == $row['To Date']) {
                $doc->startElement('date');
                $doc->writeAttribute('standardDate', number_pad($row['From Date'], 4));
                $doc->text(abs(intval($row['From Date'])) . ' B.C.');
                $doc->endElement();
            } else {
                $doc->startElement('dateRange');
                $doc->startElement('fromDate');
                $doc->writeAttribute('standardDate', number_pad($row['From Date'], 4));
                $doc->text(abs(intval($row['From Date'])) . ' B.C.');
                $doc->endElement();
                $doc->startElement('toDate');
                $doc->writeAttribute('standardDate', number_pad($row['To Date'], 4));
                $doc->text(abs(intval($row['To Date'])) . ' B.C.');
                $doc->endElement();
                $doc->endElement();
            }
        }
        //authority
        if (strlen($row['Authority']) > 0 || strlen($row['Stated authority']) > 0 || strlen($row['Magistrate ID 1']) > 0 || strlen($row['Magistrate ID 2']) > 0) {
            $doc->startElement('authority');
            if (strlen($row['Authority']) > 0) {
                $vals = explode('|', $row['Authority']);
                foreach ($vals as $val) {
                    $uri = 'http://nomisma.org/id/' . $val;
                    $uncertainty = false;
                    $content = processUri($uri);
                    $role = 'authority';
                    $doc->startElement($content['element']);
                    $doc->writeAttribute('xlink:type', 'simple');
                    $doc->writeAttribute('xlink:role', $role);
                    $doc->writeAttribute('xlink:href', $uri);
                    if ($uncertainty == true) {
                        $doc->writeAttribute('certainty', 'uncertain');
                    }
                    $doc->text($content['label']);
                    $doc->endElement();
                }
            }
            /*elseif (strlen($row['Mint ID']) > 0 && $row['Mint ID'] != 'uncertain_value'){
            			$vals = explode('|', $row['Mint ID']);
            			foreach ($vals as $val){
            				$uri = 'http://nomisma.org/id/' . $val;
            				$uncertainty = (strtolower(trim($row['Mint Uncertain'])) == 'true' ? true : false);
            				$content = processUri($uri);
            			
            				$doc->startElement('corpname');
            				$doc->writeAttribute('xlink:type', 'simple');
            				$doc->writeAttribute('xlink:role', 'authority');
            				$doc->writeAttribute('xlink:href', $uri);
            				if($uncertainty == true){
            					$doc->writeAttribute('certainty', 'uncertain');
            				}
            				$doc->text($content['label']);
            				$doc->endElement();
            			} 
            		}*/
            if (strlen($row['Stated authority']) > 0) {
                $vals = explode('|', $row['Stated authority']);
                foreach ($vals as $val) {
                    $uri = 'http://nomisma.org/id/' . $val;
                    $uncertainty = false;
                    $content = processUri($uri);
                    $role = 'statedAuthority';
                    $doc->startElement($content['element']);
                    $doc->writeAttribute('xlink:type', 'simple');
                    $doc->writeAttribute('xlink:role', $role);
                    $doc->writeAttribute('xlink:href', $uri);
                    if ($uncertainty == true) {
                        $doc->writeAttribute('certainty', 'uncertain');
                    }
                    $doc->text($content['label']);
                    $doc->endElement();
                }
            }
            //magistrates
            if (strlen($row['Magistrate ID 1']) > 0) {
                $vals = explode('|', $row['Magistrate ID 1']);
                foreach ($vals as $val) {
                    $uri = 'http://nomisma.org/id/' . $val;
                    $uncertainty = false;
                    $content = processUri($uri);
                    $role = 'issuer';
                    $doc->startElement($content['element']);
                    $doc->writeAttribute('xlink:type', 'simple');
                    $doc->writeAttribute('xlink:role', $role);
                    $doc->writeAttribute('xlink:href', $uri);
                    if ($uncertainty == true) {
                        $doc->writeAttribute('certainty', 'uncertain');
                    }
                    $doc->text($content['label']);
                    $doc->endElement();
                }
            }
            if (strlen($row['Magistrate ID 2']) > 0) {
                $vals = explode('|', $row['Magistrate ID 2']);
                foreach ($vals as $val) {
                    $uri = 'http://nomisma.org/id/' . $val;
                    $uncertainty = false;
                    $content = processUri($uri);
                    $role = 'issuer';
                    $doc->startElement($content['element']);
                    $doc->writeAttribute('xlink:type', 'simple');
                    $doc->writeAttribute('xlink:role', $role);
                    $doc->writeAttribute('xlink:href', $uri);
                    if ($uncertainty == true) {
                        $doc->writeAttribute('certainty', 'uncertain');
                    }
                    $doc->text($content['label']);
                    $doc->endElement();
                }
            }
            $doc->endElement();
        }
        //geography
        //mint
        if (strlen($row['Mint ID']) > 0 || strlen($row['Region']) > 0) {
            $doc->startElement('geographic');
            if (strlen($row['Mint ID']) > 0) {
                $vals = explode('|', $row['Mint ID']);
                foreach ($vals as $val) {
                    $uri = 'http://nomisma.org/id/' . $val;
                    $uncertainty = strtolower(trim($row['Mint Uncertain'])) == 'true' ? true : false;
                    $content = processUri($uri);
                    $doc->startElement('geogname');
                    $doc->writeAttribute('xlink:type', 'simple');
                    $doc->writeAttribute('xlink:role', 'mint');
                    $doc->writeAttribute('xlink:href', $uri);
                    if ($uncertainty == true) {
                        $doc->writeAttribute('certainty', 'uncertain');
                    }
                    $doc->text($content['label']);
                    $doc->endElement();
                }
                //regions extracted at the point of indexing in Numishare
                /*if (isset($content['parent'])){
                			$orgs[] = $content['parent'];
                			$parentArray = processUri($content['parent']);
                			$role = 'region';
                			$doc->startElement($parentArray['element']);
                				$doc->writeAttribute('xlink:type', 'simple');
                				$doc->writeAttribute('xlink:role', $role);
                				$doc->writeAttribute('xlink:href', $content['parent']);
                				$doc->text($parentArray['label']);
                			$doc->endElement();
                		}*/
            }
            if (strlen($row['Region']) > 0) {
                $vals = explode('|', $row['Region']);
                foreach ($vals as $val) {
                    $uri = 'http://nomisma.org/id/' . $val;
                    $uncertainty = strtolower(trim($row['Region Uncertain'])) == 'true' ? true : false;
                    $content = processUri($uri);
                    $doc->startElement($content['element']);
                    $doc->writeAttribute('xlink:type', 'simple');
                    $doc->writeAttribute('xlink:role', 'region');
                    $doc->writeAttribute('xlink:href', $uri);
                    if ($uncertainty == true) {
                        $doc->writeAttribute('certainty', 'uncertain');
                    }
                    $doc->text($content['label']);
                    $doc->endElement();
                }
            }
            $doc->endElement();
        }
        //obverse
        if (strlen($row['O']) > 0) {
            $doc->startElement('obverse');
            $key = trim($row['O']);
            $type = '';
            $doc->startElement('type');
            foreach ($stylesheet as $desc) {
                if ($desc['Abbreviation'] == $key) {
                    $type = $desc['en'];
                    foreach ($desc as $k => $v) {
                        if ($k != 'Abbreviation') {
                            $doc->startElement('description');
                            $doc->writeAttribute('xml:lang', $k);
                            $doc->text(trim($v));
                            $doc->endElement();
                        }
                    }
                    break;
                }
            }
            $doc->endElement();
            //deity
            foreach ($deities as $deity) {
                if (strstr($deity['name'], ' ') !== FALSE) {
                    //haystack is string when the deity is multiple words
                    $haystack = strtolower(trim($type));
                    if (strstr($haystack, strtolower($deity['matches'])) !== FALSE) {
                        $bm_uri = strlen($deity['bm_uri']) > 0 ? ' xlink:href="' . $deity['bm_uri'] . '"' : '';
                        $doc->startElement('persname');
                        $doc->writeAttribute('xlink:type', 'simple');
                        $doc->writeAttribute('xlink:role', 'deity');
                        if (strlen($deity['bm_uri']) > 0) {
                            $doc->writeAttribute('xlink:href', $deity['bm_uri']);
                        }
                        $doc->text($deity['name']);
                        $doc->endElement();
                    }
                } else {
                    //haystack is array
                    $string = preg_replace('/[^a-z]+/i', ' ', trim($type));
                    $haystack = explode(' ', $string);
                    if (in_array($deity['matches'], $haystack)) {
                        $doc->startElement('persname');
                        $doc->writeAttribute('xlink:type', 'simple');
                        $doc->writeAttribute('xlink:role', 'deity');
                        if (strlen($deity['bm_uri']) > 0) {
                            $doc->writeAttribute('xlink:href', $deity['bm_uri']);
                        }
                        $doc->text($deity['name']);
                        $doc->endElement();
                    }
                }
            }
            //symbols
            if (strlen($row['Ο: to l.']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'leftField');
                $doc->text(trim($row['Ο: to l.']));
                $doc->endElement();
            }
            if (strlen($row['O: to r.']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'rightField');
                $doc->text(trim($row['O: to r.']));
                $doc->endElement();
            }
            if (strlen($row['O:below']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'below');
                $doc->text(trim($row['O:below']));
                $doc->endElement();
            }
            if (strlen($row['O:Scalp']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'scalp');
                $doc->text(trim($row['O:Scalp']));
                $doc->endElement();
            }
            $doc->endElement();
        }
        //reverse
        if (strlen($row['R']) > 0 || strlen($row['R Legend']) > 0) {
            $doc->startElement('reverse');
            $key = trim($row['R']);
            $type = '';
            $doc->startElement('type');
            foreach ($stylesheet as $desc) {
                if ($desc['Abbreviation'] == $key) {
                    $type = $desc['en'];
                    foreach ($desc as $k => $v) {
                        if ($k != 'Abbreviation') {
                            $doc->startElement('description');
                            $doc->writeAttribute('xml:lang', $k);
                            $doc->text(trim($v));
                            $doc->endElement();
                        }
                    }
                    break;
                }
            }
            $doc->endElement();
            //deity
            foreach ($deities as $deity) {
                if (strstr($deity['name'], ' ') !== FALSE) {
                    //haystack is string when the deity is multiple words
                    $haystack = strtolower(trim($type));
                    if (strstr($haystack, strtolower($deity['matches'])) !== FALSE) {
                        $bm_uri = strlen($deity['bm_uri']) > 0 ? ' xlink:href="' . $deity['bm_uri'] . '"' : '';
                        $doc->startElement('persname');
                        $doc->writeAttribute('xlink:type', 'simple');
                        $doc->writeAttribute('xlink:role', 'deity');
                        if (strlen($deity['bm_uri']) > 0) {
                            $doc->writeAttribute('xlink:href', $deity['bm_uri']);
                        }
                        $doc->text($deity['name']);
                        $doc->endElement();
                    }
                } else {
                    //haystack is array
                    $string = preg_replace('/[^a-z]+/i', ' ', trim($type));
                    $haystack = explode(' ', $string);
                    if (in_array($deity['matches'], $haystack)) {
                        $doc->startElement('persname');
                        $doc->writeAttribute('xlink:type', 'simple');
                        $doc->writeAttribute('xlink:role', 'deity');
                        if (strlen($deity['bm_uri']) > 0) {
                            $doc->writeAttribute('xlink:href', $deity['bm_uri']);
                        }
                        $doc->text($deity['name']);
                        $doc->endElement();
                    }
                }
            }
            //legend
            if (strlen($row['R Legend']) > 0) {
                $doc->startElement('legend');
                $doc->writeAttribute('scriptCode', 'Grek');
                $doc->text(trim($row['R Legend']));
                $doc->endElement();
            }
            //symbols
            if (strlen($row['<LF>1']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'leftField');
                $doc->text(trim($row['<LF>1']));
                $doc->endElement();
            }
            if (strlen($row['<LF>2']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'leftField');
                $doc->text(trim($row['<LF>2']));
                $doc->endElement();
            }
            if (strlen($row['<LF>3']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'leftField');
                $doc->text(trim($row['<LF>3']));
                $doc->endElement();
            }
            if (strlen($row['<LF>4']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'leftField');
                $doc->text(trim($row['<LF>4']));
                $doc->endElement();
            }
            if (strlen($row['<TH>1']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'beneathThrone');
                $doc->text(trim($row['<TH>1']));
                $doc->endElement();
            }
            if (strlen($row['<TH>2']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'beneathThrone');
                $doc->text(trim($row['<TH>2']));
                $doc->endElement();
            }
            if (strlen($row['<TH>3']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'beneathThrone');
                $doc->text(trim($row['<TH>3']));
                $doc->endElement();
            }
            if (strlen($row['<EX>1']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'exergue');
                $doc->text(trim($row['<EX>1']));
                $doc->endElement();
            }
            if (strlen($row['<EX>2']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'exergue');
                $doc->text(trim($row['<EX>2']));
                $doc->endElement();
            }
            if (strlen($row['<EX>3']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'exergue');
                $doc->text(trim($row['<EX>3']));
                $doc->endElement();
            }
            if (strlen($row['<RF>1']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'rightField');
                $doc->text(trim($row['<RF>1']));
                $doc->endElement();
            }
            if (strlen($row['<RF>2']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'rightField');
                $doc->text(trim($row['<RF>2']));
                $doc->endElement();
            }
            if (strlen($row['Above1']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'above');
                $doc->text(trim($row['Above1']));
                $doc->endElement();
            }
            if (strlen($row['Above2']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'above');
                $doc->text(trim($row['Above2']));
                $doc->endElement();
            }
            if (strlen($row['Below1']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'below');
                $doc->text(trim($row['Below1']));
                $doc->endElement();
            }
            if (strlen($row['Below2']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'below');
                $doc->text(trim($row['Below2']));
                $doc->endElement();
            }
            if (strlen($row['<LW>1']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'leftWing');
                $doc->text(trim($row['<LW>1']));
                $doc->endElement();
            }
            if (strlen($row['<LW>2']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'leftWing');
                $doc->text(trim($row['<LW>2']));
                $doc->endElement();
            }
            if (strlen($row['<RW>1']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'rightWing');
                $doc->text(trim($row['<RW>1']));
                $doc->endElement();
            }
            if (strlen($row['<RW>2']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'rightWing');
                $doc->text(trim($row['<RW>2']));
                $doc->endElement();
            }
            if (strlen($row['Between']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'between');
                $doc->text(trim($row['Between']));
                $doc->endElement();
            }
            if (strlen($row['Boss']) > 0) {
                $doc->startElement('symbol');
                $doc->writeAttribute('position', 'boss');
                $doc->text(trim($row['Boss']));
                $doc->endElement();
            }
            $doc->endElement();
        }
        //end typeDesc
        $doc->endElement();
        //refDesc
        $doc->startElement('refDesc');
        $doc->startElement('reference');
        $doc->writeAttribute('xlink:type', 'simple');
        $doc->writeAttribute('xlink:href', 'http://nomisma.org/id/price1991');
        $doc->writeAttribute('semantic', 'dcterms:source');
        $doc->text('Price (1991)');
        $doc->endElement();
        $doc->endElement();
        //end descMeta
        $doc->endElement();
        //end nuds
        $doc->endElement();
        $doc->endDocument();
        echo "Writing {$recordId}\n";
    } else {
        "Ignoring vacat: {$recordId}\n";
    }
}
function generate_nuds($row, $files)
{
    $recordId = $row['recordId'];
    $orgs = array();
    //parse references
    if (strlen($row['reference']) > 0) {
        //strip obverse and reverse descriptions out if possible
        preg_match('/(Obverse:?.*)Reverse/', $row['reference'], $obverse);
        preg_match('/(Reverse:?.*)$/', $row['reference'], $reverse);
        if (isset($obverse[1]) || isset($reverse[1])) {
            $refText = trim(str_replace($reverse[1], '', str_replace($obverse[1], '', $row['reference'])));
        } else {
            $refText = trim($row['reference']);
        }
    }
    $doc = new XMLWriter();
    //$doc->openUri('php://output');
    $doc->openUri('nuds/' . $recordId . '.xml');
    $doc->setIndent(true);
    //now we need to define our Indent string,which is basically how many blank spaces we want to have for the indent
    $doc->setIndentString("    ");
    $doc->startDocument('1.0', 'UTF-8');
    $doc->startElement('nuds');
    $doc->writeAttribute('xmlns', 'http://nomisma.org/nuds');
    $doc->writeAttribute('xmlns:xs', 'http://www.w3.org/2001/XMLSchema');
    $doc->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
    $doc->writeAttribute('xmlns:mets', 'http://www.loc.gov/METS/');
    $doc->writeAttribute('recordType', 'physical');
    //control
    $doc->startElement('control');
    $doc->writeElement('recordId', $recordId);
    $doc->writeElement('publicationStatus', 'approved');
    $doc->startElement('maintenanceAgency');
    $doc->writeElement('agencyName', 'American Numismatic Society');
    $doc->endElement();
    $doc->writeElement('maintenanceStatus', 'derived');
    //maintenanceHistory
    $doc->startElement('maintenanceHistory');
    $doc->startElement('maintenanceEvent');
    $doc->writeElement('eventType', 'derived');
    $doc->startElement('eventDateTime');
    $doc->writeAttribute('standardDateTime', date(DATE_W3C));
    $doc->text(date(DATE_RFC2822));
    $doc->endElement();
    $doc->writeElement('agentType', 'machine');
    $doc->writeElement('agent', 'PHP');
    $doc->writeElement('eventDescription', 'Generated from CSV, after iterative cleanup process (including Open Refine)');
    $doc->endElement();
    $doc->endElement();
    //rightsStmt
    $doc->startElement('rightsStmt');
    $doc->writeElement('copyrightHolder', 'American Numismatic Society');
    $doc->startElement('license');
    $doc->writeAttribute('xlink:type', 'simple');
    $doc->writeAttribute('xlink:href', 'http://opendatacommons.org/licenses/odbl/');
    $doc->endElement();
    $doc->endElement();
    $doc->endElement();
    //descMeta
    $doc->startElement('descMeta');
    //generate title
    $objectType = processUri($row['objectType']);
    $auth = array();
    if (strlen($row['authorityPerson']) > 0) {
        $auth[] = processUri($row['authorityPerson']);
        $auth[0]['certain'] = true;
    } else {
        if (strlen($row['org']) > 0) {
            $vals = explode('|', $row['org']);
            $i = 0;
            foreach ($vals as $val) {
                if (substr($val, -1) == '?') {
                    $uri = substr($val, 0, -1);
                    $auth[] = processUri($uri);
                    $auth[$i]['certain'] = false;
                } else {
                    $uri = $val;
                    $auth[] = processUri($uri);
                    $auth[$i]['certain'] = true;
                }
                $i++;
            }
        }
    }
    //english
    $title = $objectType['label'] . ' - ';
    $authFragment = array();
    foreach ($auth as $v) {
        $authFragment[] = $v['label'] . ($v['certain'] == false ? '?' : '');
    }
    $title .= implode('/', $authFragment) . ', ';
    if (strlen($row['dob_ah']) > 0) {
        $title .= 'AH ' . $row['dob_ah'] . ' / ';
    }
    if (strlen($row['fromDate']) > 0 && strlen($row['toDate']) > 0) {
        $title .= 'CE ';
        if ($row['fromDate'] == $row['toDate']) {
            $title .= $row['fromDate'];
        } else {
            $title .= $row['fromDate'] . '-' . $row['toDate'];
        }
    }
    if (strlen($row['dob_ah']) > 0 || strlen($row['fromDate']) > 0 && strlen($row['toDate']) > 0) {
        $title .= '. ';
    }
    $title .= $recordId;
    $doc->startElement('title');
    $doc->writeAttribute('xml:lang', 'en');
    $doc->text($title);
    $doc->endElement();
    //arabic
    $title = $recordId;
    if (strlen($row['dob_ah']) > 0 || strlen($row['fromDate']) > 0 && strlen($row['toDate']) > 0) {
        $title .= ' .';
    }
    if (strlen($row['fromDate']) > 0 && strlen($row['toDate']) > 0) {
        if ($row['fromDate'] == $row['toDate']) {
            $title .= $row['fromDate'];
        } else {
            $title .= $row['toDate'] . '-' . $row['fromDate'];
        }
        $title .= ' CE';
    }
    if (strlen($row['dob_ah']) > 0) {
        $title .= ' \\ ' . $row['dob_ah'];
    }
    $authFragment = array();
    foreach ($auth as $v) {
        $authFragment[] = ($v['certain'] == false ? '؟' : '') . $v['ar'];
    }
    $title .= ' ،' . implode('/', $authFragment);
    $title .= ' - ' . $objectType['ar'];
    $doc->startElement('title');
    $doc->writeAttribute('xml:lang', 'ar');
    $doc->text($title);
    $doc->endElement();
    /***** TYPEDESC *****/
    $doc->startElement('typeDesc');
    //objectType
    if (strlen($row['objectType']) > 0) {
        $vals = explode('|', $row['objectType']);
        foreach ($vals as $val) {
            if (substr($val, -1) == '?') {
                $uri = substr($val, 0, -1);
                $uncertainty = true;
                $content = processUri($uri);
            } else {
                $uri = $val;
                $uncertainty = false;
                $content = processUri($uri);
            }
            $doc->startElement($content['element']);
            $doc->writeAttribute('xlink:type', 'simple');
            $doc->writeAttribute('xlink:href', $uri);
            if ($uncertainty == true) {
                $doc->writeAttribute('certainty', 'uncertain');
            }
            $doc->text($content['label']);
            $doc->endElement();
        }
    }
    if (strlen($row['material']) > 0) {
        $vals = explode('|', $row['material']);
        foreach ($vals as $val) {
            if (substr($val, -1) == '?') {
                $uri = substr($val, 0, -1);
                $uncertainty = true;
                $content = processUri($uri);
            } else {
                $uri = $val;
                $uncertainty = false;
                $content = processUri($uri);
            }
            $doc->startElement($content['element']);
            $doc->writeAttribute('xlink:type', 'simple');
            $doc->writeAttribute('xlink:href', $uri);
            if ($uncertainty == true || $row['materialUncertainty'] == 'true') {
                $doc->writeAttribute('certainty', 'uncertain');
            }
            $doc->text($content['label']);
            $doc->endElement();
        }
    }
    if (strlen($row['manufacture']) > 0) {
        $vals = explode('|', $row['manufacture']);
        foreach ($vals as $val) {
            if (substr($val, -1) == '?') {
                $uri = substr($val, 0, -1);
                $uncertainty = true;
                $content = processUri($uri);
            } else {
                $uri = $val;
                $uncertainty = false;
                $content = processUri($uri);
            }
            $doc->startElement($content['element']);
            $doc->writeAttribute('xlink:type', 'simple');
            $doc->writeAttribute('xlink:href', $uri);
            if ($uncertainty == true) {
                $doc->writeAttribute('certainty', 'uncertain');
            }
            $doc->text($content['label']);
            $doc->endElement();
        }
    }
    if (is_numeric($row['fromDate']) && is_numeric($row['toDate'])) {
        if ($row['fromDate'] == $row['toDate']) {
            $doc->startElement('date');
            $doc->writeAttribute('standardDate', number_pad($row['fromDate'], 4));
            $doc->text($row['fromDate']);
            $doc->endElement();
        } else {
            $doc->startElement('dateRange');
            $doc->startElement('fromDate');
            $doc->writeAttribute('standardDate', number_pad($row['fromDate'], 4));
            $doc->text($row['fromDate']);
            $doc->endElement();
            $doc->startElement('toDate');
            $doc->writeAttribute('standardDate', number_pad($row['toDate'], 4));
            $doc->text($row['toDate']);
            $doc->endElement();
            $doc->endElement();
        }
    }
    //date on object
    if (strlen($row['dob_ah']) > 0) {
        $doc->startElement('dateOnObject');
        $doc->writeAttribute('calendar', 'ah');
        $doc->text($row['dob_ah']);
        $doc->endElement();
    }
    if (strlen($row['dob']) > 0) {
        $doc->writeElement('dateOnObject', $row['dob']);
    }
    //authority
    if (strlen($row['authorityPerson']) > 0 || strlen($row['org']) > 0 || strlen($row['otherPerson']) > 0) {
        $doc->startElement('authority');
        if (strlen($row['org']) > 0 && (strlen($row['authorityPerson']) == 0 && strlen($row['otherPerson']) == 0)) {
            $vals = explode('|', $row['org']);
            foreach ($vals as $val) {
                if (substr($val, -1) == '?') {
                    $uri = substr($val, 0, -1);
                    $uncertainty = true;
                    $content = processUri($uri);
                } else {
                    $uri = $val;
                    $uncertainty = false;
                    $content = processUri($uri);
                }
                $role = 'authorizingEntity';
                $doc->startElement($content['element']);
                $doc->writeAttribute('xlink:type', 'simple');
                $doc->writeAttribute('xlink:role', $role);
                $doc->writeAttribute('xlink:href', $uri);
                if ($uncertainty == true) {
                    $doc->writeAttribute('certainty', 'uncertain');
                }
                $doc->text($content['label']);
                $doc->endElement();
            }
        }
        if (strlen($row['authorityPerson']) > 0) {
            $vals = explode('|', $row['authorityPerson']);
            foreach ($vals as $val) {
                if (substr($val, -1) == '?') {
                    $uri = substr($val, 0, -1);
                    $uncertainty = true;
                    $content = processUri($uri);
                } else {
                    $uri = $val;
                    $uncertainty = false;
                    $content = processUri($uri);
                }
                $doc->startElement($content['element']);
                $doc->writeAttribute('xlink:type', 'simple');
                $doc->writeAttribute('xlink:role', 'authority');
                $doc->writeAttribute('xlink:href', $uri);
                if ($uncertainty == true) {
                    $doc->writeAttribute('certainty', 'uncertain');
                }
                $doc->text($content['label']);
                $doc->endElement();
                //if there is a dynasty or org
                if (isset($content['parent'])) {
                    $orgs[] = $content['parent'];
                    $parentArray = processUri($content['parent']);
                    $role = 'authorizingEntity';
                    $doc->startElement($parentArray['element']);
                    $doc->writeAttribute('xlink:type', 'simple');
                    $doc->writeAttribute('xlink:role', $role);
                    $doc->writeAttribute('xlink:href', $content['parent']);
                    $doc->text($parentArray['label']);
                    $doc->endElement();
                }
            }
        }
        if (strlen($row['otherPerson']) > 0) {
            $vals = explode('|', $row['otherPerson']);
            foreach ($vals as $val) {
                if (substr($val, -1) == '?') {
                    $uri = substr($val, 0, -1);
                    $uncertainty = true;
                    $content = processUri($uri);
                } else {
                    $uri = $val;
                    $uncertainty = false;
                    $content = processUri($uri);
                }
                $doc->startElement($content['element']);
                $doc->writeAttribute('xlink:type', 'simple');
                $doc->writeAttribute('xlink:href', $uri);
                $doc->writeAttribute('xlink:role', 'authority');
                if ($uncertainty == true) {
                    $doc->writeAttribute('certainty', 'uncertain');
                }
                $doc->text($content['label']);
                $doc->endElement();
                //if there is a dynasty or org; only insert if there isn't already one
                if (isset($content['parent'])) {
                    if (!in_array($content['parent'], $orgs)) {
                        $parentArray = processUri($content['parent']);
                        $role = 'authorizingEntity';
                        $doc->startElement($parentArray['element']);
                        $doc->writeAttribute('xlink:type', 'simple');
                        $doc->writeAttribute('xlink:role', $role);
                        $doc->writeAttribute('xlink:href', $content['parent']);
                        $doc->text($parentArray['label']);
                        $doc->endElement();
                    }
                }
            }
        }
        //add Muluk al-Tawaif
        if ($recordId >= 1633 && $recordId <= 1636) {
            $doc->startElement('corpname');
            $doc->writeAttribute('xlink:type', 'simple');
            $doc->writeAttribute('xlink:role', 'authorizingEntity');
            $doc->writeAttribute('xlink:href', 'http://nomisma.org/id/muluk_al-tawaif');
            $doc->text('Muluk al-Tawaif');
            $doc->endElement();
        }
        $doc->endElement();
    }
    //geography
    //mint
    if (strlen($row['mint']) > 0) {
        $doc->startElement('geographic');
        $vals = explode('|', $row['mint']);
        foreach ($vals as $val) {
            if (substr($val, -1) == '?') {
                $uri = substr($val, 0, -1);
                $uncertainty = true;
                $content = processUri($uri);
            } else {
                $uri = $val;
                $uncertainty = false;
                $content = processUri($uri);
            }
            $doc->startElement($content['element']);
            $doc->writeAttribute('xlink:type', 'simple');
            $doc->writeAttribute('xlink:role', 'mint');
            $doc->writeAttribute('xlink:href', $uri);
            if ($uncertainty == true) {
                $doc->writeAttribute('certainty', 'uncertain');
            }
            $doc->text($content['label']);
            $doc->endElement();
        }
        $doc->endElement();
    }
    //obverse
    if (isset($obverse[1])) {
        $doc->startElement('obverse');
        $doc->startElement('type');
        $doc->startElement('description');
        $doc->writeAttribute('xml:lang', 'en');
        $doc->text(trim(str_replace('Obverse:', '', $obverse[1])));
        $doc->endElement();
        $doc->endElement();
        $doc->endElement();
    }
    //reverse
    if (isset($reverse[1])) {
        $doc->startElement('reverse');
        $doc->startElement('type');
        $doc->startElement('description');
        $doc->writeAttribute('xml:lang', 'en');
        $doc->text(trim(str_replace('Reverse:', '', $reverse[1])));
        $doc->endElement();
        $doc->endElement();
        $doc->endElement();
    }
    //end typeDesc
    $doc->endElement();
    /***** PHYSDESC *****/
    $doc->startElement('physDesc');
    //measurementsSet
    if (is_numeric($row['weight']) || is_numeric($row['diameter']) || is_numeric($row['height']) && is_numeric($row['width'])) {
        $doc->startElement('measurementsSet');
        if (strlen($row['weight']) > 0 && is_numeric($row['weight'])) {
            $doc->startElement('weight');
            $doc->writeAttribute('units', 'g');
            $doc->text($row['weight']);
            $doc->endElement();
        }
        if (strlen($row['diameter']) > 0 && is_numeric($row['diameter'])) {
            $doc->startElement('diameter');
            $doc->writeAttribute('units', 'mm');
            $doc->text($row['diameter']);
            $doc->endElement();
        }
        if (strlen($row['height']) > 0 && is_numeric($row['height']) && strlen($row['width']) > 0 && is_numeric($row['width'])) {
            $doc->startElement('height');
            $doc->writeAttribute('units', 'mm');
            $doc->text($row['height']);
            $doc->endElement();
            $doc->startElement('width');
            $doc->writeAttribute('units', 'mm');
            $doc->text($row['width']);
            $doc->endElement();
        }
        $doc->endElement();
    }
    if (strlen($row['authenticity']) > 0) {
        $doc->writeElement('authenticity', $row['authenticity']);
    }
    //end physDesc
    $doc->endElement();
    /***** REFDESC *****/
    if (isset($refText)) {
        if (strlen($refText) > 0) {
            $references = explode(';', $refText);
            $doc->startElement('refDesc');
            foreach ($references as $reference) {
                $doc->writeElement('reference', trim($reference));
            }
            $doc->endElement();
        }
    }
    /***** ADMINDESC *****/
    $doc->startElement('adminDesc');
    $doc->startElement('identifier');
    $doc->writeAttribute('localType', 'catnum');
    $doc->text($recordId);
    $doc->endElement();
    if (strlen($row['regno']) > 0) {
        $doc->startElement('identifier');
        $doc->writeAttribute('localType', 'regno');
        $doc->text($row['regno']);
        $doc->endElement();
    }
    if (strlen($row['1887 catalog number']) > 0) {
        $doc->startElement('identifier');
        $doc->writeAttribute('localType', '1887catnum');
        $doc->text($row['1887 catalog number']);
        $doc->endElement();
    }
    $doc->startElement('collection');
    $doc->writeAttribute('xlink:type', 'simple');
    $doc->writeAttribute('xlink:href', 'http://nomisma.org/id/egyptian_national_library');
    $doc->text('Egyptian National Library');
    $doc->endElement();
    //end adminDesc
    $doc->endElement();
    //note
    if (strlen($row['note']) > 0) {
        $doc->startElement('noteSet');
        $doc->writeElement('note', $row['note']);
        $doc->endElement();
    }
    //end descMeta
    $doc->endElement();
    /***** IMAGES *****/
    $doc->startElement('digRep');
    $doc->startElement('mets:fileSec');
    foreach ($files as $file) {
        if (strpos($file, '.jpg') !== FALSE) {
            $arr = explode('_', str_replace('.jpg', '', $file));
            $id = $arr[0];
            $type = $arr[1];
            if ($id == $recordId) {
                switch ($type) {
                    case 'obv':
                        $use = 'obverse';
                        break;
                    case 'rev':
                        $use = 'reverse';
                        break;
                    default:
                        $use = $type;
                }
                $doc->startElement('mets:fileGrp');
                $doc->writeAttribute('USE', $use);
                //reference image
                $doc->startElement('mets:file');
                $doc->writeAttribute('USE', 'reference');
                $doc->writeAttribute('MIMETYPE', 'image/jpeg');
                $doc->startElement('mets:FLocat');
                $doc->writeAttribute('LOCTYPE', 'URL');
                $doc->writeAttribute('xlink:href', 'media/reference/' . $file);
                $doc->endElement();
                $doc->endElement();
                //display thumbnail for obverse and reverse images only
                if ($use == 'obverse' || $use == 'reverse') {
                    $doc->startElement('mets:file');
                    $doc->writeAttribute('USE', 'thumbnail');
                    $doc->writeAttribute('MIMETYPE', 'image/jpeg');
                    $doc->startElement('mets:FLocat');
                    $doc->writeAttribute('LOCTYPE', 'URL');
                    $doc->writeAttribute('xlink:href', 'media/thumbnail/' . $file);
                    $doc->endElement();
                    $doc->endElement();
                }
                //end mets:fileGrp
                $doc->endElement();
            }
        }
    }
    //end mets:fileSec
    $doc->endElement();
    //end digRep
    $doc->endElement();
    //end nuds
    $doc->endElement();
    $doc->endDocument();
    echo "Writing {$recordId}\n";
}