This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 99a36d32dca6 CAMEL-22763: Add support for exchange variables in XPath 
expressions (#20313)
99a36d32dca6 is described below

commit 99a36d32dca6bfd227f45d77e28338a23a643b8e
Author: jva70 <[email protected]>
AuthorDate: Tue Dec 9 13:16:58 2025 +0100

    CAMEL-22763: Add support for exchange variables in XPath expressions 
(#20313)
---
 .../camel-xpath/src/main/docs/xpath-language.adoc  | 20 ++++++++++
 .../language/xpath/MessageVariableResolver.java    |  3 ++
 .../apache/camel/language/XPathVariableTest.java   | 46 ++++++++++++++++++++++
 3 files changed, 69 insertions(+)

diff --git a/components/camel-xpath/src/main/docs/xpath-language.adoc 
b/components/camel-xpath/src/main/docs/xpath-language.adoc
index 09378d3c0b7e..eacbb789aba0 100644
--- a/components/camel-xpath/src/main/docs/xpath-language.adoc
+++ b/components/camel-xpath/src/main/docs/xpath-language.adoc
@@ -66,6 +66,26 @@ local part. Camel will try to resolve a variable in the 
following steps:
 * from `variables` that has been set using the `variable(name, value)` fluent 
builder
 * from `message.in.header` if there is a header with the given key
 * from `exchange.properties` if there is a property with the given key
+* from `exchange.variables` if there is a variable with the given key
+
+==== Example of using variables, header and body in XPath expression
+You can combine headers, properties, body and variables in an XPath expression 
as shown in the example below:
+[source,java]
+----
+from("timer:java?period=1000,repeatCount=1")
+    .setBody()
+        .simple("<dummy>YES</dummy>")
+    .setVariable("Foo", simple("<hello><id>123</id></hello>", Document.class))
+    .setVariable("Bar", simple("<holla><id>123</id></holla>", Document.class))
+    .setHeader("Baz", simple("<hallo><id>123</id></hallo>", Document.class))
+    .choice()
+        .when(xpath("($Foo/hello/id = $Bar/holla/id) and /dummy='YES' and 
$Baz/hallo/id=123"))
+            .log("EQUAL")
+        .otherwise()
+            .log("DIFFERENT")
+    .end();
+}
+----
 
 == Functions
 
diff --git 
a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/MessageVariableResolver.java
 
b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/MessageVariableResolver.java
index 935cc90937ae..ec43edcffdfd 100644
--- 
a/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/MessageVariableResolver.java
+++ 
b/components/camel-xpath/src/main/java/org/apache/camel/language/xpath/MessageVariableResolver.java
@@ -65,6 +65,9 @@ public class MessageVariableResolver implements 
XPathVariableResolver {
                 if (answer == null) {
                     answer = exchange.get().getProperty(localPart);
                 }
+                if (answer == null) {
+                    answer = exchange.get().getVariable(localPart);
+                }
             }
         } else if (uri.equals(SYSTEM_PROPERTIES_NAMESPACE)) {
             try {
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/language/XPathVariableTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/language/XPathVariableTest.java
new file mode 100644
index 000000000000..0b3369dcfe63
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/language/XPathVariableTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.language;
+
+import org.w3c.dom.Document;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class XPathVariableTest extends ContextTestSupport {
+
+    @Test
+    public void testXPathVariable() throws Exception {
+        Object out = template.requestBody("direct:start", "<dummy/>");
+        assertIsInstanceOf(Integer.class, out);
+        Assertions.assertEquals(123, (Integer) out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start")
+                        .setVariable("Test-Variable", 
simple("<hello><id>123</id></hello>", Document.class))
+                        .setBody(xpath("$Test-Variable/hello/id", 
Integer.class));
+            }
+        };
+    }
+}

Reply via email to