public function generateInstanceDocument( $xbrlInstance, $nsMgr )
	{
		// Get the namespaces from the input documment
		// $namespaces = $xbrlInstance->getInstanceXml()->getDocNamespaces();

		// Get the taxonomy document uri from the input document
		$taxonomyUri = $xbrlInstance->getInstanceTaxonomy()->getTaxonomyXSD();

		$document[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		$document[] = "<!-- Generated by XBRLQuery formula processor -->";

		// Add the root element and namespaces
		// First make sure there is a default xbrli prefix
		if ( ! isset( $this->namespaces[''] ) )
		{
			$this->namespaces[''] = \XBRL_Constants::$standardPrefixes[ STANDARD_PREFIX_XBRLI ];
		}
		$document[] ="<xbrl";
		foreach ( $this->namespaces as $prefix => $namespace )
		{
			$document[] = "  " . ( $prefix ? "xmlns:$prefix=\"$namespace\"" : "xmlns=\"$namespace\"" );
		}

		if ( ! isset( $this->namespaces['xsi'] ) &&
				array_filter( $this->facts, function( $facts ) {
					return array_filter( $facts, function( $fact ) { return is_null( $fact['value'] ); } ); }
				)
		   )
		{
			$document[] = "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
		}

		if ( ! isset( $this->namespaces['link'] ) )
		{
			$document[] = "  xmlns:link=\"http://www.xbrl.org/2003/linkbase\"";
		}

		$document[] =">";

		// Add the schema ref
		$document[] = "";
		$document[] = "  <link:schemaRef  xlink:type=\"simple\" xlink:href=\"$taxonomyUri\"/>";
		$document[] = "";

		foreach ( $this->linkbases as $linkbase )
		{
			$document[] = "  <link:linkbaseRef";
			$document[] = "    xlink:type=\"{$linkbase['xlink:type']}\"";
			$document[] = "    xlink:arcrole=\"{$linkbase['xlink:arcrole']}\"";
			$document[] = "    xlink:href=\"{$linkbase['xlink:href']}\"";
			$document[] = "  />";
			$document[] = "";
		}

		// Add the contexts
		// $this->addContexts( $document, $xbrlInstance->getPrefixForNamespace( \XBRL_Constants::$standardPrefixes[ STANDARD_PREFIX_XBRLDI ] ) );
		$this->addContexts( $document, STANDARD_PREFIX_XBRLDI );

		// Add the units
		$this->addUnits( $document );

		// Add the facts
		$this->addFacts2( $document );

		// Close the document
		$document[] ="</xbrl>";

		// Run validation on the generated document

		return implode( "\n", $document) ;
	}