get_canonical_id (OpenID::Yadis)

    # File lib/openid/yadis/xrds.rb, line 25
        def Yadis::get_canonical_id(iname, xrd_tree)
          # Return the CanonicalID from this XRDS document.
          #
          # @param iname: the XRI being resolved.
          # @type iname: unicode
          #
          # @param xrd_tree: The XRDS output from the resolver.
          #
          # @returns: The XRI CanonicalID or None.
          # @returntype: unicode or None

          xrd_list = []
          REXML::XPath::match(xrd_tree.root, '/xrds:XRDS/xrd:XRD', XRDS_NAMESPACES).each { |el|
            xrd_list << el
          }

          xrd_list.reverse!

          cid_elements = []

          if !xrd_list.empty?
            xrd_list[0].elements.each { |e|
              if !e.respond_to?('name')
                next
              end
              if e.name == 'CanonicalID'
                cid_elements << e
              end
            }
          end

          cid_element = cid_elements[-1]

          if !cid_element
            return nil
          end

          canonicalID = XRI.make_xri(cid_element.text)

          childID = canonicalID

          xrd_list[1..-1].each { |xrd|
            parent_sought = childID[0...childID.rindex('!')]

            parent_list = []
            xrd.elements.each("CanonicalID") { |c|
              parent_list.push(XRI.make_xri(c.text))
            }

            if !parent_list.member?(parent_sought)
              raise XRDSFraud.new(sprintf("%s can not come from any of %s", parent_sought,
                                          parent_list))
            end

            childID = parent_sought
          }

          root = XRI.root_authority(iname)
          if not XRI.provider_is_authoritative(root, childID)
            raise XRDSFraud.new(sprintf("%s can not come from root %s", childID, root))
          end

          return canonicalID
        end