{"record":{"id":"9c6bbbb78eb198d9","repo":"flowable/flowable-engine","slug":"could-not-evaluate-xpath-expression-xpathexpress-9c6bbb","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/tenantdetector/XpathBasedInboundEventTenantDetector.java","lineNumber":42,"sourceCode":"/**\n * @author Joram Barrez\n */\npublic class XpathBasedInboundEventTenantDetector implements InboundEventTenantDetector<Document> {\n\n    protected String xpathExpression;\n\n    public XpathBasedInboundEventTenantDetector(String xpathExpression) {\n        this.xpathExpression = xpathExpression;\n    }\n\n    @Override\n    public String detectTenantId(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    public String getXpathExpression() {\n        return xpathExpression;\n    }\n    public void setXpathExpression(String xpathExpression) {\n        this.xpathExpression = xpathExpression;\n    }\n}\n","sourceCodeStart":24,"sourceCodeEnd":53,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-event-registry/src/main/java/org/flowable/eventregistry/impl/tenantdetector/XpathBasedInboundEventTenantDetector.java#L24-L53","documentation":"XpathBasedInboundEventTenantDetector.detectTenantId compiles its configured xpathExpression and evaluates it against the XML payload Document, expecting a Node whose text content is the tenant id. Any failure — invalid XPath syntax, non-node result, null result leading to NPE, or evaluation error — is wrapped in FlowableException 'Could not evaluate xpath expression <expr>'.","triggerScenarios":"detectTenantId(payload) called (directly or via tenantId/test code) when the configured xpathExpression is syntactically invalid, matches no node (evaluate returns null and result.getTextContent() NPEs), or matches a non-Node result type; also when the payload Document is incompatible with the expression.","commonSituations":"Typo'd XPath in tenant-detector configuration; XPath written for a document where the tenant element is absent (testDetectTenantIdMissingTenantIdInXml); namespace-qualified XML where the plain XPath matches nothing; wrong detector configured for JSON payloads.","solutions":["Verify the xpathExpression syntax with an XPath tester or an xPath.compile() call outside the flow.","Check the payload actually contains an element matching the expression (missing matches yield null -> NPE inside the try).","Account for XML namespaces — use namespace-aware XPath or local-name() predicates if the document uses namespaces.","Confirm this detector is only applied to XML payloads, not JSON.","After fixing, the detector returns result.getTextContent(); ensure the matched node's text is the tenant id."],"exampleFix":"// before\nxpathExpression = \"/order/tenanttId\"; // typo -> no match -> NPE\n\n// after\nxpathExpression = \"/order/tenantId\";","handlingStrategy":"validation","validationCode":"try {\n    XPathFactory.newInstance().newXPath().compile(xpathExpression);\n} catch (XPathExpressionException e) {\n    throw new IllegalArgumentException(\"Invalid tenant-detector xpath: \" + xpathExpression, e);\n}\n// xpath syntax is valid; also confirm a node matches in test payloads","typeGuard":null,"tryCatchPattern":"try {\n    String tenantId = detector.detectTenantId(document);\n} catch (FlowableException e) {\n    logger.error(\"Tenant detection failed for xpath: {}\", detector.getXpathExpression(), e.getCause());\n    throw e;\n}","preventionTips":["Test the configured xpathExpression against representative payloads in unit tests (including missing-element cases).","Use namespace-aware XPath (or local-name()) for namespaced documents.","Keep the detector applied only to XML payloads.","Ensure the matched node's text content is exactly the tenant id."],"tags":["flowable","event-registry","xpath","tenant-detection","xml"],"backgroundTag":"invalid-regex-pattern","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"}