Class XPath

java.lang.Object
org.libxmq.imp.xml.XPath

public final class XPath extends Object
A compiled XPath 1.0 expression that is evaluated against the xmq DOM. The engine behaves like the XPath implementation in libxml2 2.9.14 (xpath.c, Copyright (C) 1998-2012 Daniel Veillard, MIT license), since the C version of xmq uses libxml2 and the java version must produce identical results. This covers which expressions are accepted, the traversal of the axes (eg dtd nodes are skipped and entity references are never descended into), document order, the conversion of numbers to strings, the comparison rules and the error behaviour (eg an undefined namespace prefix or an unknown function is an error). The result of an evaluation is one of: a List<XmlNode> (a node-set in document order without duplicates), a String, a Double or a Boolean. Namespace nodes in a node-set are XPathNamespaceNodes. Example:
XPath xp = XPath.compile("//item[@id='a']/name");
List<XmlNode> nodes = xp.selectNodes(doc);
  • Method Details

    • compile

      public static XPath compile(String expr) throws XPathException
      Compile an expression with the default settings, the namespaces are given when evaluating.
      Parameters:
      expr - The expression.
      Returns:
      The compiled expression.
      Throws:
      XPathException - If the expression is invalid.
    • compile

      public static XPath compile(String expr, XPathSettings settings) throws XPathException
      Compile an expression, like xml_xpath_compile in the C version. The settings (and their namespaces) are copied, prefixes are resolved when the expression is evaluated.
      Parameters:
      expr - The expression.
      settings - The settings.
      Returns:
      The compiled expression.
      Throws:
      XPathException - If the expression is invalid.
    • expression

      public String expression()
      Returns the source of the expression.
      Returns:
      The expression.
    • evaluate

      public Object evaluate(XmlNode context, Map<String,String> namespaces) throws XPathException
      Evaluate the expression with a fresh context, like xml_xpath_eval.
      Parameters:
      context - The context node.
      namespaces - Extra prefixes, or null.
      Returns:
      A List<XmlNode>, String, Double or Boolean.
      Throws:
      XPathException - On an error.
    • selectNodes

      public List<XmlNode> selectNodes(XmlNode context) throws XPathException
      Select the nodes of a node set expression.
      Parameters:
      context - The context node.
      Returns:
      The nodes in document order.
      Throws:
      XPathException - On an error or if the value is not a node set.
    • selectNodes

      public List<XmlNode> selectNodes(XmlNode context, Map<String,String> namespaces) throws XPathException
      Select the nodes of a node set expression.
      Parameters:
      context - The context node.
      namespaces - Extra prefixes, or null.
      Returns:
      The nodes in document order.
      Throws:
      XPathException - On an error or if the value is not a node set.
    • evaluateString

      public String evaluateString(XmlNode context) throws XPathException
      Evaluate as a string, like xpath string().
      Parameters:
      context - The context node.
      Returns:
      The string.
      Throws:
      XPathException - On an error.
    • evaluateString

      public String evaluateString(XmlNode context, Map<String,String> namespaces) throws XPathException
      Evaluate as a string, like xpath string().
      Parameters:
      context - The context node.
      namespaces - Extra prefixes, or null.
      Returns:
      The string.
      Throws:
      XPathException - On an error.
    • evaluateNumber

      public double evaluateNumber(XmlNode context) throws XPathException
      Evaluate as a number, like xpath number().
      Parameters:
      context - The context node.
      Returns:
      The number.
      Throws:
      XPathException - On an error.
    • evaluateNumber

      public double evaluateNumber(XmlNode context, Map<String,String> namespaces) throws XPathException
      Evaluate as a number, like xpath number().
      Parameters:
      context - The context node.
      namespaces - Extra prefixes, or null.
      Returns:
      The number.
      Throws:
      XPathException - On an error.
    • evaluateBoolean

      public boolean evaluateBoolean(XmlNode context) throws XPathException
      Evaluate as a boolean, like xpath boolean().
      Parameters:
      context - The context node.
      Returns:
      The boolean.
      Throws:
      XPathException - On an error.
    • evaluateBoolean

      public boolean evaluateBoolean(XmlNode context, Map<String,String> namespaces) throws XPathException
      Evaluate as a boolean, like xpath boolean().
      Parameters:
      context - The context node.
      namespaces - Extra prefixes, or null.
      Returns:
      The boolean.
      Throws:
      XPathException - On an error.
    • numberToString

      public static String numberToString(double d)
      Format a number like xpath string() (libxml2 xmlXPathFormatNumber).
      Parameters:
      d - The number.
      Returns:
      The string.
    • stringToNumber

      public static double stringToNumber(String s)
      Parse a number like xpath number() (libxml2 xmlXPathStringEvalNumber).
      Parameters:
      s - The string.
      Returns:
      The number, NaN if not a number.
    • toString

      public String toString()
      Overrides:
      toString in class Object