Back in May, James Strachan answered a query with this:

Yes its very possible indeed. The trick is to use XPath variables. e.g.

 import org.jaxen.SimpleVariableContext;

 SimpleVariableContext variables = new SimpleVariableContext();
 XPath xpath = doc.createXPath( "//foo[@doo=$x]" );
 xpath.setVariableContext( variables );

 Then you can set whatever variable value you want for 'x' and apply the
 XPath to any node.

 variables.setVariableValue( "x", "abc" );
 List answer = xpath.selectNodes( document );
 List answer = xpath.selectNodes( someNode );

 I've been trying to get this to go, and having a problem. Here is my
program:

import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;
import org.dom4j.xpath.*;
import org.jaxen.SimpleVariableContext;

public class XPathTest {
static String s1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
static String s2 = "<StateTable>";
static String s3 = "<Action>";
static String s4 = "<InputEvent>Cash</InputEvent>";
static String s5 = "<ProcessFile>Cash.xsl</ProcessFile>";
static String s6 = "</Action>";
static String s7 = "</StateTable>";

public static void main(String[] args){
(new XPathTest()).go(s1+s2+s3+s4+s5+s6+s7,
"/StateTable/Action[InputEvent=\"Cash\"]/ProcessFile",
"n/a");
(new XPathTest()).go(s1+s2+s3+s4+s5+s6+s7,
"/StateTable/Action[InputEvent=$x]/ProcessFile",
"Cash");
}

void go(String theXML, String expr, String para){
try
{
SAXReader reader = new SAXReader(false);
Document doc = reader.read(new StringReader(theXML));
XPath xp = doc.createXPath(expr);
SimpleVariableContext vc = new SimpleVariableContext();
xp.setVariableContext(vc);
vc.setVariableValue("x", para);
System.out.println(doc.valueOf(xp.getText()));
}
catch (Exception e)
{
e.printStackTrace(System.out);
}
}
}

The first call to go(), not using a variable, works fine. The second bombs
with:

org.dom4j.XPathException: Exception occurred evaluting XPath:
/StateTable/Action
[InputEvent=$x]/ProcessFile. Exception: Variable {null}:x

I'd much appreciate some assistance in finding out what's wrong.

Cheers

Tony

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com


-------------------------------------------------------
This sf.net email is sponsored by: viaVerio will pay you up to
$1,000 for every account that you consolidate with us.
http://ad.doubleclick.net/clk;4749864;7604308;v?
http://www.viaverio.com/consolidator/osdn.cfm
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to