Class XPathUtils


  • public abstract class XPathUtils
    extends Object
    XPath utility class providing static utility methods dealing with XPath expression evaluation. Class is abstract to prevent instantiation.
    Author:
    Christoph Deppisch
    • Method Detail

      • getDynamicNamespaces

        public static Map<String,​String> getDynamicNamespaces​(String expression)
        Extracts dynamic namespaces that are inline inside a XPath expression. Example: /{http://sample.org/foo}foo/{http://sample.org/bar}bar
        Parameters:
        expression -
        Returns:
      • replaceDynamicNamespaces

        public static String replaceDynamicNamespaces​(String expression,
                                                      Map<String,​String> namespaces)
        Replaces all dynamic namespaces in a XPath expression with respective prefixes in namespace map. XPath: /{http://sample.org/foo}foo/{http://sample.org/bar}bar results in /ns1:foo/ns2:bar where the namespace map contains ns1 and ns2.
        Parameters:
        expression -
        namespaces -
        Returns:
      • hasDynamicNamespaces

        public static boolean hasDynamicNamespaces​(String expression)
        Searches for dynamic namespaces in expression.
        Parameters:
        expression -
        Returns:
      • evaluate

        public static Object evaluate​(Node node,
                                      String xPathExpression,
                                      NamespaceContext nsContext,
                                      XPathExpressionResult resultType)
        Evaluate XPath expression as String result type regardless what actual result type the expression will evaluate to.
        Parameters:
        node -
        xPathExpression -
        nsContext -
        resultType -
        Returns:
      • evaluateAsNode

        public static Node evaluateAsNode​(Node node,
                                          String xPathExpression,
                                          NamespaceContext nsContext)
        Evaluate XPath expression with result type Node.
        Parameters:
        node -
        xPathExpression -
        nsContext -
        Returns:
      • evaluateAsNodeList

        public static NodeList evaluateAsNodeList​(Node node,
                                                  String xPathExpression,
                                                  NamespaceContext nsContext)
        Evaluate XPath expression with result type NodeList.
        Parameters:
        node -
        xPathExpression -
        nsContext -
        Returns:
      • evaluateAsString

        public static String evaluateAsString​(Node node,
                                              String xPathExpression,
                                              NamespaceContext nsContext)
        Evaluate XPath expression with result type String.
        Parameters:
        node -
        xPathExpression -
        nsContext -
        Returns:
      • evaluateAsBoolean

        public static Boolean evaluateAsBoolean​(Node node,
                                                String xPathExpression,
                                                NamespaceContext nsContext)
        Evaluate XPath expression with result type Boolean value.
        Parameters:
        node -
        xPathExpression -
        nsContext -
        Returns:
      • evaluateAsNumber

        public static Double evaluateAsNumber​(Node node,
                                              String xPathExpression,
                                              NamespaceContext nsContext)
        Evaluate XPath expression with result type Number.
        Parameters:
        node -
        xPathExpression -
        nsContext -
        Returns:
      • evaluateAsObject

        public static Object evaluateAsObject​(Node node,
                                              String xPathExpression,
                                              NamespaceContext nsContext,
                                              QName resultType)
        Evaluate XPath expression.
        Parameters:
        node -
        xPathExpression -
        nsContext -
        Returns:
      • isXPathExpression

        public static boolean isXPathExpression​(String expression)
        Method to find out whether an expression is of XPath nature or custom dot notation syntax.
        Parameters:
        expression - the expression string to check.
        Returns:
        boolean the result.
      • evaluateExpression

        public static Object evaluateExpression​(Node node,
                                                String xPathExpression,
                                                NamespaceContext nsContext,
                                                QName returnType)
        Evaluates the expression.
        Parameters:
        node - the node.
        xPathExpression - the expression.
        nsContext - the context.
        returnType -
        Returns:
        the result.