Index: wp-admin/link-import.php
===================================================================
--- wp-admin/link-import.php	(revision 7525)
+++ wp-admin/link-import.php	(working copy)
@@ -100,8 +100,24 @@
 
 			include_once('link-parse-opml.php');
 
+			$blogroll_cats = get_terms('link_category', 'get=all');
+			foreach ($blogroll_cats as $category){
+				$blogroll_categories[$category->name] = $category->term_id;
+			}
+
 			$link_count = count($names);
+
 			for ( $i = 0; $i < $link_count; $i++ ) {
+
+				if ( !$cat_id = is_term( $link_categories[$i], 'link_category' ) ) {
+					$cat_id = wp_insert_term( $link_categories[$i], 'link_category' );
+					$cat_id = $cat_id['term_id'];
+					$blogroll_categories[$link_categories[$i]] = $cat_id; //adding item to an array of link_cateories from database
+				}
+				else {
+					$cat_id = (int) $blogroll_categories[$link_categories[$i]];
+				}
+
 				if ('Last' == substr($titles[$i], 0, 4))
 					$titles[$i] = '';
 				if ( 'http' == substr($titles[$i], 0, 4) )
Index: wp-admin/link-parse-opml.php
===================================================================
--- wp-admin/link-parse-opml.php	(revision 7525)
+++ wp-admin/link-parse-opml.php	(working copy)
@@ -9,7 +9,8 @@
 	'TITLE'       => 'link_name',
 	'TARGET'      => 'link_target',
 	'DESCRIPTION' => 'link_description',
-	'XMLURL'      => 'link_rss'
+	'XMLURL'      => 'link_rss',
+	'CATEGORY'    => 'category'
 );
 
 $map = $opml_map;
@@ -20,7 +21,7 @@
  **/
 function startElement($parser, $tagName, $attrs) {
 	global $updated_timestamp, $all_links, $map;
-	global $names, $urls, $targets, $descriptions, $feeds;
+	global $names, $urls, $targets, $descriptions, $feeds, $link_categories;
 
 	if ($tagName == 'OUTLINE') {
 		foreach (array_keys($map) as $key) {
@@ -37,6 +38,7 @@
 		$targets[] = $link_target;
 		$feeds[] = $link_rss;
 		$descriptions[] = $link_description;
+		$link_categories[] = $category;
 	} // end if outline
 }
 
Index: wp-links-opml.php
===================================================================
--- wp-links-opml.php	(revision 7525)
+++ wp-links-opml.php	(working copy)
@@ -31,22 +31,18 @@
 foreach ((array) $cats as $cat) {
 	$catname = apply_filters('link_category', $cat->name);
 
-?>
-<outline type="category" title="<?php echo attribute_escape($catname); ?>">
-<?php
-
 	$bookmarks = get_bookmarks("category={$cat->term_id}");
+
 	foreach ((array) $bookmarks as $bookmark) {
 		$title = attribute_escape(apply_filters('link_title', $bookmark->link_name));
+		//adding category as an attribute to the outline. category is a valid attribute in OPML.
+		//so importer will create link_category if not exists in the database on the fly!
+		//generated OPML output has been validated on http://validator.opml.org/
 ?>
-	<outline text="<?php echo $title; ?>" type="link" xmlUrl="<?php echo attribute_escape($bookmark->link_rss); ?>" htmlUrl="<?php echo attribute_escape($bookmark->link_url); ?>" updated="<?php if ('0000-00-00 00:00:00' != $bookmark->link_updated) echo $bookmark->link_updated; ?>" />
+	<outline text="<?php echo $title; ?>" type="link" url="<?php echo attribute_escape($bookmark->link_url); ?>" category="<?php echo $catname; ?>" />
 <?php
 
 	}
-?>
-</outline>
-<?php
-
 }
 ?>
 </body>
