You haven't understood two of the most basic concepts of XSLT: context, and template rules. This is what your code is doing:
<xsl:if test="ns0:Messages/..../details/name = A">
The xsl:if says "if there is an element selected by the path ns0:Messages/..../details/name whose value is equal to A then..." Your context node is the document root, so you are testing whether there is such an element anywhere in the document. If there is, then you do the xsl:for-each, which again selects from the document root, and thus processes all the selected elements in the whole document, whether or not they matched the condition in the xsl:if.
(There's another little mistake in your code as well, you should have said ='A' rather than =A. A without quotes means child::A, that is, an element whose name is A.)
What you want to achieve is to transform a details element with name=A intoa Message1, and to transform a details element with name=B into a message2. So write two template rules:
<xsl:value-of select="name" />
<xsl:value-of select="id" />
and similarly for match="details[name='B']"
Then just make sure the details elements are processed:
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference