{"record":{"id":"81da36ee9aaa77df","repo":"jhy/jsoup","slug":"could-not-evaluate-xpath-query-s-s","errorCode":null,"errorMessage":"Could not evaluate XPath query [%s]: %s","messagePattern":"Could not evaluate XPath query \\[(.+?)\\]: (.+?)","errorType":"exception","errorClass":"Selector.SelectorParseException","httpStatus":null,"severity":"error","filePath":"src/main/java/org/jsoup/helper/W3CDom.java","lineNumber":289,"sourceCode":"     @return the matches nodes\n     */\n    public NodeList selectXpath(String xpath, Node contextNode) {\n        Validate.notEmptyParam(xpath, \"xpath\");\n        Validate.notNullParam(contextNode, \"contextNode\");\n\n        NodeList nodeList;\n        try {\n            // if there is a configured XPath factory, use that instead of the Java base impl:\n            String property = System.getProperty(XPathFactoryProperty);\n            final XPathFactory xPathFactory = property != null ?\n                XPathFactory.newInstance(\"jsoup\") :\n                XPathFactory.newInstance();\n\n            XPathExpression expression = xPathFactory.newXPath().compile(xpath);\n            nodeList = (NodeList) expression.evaluate(contextNode, XPathConstants.NODESET); // love the strong typing here /s\n            Validate.notNull(nodeList);\n        } catch (XPathExpressionException | XPathFactoryConfigurationException e) {\n            throw new Selector.SelectorParseException(\n                e, \"Could not evaluate XPath query [%s]: %s\", xpath, e.getMessage());\n        }\n        return nodeList;\n    }\n\n    /**\n     Retrieves the original jsoup DOM nodes from a nodelist created by this convertor.\n     @param nodeList the W3C nodes to get the original jsoup nodes from\n     @param nodeType the jsoup node type to retrieve (e.g. Element, DataNode, etc)\n     @param <T> node type\n     @return a list of the original nodes\n     */\n    public <T extends org.jsoup.nodes.Node> List<T> sourceNodes(NodeList nodeList, Class<T> nodeType) {\n        Validate.notNull(nodeList);\n        Validate.notNull(nodeType);\n        List<T> nodes = new ArrayList<>(nodeList.getLength());\n\n        for (int i = 0; i < nodeList.getLength(); i++) {","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/jhy/jsoup/blob/9851ac5d9c576c6888910b5a51a2362bbc978959/src/main/java/org/jsoup/helper/W3CDom.java#L271-L307","documentation":"W3CDom.selectXpath compiles and evaluates an XPath expression against a W3C DOM context node. If the XPath engine throws XPathExpressionException or XPathFactoryConfigurationException, jsoup wraps it in a Selector.SelectorParseException with the query and engine message. It indicates the XPath was syntactically invalid, used unsupported functions/axes, or the XPath factory was misconfigured.","triggerScenarios":"Passing a malformed XPath string (bad syntax, unknown function, invalid axis) to W3CDom.selectXpath/Jsoup.selectXpath; using an XPath feature unsupported by the JDK's default factory; a system property like javax.xml.xpath.XPathFactory:<uri> pointing to a missing/broken factory class.","commonSituations":"Typos in XPath (e.g. unbalanced brackets, missing @ on attributes); assuming XPath 2.0+ features (matches(), for-expressions) that JDK 1.0 XPath doesn't support; JVM-wide XPathFactory configuration overriding the default implementation.","solutions":["Check the engine message after the colon for the exact syntax error at the reported offset.","Test the query in a standalone XPath evaluator; fix syntax (e.g. use @attr, correct predicates).","Remove or fix javax.xml.xpath.XPathFactory system properties that install a broken custom factory.","Simplify to XPath 1.0-compatible expressions if using unsupported functions.","Catch Selector.SelectorParseException for user-supplied queries and surface a friendly error."],"exampleFix":"// before\nW3CDom.selectXpath(\"//div[@class=\\\"x\\\"]\", doc); // wrong quoting\n// after\nW3CDom.selectXpath(\"//div[@class='x']\", doc);","handlingStrategy":"try-catch","validationCode":"// basic XPath sanity check before calling\nif (xpath == null || xpath.isBlank() || xpath.contains(\"[\") != xpath.contains(\"]\")) throw new IllegalArgumentException(\"malformed xpath\");","typeGuard":null,"tryCatchPattern":"try { W3CDom.selectXpath(xpath, w3cDoc); } catch (org.jsoup.select.Selector.SelectorParseException e) { log.error(\"Bad XPath '{}': {}\", xpath, e.getMessage()); }","preventionTips":["Validate user-supplied XPath against XPath 1.0 grammar (JDK default engine).","Avoid overriding javax.xml.xpath.XPathFactory system properties unless necessary.","Unit-test XPath queries against a fixed sample document.","Escape quotes consistently: single quotes inside double-quoted attribute predicates."],"tags":["xpath","parse-error","selector","w3c-dom"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"9851ac5d9c576c6888910b5a51a2362bbc978959","analyzedAt":"2026-09-08T15:22:04.931Z","contentChangedAt":"2026-09-08T15:22:04.931Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}