You don't need any of your for-each, just use the suggested templates and one template creating the root element if needed and perhaps one suppressing the processing of empty elements:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:test="http://search.yahoo.com/mrss/"
version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/rss">
<rss version="{@version}">
<xsl:apply-templates/>
</rss>
</xsl:template>
<xsl:template match="item">
<test:item>
<xsl:apply-templates/>
</test:item>
</xsl:template>
<xsl:template match="item/*[normalize-space()]">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="item/*[not(normalize-space())]"/>
</xsl:stylesheet>
That stylesheet transforms the input
Code:
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<item>
<title>this is title</title>
<description>about us</description>
<category/>
<link>www.google.co.in</link>
</item>
<item>
<title>Home</title>
<description>Home page</description>
<category>web</category>
<link>www.yahoo.com</link>
</item>
</channel>
</rss>
into
Code:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:test="http://search.yahoo.com/mrss/" version="2.0">
<test:item title="this is title" description="about us" link="www.google.co.in"/>
<test:item title="Home" description="Home page" category="web" link="www.yahoo.com"/>
</rss>