{"record":{"id":"6fb047902fa02b78","repo":"flowable/flowable-engine","slug":"could-not-evaluate-xpath-expression-xpathexpress","errorCode":null,"errorMessage":"Could not evaluate xpath expression ${xpathExpression}","messagePattern":"Could not evaluate xpath expression (.+?)","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-event-registry/src/main/java/org/flowable/eventregistry/impl/keydetector/XpathBasedInboundEventKeyDetector.java","lineNumber":42,"sourceCode":"/**\n * @author Joram Barrez\n */\npublic class XpathBasedInboundEventKeyDetector implements InboundEventKeyDetector<Document> {\n\n    protected String xpathExpression;\n\n    public XpathBasedInboundEventKeyDetector(String xpathExpression) {\n        this.xpathExpression = xpathExpression;\n    }\n\n    @Override\n    public String detectEventDefinitionKey(Document payload) {\n        try {\n            XPath xPath = XPathFactory.newInstance().newXPath();\n            Node result = (Node) xPath.compile(xpathExpression).evaluate(payload, XPathConstants.NODE);\n            return result.getTextContent();\n        } catch (Exception e) {\n            throw new FlowableException(\"Could not evaluate xpath expression \" + xpathExpression, e);\n        }\n    }\n\n}\n","sourceCodeStart":24,"sourceCodeEnd":47,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-event-registry/src/main/java/org/flowable/eventregistry/impl/keydetector/XpathBasedInboundEventKeyDetector.java#L24-L47","documentation":"XpathBasedInboundEventKeyDetector extracts the event definition key from an inbound XML payload by compiling and evaluating a configured XPath expression expected to return a single Node. Any failure — malformed XPath, evaluation errors, or a non-node result (null cast failure) — is wrapped in a FlowableException that includes the xpathExpression.","triggerScenarios":"Calling detectEventDefinitionKey(Document) when the configured xpathExpression is syntactically invalid, or when it evaluates to no node (null) so the cast to Node fails, while processing an inbound XML event payload.","commonSituations":"XPath configured against a different XML schema than the actual payload (element renamed, namespace prefixes missing); wrong XPath dialect; test fixtures with payloads lacking the key element.","solutions":["Validate the XPath expression syntax and test it against a real payload (e.g. in an XPath tester) before wiring the detector","Ensure the payload actually contains the element targeted by the XPath, including required namespaces","Adjust the XPath to be namespace-aware or prefix-free as appropriate for the document","Wrap inbound event processing with error handling and fall back to a header-based or default key detector for malformed payloads"],"exampleFix":"// before\ndetector.setXpathExpression(\"/event/key\"); // payload uses namespace: no match -> NPE -> wrapped\n// after\ndetector.setXpathExpression(\"//*[local-name()='key']\");","handlingStrategy":"try-catch","validationCode":"// pre-validate against a sample payload\nNode n = (Node) XPathFactory.newInstance().newXPath()\n    .compile(xpathExpression).evaluate(sampleDocument, XPathConstants.NODE);\nif (n == null) throw new IllegalStateException(\"XPath matches no node in sample payload\");","typeGuard":null,"tryCatchPattern":"try {\n    String key = detector.detectEventDefinitionKey(payload);\n} catch (FlowableException e) {\n    if (e.getMessage().startsWith(\"Could not evaluate xpath\")) {\n        // log payload + xpathExpression, route to dead-letter / default key handling\n    }\n}","preventionTips":["Test the XPath against real payloads before production","Use namespace-agnostic expressions (local-name()) when namespaces may vary","Log the failing payload and expression from the wrapped cause"],"tags":["flowable","event-registry","xpath","xml","key-detection"],"backgroundTag":"xpath-evaluation-failed","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-18T11:17:12.947Z"}