You are thinking of a <x> as an instruction to create a start tag, and </x> as a separate instruction to create an end tag. That's an entirely incorrect mental model of the semantics of the language, and you will have conceptual difficulties writing XSLT code until you get it out of your head.
Sam has shown you one way to do this; another way (depending on the details) is
Code:
<xsl:element name="{if (a) then 'Element1' else 'Element2'}">
... create the attributes and content ....
</xsl:element>