You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Aodh Ó Lionáird <ao...@student.cs.ucc.ie> on 2008/08/18 10:38:31 UTC

Resolve Variables lazily?

Suppose I have the following XPath expression: /section[@n=$section_id]

When I evaluate the expression e.g.

String exp="/section[@n=$section_id]";

NodeList nodeList=(NodeList)xpath.evaluate(exp,domDoc,
XPathConstants.NODESET));

I notice that my resolveVariable method is called for each result. Now,
the behaviour I want is for the resolveVariable method to be called once
for each invocation of xpath.evaluate(). The idea being, that I can then
extract a section of a document based on its section_id which I provide
through variable resolution.

I guess I would call this Lazy resolution? Is there a way to do this using
an Java XPath implementations?

//For illustrative purposes only.
public Object resolveVariable(QName var){

   //List uniqueIDs declared elsewhere.

  System.out.println("Resolver invoked");

  if (var.equals(new QName("section_id"))){

	Integer uniqueID=-1;

        if(uniqueIDs.hasNext()){
        	uniqueID=Integer.valueOf(uniqueIDs.next().toString());
        }


        return uniqueID;

        }else{
            return null;
        }
  }

}

Thanks,

A.