如何在XPath查询中使用Java String变量

| 我有一个“ 0”文件,其中包含作者姓名和书名。我正在使用以下代码片段查询
books.xml
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr 
    = xpath.compile(\"//book[author= \'Larry Niven\']/title/text()\");
现在,如果我想在程序作为String变量运行时传递名称,则不要直接将名称放入查询中。仅将字符串变量名放不起作用!     
已邀请:
String rawXPath = \"//book[author= \'\" + larrysName + \"\']/title/text()\";
要么
String rawXPath = String.format(\"//book[author= \'%s\']/title/text()\", larrysName);
其中
larrysName
是类型
String
的变量,来自某个地方。     
这里的问题是当您有一个臭名昭著的拉里·“巴什尔”·奥尼尔·尼文时。 在这种情况下,您将需要对变量进行转义,就像在这个简单的实现中一样:
  public static String escape(String s) {
    Matcher matcher = Pattern.compile(\"[\'\\\"]\")
        .matcher(s);
    StringBuilder buffer = new StringBuilder(\"concat(\");
    int start = 0;
    while (matcher.find()) {
      buffer.append(\"\'\")
          .append(s.substring(start, matcher.start()))
          .append(\"\',\");
      buffer.append(\"\'\".equals(matcher.group()) ? \"\\\"\'\\\",\" : \"\'\\\"\',\");
      start = matcher.end();
    }
    if (start == 0) {
      return \"\'\" + s + \"\'\";
    }
    return buffer.append(\"\'\")
        .append(s.substring(start))
        .append(\"\'\")
        .append(\")\")
        .toString();
  }
这可以用以下代码演示:
String xml =
    \"<xml><foo bar=\\\"Larry &quot;Basher&quot; O\'Niven\\\">Ringworm</foo></xml>\";
String query =
    String.format(\"//foo[@bar=%s]\", escape(\"Larry \\\"Basher\\\" O\'Niven\"));
System.out.println(query);
String book = XPathFactory.newInstance()
    .newXPath()
    .evaluate(query, new InputSource(new StringReader(xml)));
System.out.println(query + \" > \" + book);
    
实际上,您可以在XPath中同时使用自定义函数和变量-但是快速破解对于许多用途而言可能会更有效率。 以下是我为学生开发的一些学习工具代码。它可以让您做到这一点:
// create some variable we want to use in the xpath
xPathVariableAndFunctionResolver.newVariable(\"myNamespace\", \"id\", \"xs:string\", \"l2\"); // myNamespace is declared in the namespace context with prefix \'my\'

// create an XPath expression
String expression = \"//did:Component[@id=$my:id]\"; // variable $namespace:name
XPathExpression findComponents = xPathFunctionAndVariableOperator.compile(expression);

// execute the XPath expression against the document
NodeList statements = (NodeList)findComponents.evaluate(document, XPathConstants.NODESET);
与XPath函数大致相同。代码,首先是常规XPath评估的包装器:
public class XPathOperator {

    protected XPath xPath;
    protected XPathFactory xPathFactory;

    private Hashtable<String, XPathExpression> compiled = new Hashtable<String, XPathExpression>();

    protected void initFactory() throws XPathFactoryConfigurationException {
        xPathFactory = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
    }

    protected void initXPath(NamespaceContext context) {
        xPath = xPathFactory.newXPath();
        xPath.setNamespaceContext(context);
    }

    public XPathOperator(NamespaceContext context) throws XPathFactoryConfigurationException {
        initFactory();
        initXPath(context);
    }

    public Object evaluate(Document document, String expression, QName value) throws XPathExpressionException {

        // create an XPath expression - http://www.zvon.org/xxl/XPathTutorial/General/examples.html
        XPathExpression findStatements = compile(expression);

        // execute the XPath expression against the document
        return (NodeList)findStatements.evaluate(document, value);
    }

    public XPathExpression compile(String expression) throws XPathExpressionException {
        if(compiled.containsKey(expression)) {
            return (XPathExpression) compiled.get(expression);
        }

        XPathExpression xpath = xPath.compile(expression);

        System.out.println(\"Compiled XPath \" + expression);

        compiled.put(expression, xpath);

        return xpath;
    }
}
然后,我们添加自定义变量和函数的概念,当然还有名称空间:
public class XPathFunctionAndVariableOperator extends XPathOperator {

        public XPathFunctionAndVariableOperator(NamespaceContext context, XPathVariableResolver xPathVariableResolver, XPathFunctionResolver xPathFunctionResolver) throws XPathFactoryConfigurationException {

    super(context);

        xPath.setXPathVariableResolver(xPathVariableResolver);
        xPath.setXPathFunctionResolver(xPathFunctionResolver);
    }
}
如果没有变量和函数解析器,那将不会很有趣:
public class XPathVariableAndFunctionResolver implements XPathVariableResolver, XPathFunctionResolver {

    private Hashtable functions = new Hashtable();
    private Hashtable variables = new Hashtable();

    private SchemaDVFactory factory = SchemaDVFactory.getInstance();

    public XPathFunction resolveFunction(QName functionName, int arity) {
        Hashtable table = (Hashtable)functions.get(functionName.getNamespaceURI());
        if(table != null) {
            XPathFunction function = (XPathFunction)table.get(functionName.getLocalPart());
            if(function == null) {
                throw new RuntimeException(\"Function \" + functionName.getLocalPart() + \" does not exist in namespace \" + functionName.getNamespaceURI() + \"!\");
            }
            System.out.println(\"Resolved function \" + functionName + \" with \" + arity + \" argument(s)\");
            return function;
        }
        throw new RuntimeException(\"Function namespace \" + functionName.getNamespaceURI() + \" does not exist!\");
    }

    /**
     *
     * Adds a variable using namespace and name, primitive type and default value
     *
     * @param namespace
     * @param name
     * @param datatype      one of the built-in XML datatypes
     * @param value
     * @throws InvalidDatatypeValueException    if value is not of correct datatype
     */

    @SuppressWarnings(\"unchecked\")
    public void newVariable(String namespace, String name, String datatype, String value) throws InvalidDatatypeValueException {

        int index = datatype.indexOf(\":\");
        if(index != -1) {
            datatype = datatype.substring(index+1);
        }
        XSSimpleType builtInType = factory.getBuiltInType(datatype);

        if(builtInType == null) {
            throw new RuntimeException(\"Null type for \" + datatype);
        }

        ValidationState validationState = new ValidationState();
        ValidatedInfo validatedInfo = new ValidatedInfo();

        builtInType.validate(value, validationState, validatedInfo);

        System.out.println(\"Defined variable \" + name + \" as \" + datatype + \" with value \" + value);

        Hashtable table;
        if(!variables.containsKey(namespace)) {
            table = new Hashtable();
            variables.put(namespace, table);
        } else {
            table = (Hashtable)variables.get(namespace);
        }

        table.put(name, new Object[]{validatedInfo, builtInType});
    }

    public void newVariableValue(String namespace, String name, String value) throws InvalidDatatypeValueException {
        ValidationState validationState = new ValidationState();

        Hashtable table;
        if(!variables.containsKey(namespace)) {
            throw new RuntimeException(\"Unknown variable namespace \" + namespace);
        } else {
            table = (Hashtable)variables.get(namespace);
        }

        Object[] bundle = (Object[])table.get(name);
        ValidatedInfo validatedInfo = (ValidatedInfo)bundle[0];
        XSSimpleType builtInType =  (XSSimpleType)bundle[1];
        builtInType.validate(value, validationState, validatedInfo); // direct reference transfer of value

        System.out.println(\"Assigned value \" + validatedInfo.normalizedValue + \" to variable \" + name);
    }

    public Object resolveVariable(QName variableName) {

        Hashtable table;
        if(!variables.containsKey(variableName.getNamespaceURI())) {
            throw new RuntimeException(\"Unknown variable namespace \" + variableName.getNamespaceURI());
        } else {
            table = (Hashtable)variables.get(variableName.getNamespaceURI());
        }

        Object[] bundle = (Object[])table.get(variableName.getLocalPart());
        if(bundle != null) {
            ValidatedInfo var = (ValidatedInfo)bundle[0];

            if(var != null) {
                switch(var.actualValueType) { // some types omitted, customize your own
                case XSConstants.INTEGER_DT:
                case XSConstants.DECIMAL_DT:
                case XSConstants.INT_DT:
                case XSConstants.LONG_DT:
                case XSConstants.SHORT_DT:
                case XSConstants.BYTE_DT:
                case XSConstants.UNSIGNEDBYTE_DT:
                case XSConstants.UNSIGNEDINT_DT:
                case XSConstants.UNSIGNEDLONG_DT:
                case XSConstants.UNSIGNEDSHORT_DT:
                    return new Integer(var.normalizedValue);
                case XSConstants.DATE_DT:
                case XSConstants.DATETIME_DT:
                case XSConstants.GDAY_DT:
                case XSConstants.GMONTH_DT:
                case XSConstants.GMONTHDAY_DT:
                case XSConstants.GYEAR_DT:
                case XSConstants.GYEARMONTH_DT:
                case XSConstants.DURATION_DT:
                case XSConstants.TIME_DT:
                    return new Date(var.normalizedValue);
                case XSConstants.FLOAT_DT:
                    return new Float(Float.parseFloat(var.normalizedValue));
                case XSConstants.DOUBLE_DT:
                    return new Double(Double.parseDouble(var.normalizedValue));
                case XSConstants.STRING_DT:
                case XSConstants.QNAME_DT:
                    return var.normalizedValue;
                default:
                    throw new RuntimeException(\"Unknown datatype \" + var.actualValueType + \" for variable \" + variableName + \" in namespace \" + variableName.getNamespaceURI());
                }
            }
        }
        throw new RuntimeException(\"Could not resolve value \" + variableName + \" in namespace \" + variableName.getNamespaceURI());
    }

    public void addFunction(String namespace, String name, XPathFunction function) {
        Hashtable table;
        if(!functions.containsKey(namespace)) {
            table = new Hashtable();
            functions.put(namespace, table);
        } else {
            table = (Hashtable)functions.get(namespace);
        }
        table.put(name, function);
    }

}
这些功能显然不能包含在上面,因为通常会运行自定义代码(即,重点是您编写自己的类),因此可以使用类似
public abstract class XPathFunctionImpl implements XPathFunction {

    /**
     * This function is called by the XPath expression as it implements the interface XPathFunction
     */

    protected int numberArguments;

    public Object evaluate(List args) throws XPathFunctionException {
        if(args.size() == numberArguments) {
            return evaluateImpl(args);
        }
        throw new RuntimeException(\"Illegal number of arguments for \" + this);
    }

    public abstract Object evaluateImpl(List args) throws XPathFunctionException;

}
然后,以某种方式在EvaluateImpl(..)中实现/子类化自己的逻辑。 这样肯定会使String追加看起来相当……有吸引力;)注意:此代码已经有好几年了,可能存在一种更好的方法来完成所有这些工作。     
如果想要现成的实现,则可以使用支持变量声明的commons JXPath: http://commons.apache.org/jxpath/users-guide.html#Variables     

要回复问题请先登录注册