This is some excellent details that were provided by "-Father Ramon" in a thread in the user forums.
The form of $ expansion is used everywhere else but NOT XPATH. The expression and requires both a leading and trailing $. It is similar to ~expansion in that it results in text replacement rather than a variable reference, but differs in 3 important ways:
I say try yo because it is a known issue that it does the escaping wrong and ends up replacing the meta-character with the equivalent of \u0000 (e.g. the null character) so you will always end up with a bogus expression if your variable contained literals. I've been trying to convince somebody in a position to do so that this needs to be fixed for about a year and a half now (and that the fix is trivial), but so far with no luck.
Even if it did work correctly, what this means is that using $ expansion always results in an expression that matches the literal value of the variable and can't be used to embed wildcards or other meta-characters.
The end result of that being that you can't construct regular expressions in local variables, and you have to use expansion instead of $ expansion if you want to use a GCV as a regular expression. I think it would be a great enhancement if when they fix the escaping problem, they add some additional syntax to signal that you want to expand in a regex without escaping, maybe something like $!variable!$.
A leading "$" is a DirXML Variables reference and is used on Global Configuration Variables and Local Variables. For example, if you have Global Configuration Variables abc that contains the string "xyz", then the expression:
$abc results in the string "XYZ"
but the expression
~abc~ results in the expression "xyz"
Surrounding with "~" works at replaced at driver startup time which is policy load time and replaces the text before the policy is even parsed, so it is as if the replacement text was part of the policy. which is shorthand for:
child::xyzWhich selects all the abc elements that are children of the context node.
To get a string from expansion, you would need.
'~abc~' which would expand to: 'xyz'which is a string literal.