{"record":{"id":"9c07cea292303d4a","repo":"code4craft/webmagic","slug":"xpath-error","errorCode":null,"errorMessage":"XPath error!","messagePattern":"XPath error!","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"webmagic-saxon/src/main/java/us/codecraft/webmagic/selector/Xpath2Selector.java","lineNumber":48,"sourceCode":" *\n * @author code4crafter@gmail.com, hooy <br>\n * Date: 13-4-21\n * Time: 上午9:39\n */\npublic class Xpath2Selector implements Selector, NodeSelector {\n\n    private final String xpathStr;\n\n    private XPathExpression xPathExpression;\n\n    private final Logger logger = LoggerFactory.getLogger(getClass());\n\n    public Xpath2Selector(String xpathStr) {\n        this.xpathStr = xpathStr;\n        try {\n            init();\n        } catch (XPathExpressionException e) {\n            throw new IllegalArgumentException(\"XPath error!\", e);\n        }\n    }\n\n    public static Xpath2Selector newInstance(String xpathStr) {\n        return new Xpath2Selector(xpathStr);\n    }\n\n    enum XPath2NamespaceContext implements NamespaceContext {\n\n        INSTANCE;\n\n        private final Map<String, String> prefix2NamespaceMap = new ConcurrentHashMap<>();\n\n        private final Map<String, List<String>> namespace2PrefixMap = new ConcurrentHashMap<>();\n\n        private void put(String prefix, String namespaceURI) {\n            prefix2NamespaceMap.put(prefix, namespaceURI);\n            List<String> prefixes = namespace2PrefixMap.computeIfAbsent(namespaceURI, k -> new ArrayList<>());","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/code4craft/webmagic/blob/67816a19d68a4fec4657bf1336227e046e251df2/webmagic-saxon/src/main/java/us/codecraft/webmagic/selector/Xpath2Selector.java#L30-L66","documentation":"Xpath2Selector's constructor compiles the XPath 2.0 expression with Saxon (init()). If the expression is syntactically invalid or not valid XPath 2.0, XPathExpressionException is wrapped into IllegalArgumentException \"XPath error!\". The selector can never be used once this fails.","triggerScenarios":"Calling new Xpath2Selector(str) or Xpath2Selector.newInstance(str) with a malformed expression: unbalanced parentheses/brackets, invalid axis or function names, XPath 3.0-only syntax, stray characters, or empty string.","commonSituations":"Copying XPath written for HTMLCleaner/jsoup/XPath 1.0 syntax that Saxon rejects; typos like '//div[@class=\"x\"' with missing bracket; using functions unsupported in XPath 2.0; constructing selectors dynamically from user input.","solutions":["Validate and fix the XPath string syntax; test it in a standalone Saxon/XPath 2.0 evaluator first.","Ensure quotes inside the expression are balanced and escape embedded quotes properly in Java string literals.","Remove or rewrite functions/syntax not available in XPath 2.0.","Pre-compile expressions at startup (fail fast) and wrap construction in try-catch to surface the bad selector name."],"exampleFix":"// before\nnew Xpath2Selector(\"//div[@class='item'\");\n// after\nnew Xpath2Selector(\"//div[@class='item']\");","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    selector = new Xpath2Selector(xpathStr);\n} catch (IllegalArgumentException e) {\n    throw new IllegalArgumentException(\"Invalid XPath expression: \" + xpathStr, e);\n}","preventionTips":["Test every XPath string in a Saxon XPath 2.0 evaluator before embedding it","Compile selectors once at startup (fail fast) rather than per page","Escape Java string quotes correctly when embedding XPath literals","Validate user-supplied XPath with a dry-run compile before use"],"tags":["java","xpath","saxon","parser"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"67816a19d68a4fec4657bf1336227e046e251df2","analyzedAt":"2026-09-08T11:15:28.425Z","contentChangedAt":"2026-09-08T11:15:28.425Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}