Class Test.Test1 Extends (%RegisteredObject, %XML.Adaptor)
{

ClassMethod RecursiveGlobalCount(pGlobalName As %String, pKey As %String, pCount As %Integer) As %Integer
  {
      /// pCount is zero if not provided
      Set pCount = +$Get(pCount)
      Set tKey = $Get(pKey)

      //pKey should only be undefined on first run
      Set tKey = $Query(@pGlobalName@(tKey))
      If (tKey '= "") Set pCount = 1 + pCount // setting tKey got the first node

      For {
          Set tKey = $Query(@tKey)
          If ((tKey = "") || (tKey '[ pKey)) Quit
          Set pCount = 1 + pCount
      }
      Quit pCount
  }

}

Testing:

    Write  ##class(Test.Test1).RecursiveGlobalCount("^Locations","",0)

    5

    Write  ##class(Test.Test1).RecursiveGlobalCount("^Locations","Canada",0)

    2

    Write ##class(Test.Test1).RecursiveGlobalCount("^Locations","USA",0)
    3

To specify the list of public variables for the method, use the following syntax:

Method name(formal_spec) As returnclass [ PublicList = variablelist ]
{
   //implementation
}

Where publiclist is either a single variable name or a comma-separated list of variable names, enclosed in parentheses.

In the documentation search for publiclist and you can get the details