Then later when you test the value of $foo, it atomizes this temporary document into a string of the individual 1s and 0s.
Also, you shouldn't think of variables in XSLT the same way as you do in other languages.
WARNING: Some XSLT processors don't require you to put single quotes around a literal value if the literal value begins with a number.
This is because the XML specification states that XML element names can't begin with a number.
Go through every data node, testing each one separately.
_Each_ time a data element passes the test, write down a "1" (as a text node) in this temporary document.
Rather, this code fragment defines $foo as the collection of all the things created by the block.
In particular, B does not say "Look through the data nodes and if *any* of them satisfy the test ["contains(elements/ element A '1234) or contains(elements/package/id '55) or contains(elements/category/id, '67)" or contains(............."], then assign the value of "1" to B, otherwise assign "0." Rather, what this code does is say "Create a temporary document in memory.
(Some XSLT processors offer a special extension function to allow the resetting of variables.) XSLT variables actually have a lot more in common with constants in many programming languages and are used for a similar purpose.
If you use the same value multiple times in your stylesheet, and there's a possibility that you'll have to change them all to a different value, it's better to assign that value to a variable and use references to the variable instead.