Tony,

You're code is fine but for 1 line. You're creating the XPath properly,
setting variables in the context, then not using the XPath object directly.
See below...


From: "Tony Ms" <[EMAIL PROTECTED]>
> 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()));

Change this like to...

System.out.println(xp.valueOf(doc));

Then you will actually use the XPath object with its variable context. The
line you had just takes the XPath String, ignores the variable context and
evaluates the XPath expression in an empty variable context.

James
-------
http://radio.weblogs.com/0112098/

__________________________________________________
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 emial is sponsored by: Influence the future 
of Java(TM) technology. Join the Java Community 
Process(SM) (JCP(SM)) program now. 
http://ads.sourceforge.net/cgi-bin/redirect.pl?sunm0002en
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to