protected function createFeatureTypeFromXml($xml, $myWfs, $featureTypeName)
 {
     $newFeatureType = new WfsFeatureType($myWfs);
     $doc = new DOMDocument();
     $doc->loadXML($xml);
     $xpath = new DOMXpath($doc);
     $xpath->registerNamespace("xs", "http://www.w3.org/2001/XMLSchema");
     // populate a Namespaces Hastable where we can use thec namesopace as a lookup for the prefix
     // and also keep a
     $namespaces = array();
     $namespaceList = $xpath->query("//namespace::*");
     $targetNamespace = $doc->documentElement->getAttribute("targetNamespace");
     $targetNamespaceNode = null;
     foreach ($namespaceList as $namespaceNode) {
         $namespaces[$namespaceNode->nodeValue] = $namespaceNode->localName;
         if ($namespaceNode->nodeValue == $targetNamespace) {
             $targetNamespaceNode = $namespaceNode;
         }
         $newFeatureType->addNamespace($namespaceNode->localName, $namespaceNode->nodeValue);
     }
     list($ftLocalname, $ftTypePrefix) = array_reverse(explode(":", $featureTypeName));
     // for the sake of simplicity we only care about top level elements. Seems to have worked so far
     $query = sprintf("/xs:schema/xs:element[@name='%s']", $ftLocalname);
     $elementList = $xpath->query($query);
     foreach ($elementList as $elementNode) {
         $elementName = $elementNode->getAttribute("name");
         $elementType = $elementNode->getAttribute("type");
         // if Type is empty, we assume an anonymousType, else we go looking for the anmed Type
         if ($elementType == "") {
             // Just querying for complexTypes containing a Sequence - good enough for Simple Features
             $query = "xs:complexType//xs:element";
             $subElementList = $xpath->query($query, $elementNode);
         } else {
             // The elementType is now bound to a prefix e.g. topp:housType
             // if the prefix is in the targetNamespace, changces are good it's defined in this very document
             // if the prefiox is not in the targetNamespace, it's likely not defined here, and we bail
             list($elementTypeLocalname, $elementTypePrefix) = array_reverse(explode(":", $elementType));
             $elementTypeNamespace = $doc->lookupNamespaceURI($elementTypePrefix);
             if ($elementTypeNamespace !== $targetNamespaceNode->nodeValue) {
                 $e = new mb_warning("Tried to parse FeatureTypeName {$featureTypeName} : {$elementType} is not in the targetNamespace");
                 break;
             }
             // Just querying for complexTypes containing a Sequence - good enough for Simple Features
             $query = sprintf("//xs:complexType[@name='%s']//xs:element", $elementTypeLocalname);
             $subElementList = $xpath->query($query);
         }
         foreach ($subElementList as $subElement) {
             // Since this is a rewrite of the old way, it reproduces it quirks
             // in this case the namespace of the type was cut off for some reason
             $name = $subElement->getAttribute('name');
             $typeParts = explode(":", $subElement->getAttribute('type'));
             if (count($typeParts) == 1) {
                 $type = $typeParts[0];
             } else {
                 $type = $typeParts[1];
             }
             $newFeatureType->addElement($name, $type);
         }
     }
     return $newFeatureType;
 }
 /**
  * Retrieves the data of a WFS from the database and initiates the object.
  * 
  * @return 
  * @param $id Integer
  * @param $aWfs Wfs is being created by the subclass
  */
 public function createFromDb($id, $withProxyUrls = true)
 {
     if (func_num_args() == 2) {
         $aWfs = func_get_arg(1);
     } else {
         return null;
     }
     // WFS
     $sql = "SELECT * FROM wfs WHERE wfs_id = \$1;";
     $v = array($id);
     $t = array("i");
     $res = db_prep_query($sql, $v, $t);
     $cnt = 0;
     while (db_fetch_row($res)) {
         $hasOwsproxyUrl = false;
         $e = new mb_notice("class_wfs_factory: wfs_owsproxy: " . db_result($res, $cnt, "wfs_owsproxy"));
         if (db_result($res, $cnt, "wfs_owsproxy") != '') {
             $owsproxyUrl = OWSPROXY . "/" . session_id() . "/" . db_result($res, $cnt, "wfs_owsproxy") . "?";
             $e = new mb_notice("class_wfs_factory: owsproxyURl: " . $owsproxyUrl);
             $hasOwsproxyUrl = true;
         }
         $aWfs->id = db_result($res, $cnt, "wfs_id");
         $aWfs->name = db_result($res, $cnt, "wfs_name");
         $aWfs->title = db_result($res, $cnt, "wfs_title");
         $aWfs->summary = db_result($res, $cnt, "wfs_abstract");
         $aWfs->getCapabilities = db_result($res, $cnt, "wfs_getcapabilities");
         $aWfs->getCapabilitiesDoc = db_result($res, $cnt, "wfs_getcapabilities_doc");
         $aWfs->uploadUrl = db_result($res, $cnt, "wfs_upload_url");
         $aWfs->describeFeatureType = db_result($res, $cnt, "wfs_describefeaturetype");
         if (!$hasOwsproxyUrl || !$withProxyUrls) {
             $aWfs->getFeature = db_result($res, $cnt, "wfs_getfeature");
         } else {
             $aWfs->getFeature = $owsproxyUrl;
         }
         new mb_notice("class_wfs_factory.getFeature.url: " . $aWfs->getFeature);
         if (!$hasOwsproxyUrl || !$withProxyUrls) {
             $aWfs->transaction = db_result($res, $cnt, "wfs_transaction");
         } else {
             $aWfs->transaction = $owsproxyUrl;
         }
         $aWfs->fees = db_result($res, $cnt, "fees");
         $aWfs->accessconstraints = db_result($res, $cnt, "accessconstraints");
         $aWfs->owner = db_result($res, $cnt, "wfs_owner");
         $aWfs->timestamp = db_result($res, $cnt, "wfs_timestamp");
         $aWfs->timestamp_create = db_result($res, $cnt, "wfs_timestamp_create");
         $aWfs->network_access = db_result($res, $cnt, "wfs_network_access");
         $aWfs->fkey_mb_group_id = db_result($res, $cnt, "fkey_mb_group_id");
         $aWfs->uuid = db_result($res, $cnt, "uuid");
         // Featuretypes
         $sql_fe = "SELECT * FROM wfs_featuretype WHERE fkey_wfs_id = \$1 ORDER BY featuretype_id";
         $v = array($aWfs->id);
         $t = array("i");
         $res_fe = db_prep_query($sql_fe, $v, $t);
         $cnt_fe = 0;
         while (db_fetch_row($res_fe)) {
             $ft = new WfsFeatureType($aWfs);
             $ft->id = db_result($res_fe, $cnt_fe, "featuretype_id");
             $ft->name = db_result($res_fe, $cnt_fe, "featuretype_name");
             $ft->title = db_result($res_fe, $cnt_fe, "featuretype_title");
             $ft->summary = db_result($res_fe, $cnt_fe, "featuretype_abstract");
             $ft->srs = db_result($res_fe, $cnt_fe, "featuretype_srs");
             $ft->uuid = db_result($res_fe, $cnt_fe, "uuid");
             // Elements
             $sql_el = "SELECT * FROM wfs_element WHERE fkey_featuretype_id = \$1 ORDER BY element_id";
             $v = array($ft->id);
             $t = array("i");
             $res_el = db_prep_query($sql_el, $v, $t);
             $cnt_el = 0;
             while (db_fetch_row($res_el)) {
                 $ft->addElement(db_result($res_el, $cnt_el, "element_name"), db_result($res_el, $cnt_el, "element_type"), db_result($res_el, $cnt_el, "element_id"));
                 $cnt_el++;
             }
             //Namespaces
             $sql_ns = "SELECT * FROM wfs_featuretype_namespace WHERE fkey_featuretype_id = \$1 ORDER BY namespace";
             $v = array($ft->id);
             $t = array("i");
             $res_ns = db_prep_query($sql_ns, $v, $t);
             $cnt_ns = 0;
             while (db_fetch_row($res_ns)) {
                 $ft->addNamespace(db_result($res_ns, $cnt_ns, "namespace"), db_result($res_ns, $cnt_ns, "namespace_location"));
                 $cnt_ns++;
             }
             $aWfs->addFeatureType($ft);
             $cnt_fe++;
         }
         $cnt++;
     }
     return $aWfs;
 }