Steve, thanks for your prompt response. I can see the quotes make better
sense. However, it still won't work on my machine - it's failing to do the
substitution. I'd expect getText() to return the string with the
substitution in place already.

To illustrate, I've changed the System.out.println()s to:

System.out.println("A " + xp.getText());
System.out.println("B " + doc.valueOf(xp.getText()));

The output I get is:

A /StateTable/Action[InputEvent="Cash"]/ProcessFile
B Cash.xsl
A /StateTable/Action[InputEvent="$x"]/ProcessFile
B

Maybe I'm using a different version. I have jaxen-1.0-FCS and
dom4j-patched-1.3

Cheers

Tony


> Tony,
>
> There was an error in James example. Replace your line with this:
>   "/StateTable/Action[InputEvent=\"$x\"]/ProcessFile",
>
> with the escaped quotes around the variable, and it works fine. I get
> "Cash.xsl" as the output.
>
> steve
>
> Tony Ms wrote:
>
> > 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

> > dom4j-user mailing list
> > [EMAIL PROTECTED]
> > https://lists.sourceforge.net/lists/listinfo/dom4j-user
>



-------------------------------------------------------
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