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-kamelets.git
The following commit(s) were added to refs/heads/main by this push:
new 331da9ea3 Fix #3390: xpath-filter-action - a namespaces option for XML
with more than one namespace (#3391)
331da9ea3 is described below
commit 331da9ea3f87b3cde0aacd8385c053a405419018
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Nov 24 17:04:08 2026 +0100
Fix #3390: xpath-filter-action - a namespaces option for XML with more than
one namespace (#3391)
The new namespaces option takes several prefix=uri mappings separated by
commas, using the namespaceMappings option of the xpath language
(CAMEL-27486). namespacePrefix and namespaceUri stay; for the same prefix
they win. The test matches an order and its customer in two namespaces.
Claude-Session: https://claude.ai/code/session_016Y4dDmBDsSWV117kccuNf9
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
kamelets/xpath-filter-action.kamelet.yaml | 9 +++-
.../main/resources/META-INF/kamelets-index.json | 2 +-
.../kamelets/xpath-filter-action.kamelet.yaml | 9 +++-
.../resources/xml/xpath-filter-action-route.yaml | 56 ++++++++++++++++++++++
.../xml/xpath-filter-action.citrus.it.yaml | 13 +++++
5 files changed, 86 insertions(+), 3 deletions(-)
diff --git a/kamelets/xpath-filter-action.kamelet.yaml
b/kamelets/xpath-filter-action.kamelet.yaml
index 0aec7bfdd..fc8b2822c 100644
--- a/kamelets/xpath-filter-action.kamelet.yaml
+++ b/kamelets/xpath-filter-action.kamelet.yaml
@@ -40,7 +40,7 @@ spec:
Output: the same message, unchanged, when the expression matches: text
stays text, bytes stay bytes, a stream can still be read, and the headers are
kept. When it does not match, the message is dropped and nothing is sent on.
- Good to know: the expression matches when it selects at least one
element, such as `/order[amount > 100]`, or when it is a condition that is
true, such as `/order/customer = 'Ann'`. A message without a body is dropped. A
body that is not XML fails with an error such as `Content is not allowed in
prolog.`; it is not dropped silently, so the error handling of the integration
decides what happens to it. A message with a DOCTYPE fails too, so its entities
are never expanded and an enti [...]
+ Good to know: the expression matches when it selects at least one
element, such as `/order[amount > 100]`, or when it is a condition that is
true, such as `/order/customer = 'Ann'`. A message without a body is dropped. A
body that is not XML fails with an error such as `Content is not allowed in
prolog.`; it is not dropped silently, so the error handling of the integration
decides what happens to it. A message with a DOCTYPE fails too, so its entities
are never expanded and an enti [...]
required:
- expression
type: object
@@ -62,6 +62,12 @@ spec:
type: string
default: ""
example: "urn:example:orders"
+ namespaces:
+ title: Namespaces
+ description: "Several namespaces to use in the expression, each as
`prefix=uri`, separated by commas. For example,
`o=urn:example:orders,c=urn:example:customers` lets `/o:order[c:customer/c:name
= 'Ann']` match an order whose customer is in another namespace. A prefix that
is also set by `namespacePrefix` takes the URI of `namespaceUri`. A value
without `=` fails the integration when it starts."
+ type: string
+ default: ""
+ example: "o=urn:example:orders,c=urn:example:customers"
dataTypes:
in:
default: unchanged
@@ -87,6 +93,7 @@ spec:
- expression:
xpath:
expression: "{{expression}}"
+ namespaceMappings: "{{namespaces}}"
namespace:
- key: "{{namespacePrefix}}"
value: "{{namespaceUri}}"
diff --git
a/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
b/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
index 18537d18a..3d8240ba1 100644
--- a/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
+++ b/library/camel-kamelets/src/main/resources/META-INF/kamelets-index.json
@@ -2435,7 +2435,7 @@
"supportLevel" : "Preview",
"title" : "XPath Filter Action",
"summary" : "Keeps only the XML messages that match an XPath expression,
and drops all others.",
- "description" : "Keeps only the XML messages that match an XPath
expression, and drops all others.\n\nUse it to pass on only the XML documents
you care about, for example only the orders above a certain amount with
`/order[amount > 100]`, or only the paid invoices of an XML feed. For JSON, use
`predicate-filter-action`; to keep or drop messages by a header, use
`header-matches-filter-action`; to check XML against an XML Schema, use
`xml-schema-validator-action`.\n\nInput: XML as text [...]
+ "description" : "Keeps only the XML messages that match an XPath
expression, and drops all others.\n\nUse it to pass on only the XML documents
you care about, for example only the orders above a certain amount with
`/order[amount > 100]`, or only the paid invoices of an XML feed. For JSON, use
`predicate-filter-action`; to keep or drop messages by a header, use
`header-matches-filter-action`; to check XML against an XML Schema, use
`xml-schema-validator-action`.\n\nInput: XML as text [...]
"group" : "Actions",
"namespace" : "EIP",
"provider" : "Apache Software Foundation"
diff --git
a/library/camel-kamelets/src/main/resources/kamelets/xpath-filter-action.kamelet.yaml
b/library/camel-kamelets/src/main/resources/kamelets/xpath-filter-action.kamelet.yaml
index 0aec7bfdd..fc8b2822c 100644
---
a/library/camel-kamelets/src/main/resources/kamelets/xpath-filter-action.kamelet.yaml
+++
b/library/camel-kamelets/src/main/resources/kamelets/xpath-filter-action.kamelet.yaml
@@ -40,7 +40,7 @@ spec:
Output: the same message, unchanged, when the expression matches: text
stays text, bytes stay bytes, a stream can still be read, and the headers are
kept. When it does not match, the message is dropped and nothing is sent on.
- Good to know: the expression matches when it selects at least one
element, such as `/order[amount > 100]`, or when it is a condition that is
true, such as `/order/customer = 'Ann'`. A message without a body is dropped. A
body that is not XML fails with an error such as `Content is not allowed in
prolog.`; it is not dropped silently, so the error handling of the integration
decides what happens to it. A message with a DOCTYPE fails too, so its entities
are never expanded and an enti [...]
+ Good to know: the expression matches when it selects at least one
element, such as `/order[amount > 100]`, or when it is a condition that is
true, such as `/order/customer = 'Ann'`. A message without a body is dropped. A
body that is not XML fails with an error such as `Content is not allowed in
prolog.`; it is not dropped silently, so the error handling of the integration
decides what happens to it. A message with a DOCTYPE fails too, so its entities
are never expanded and an enti [...]
required:
- expression
type: object
@@ -62,6 +62,12 @@ spec:
type: string
default: ""
example: "urn:example:orders"
+ namespaces:
+ title: Namespaces
+ description: "Several namespaces to use in the expression, each as
`prefix=uri`, separated by commas. For example,
`o=urn:example:orders,c=urn:example:customers` lets `/o:order[c:customer/c:name
= 'Ann']` match an order whose customer is in another namespace. A prefix that
is also set by `namespacePrefix` takes the URI of `namespaceUri`. A value
without `=` fails the integration when it starts."
+ type: string
+ default: ""
+ example: "o=urn:example:orders,c=urn:example:customers"
dataTypes:
in:
default: unchanged
@@ -87,6 +93,7 @@ spec:
- expression:
xpath:
expression: "{{expression}}"
+ namespaceMappings: "{{namespaces}}"
namespace:
- key: "{{namespacePrefix}}"
value: "{{namespaceUri}}"
diff --git
a/tests/camel-kamelets-itest/src/test/resources/xml/xpath-filter-action-route.yaml
b/tests/camel-kamelets-itest/src/test/resources/xml/xpath-filter-action-route.yaml
index 58c19e5f3..91fac6f3a 100644
---
a/tests/camel-kamelets-itest/src/test/resources/xml/xpath-filter-action-route.yaml
+++
b/tests/camel-kamelets-itest/src/test/resources/xml/xpath-filter-action-route.yaml
@@ -134,6 +134,62 @@
- setVariable:
name: "global:ns"
simple: "${variable.global:ns}${header.CamelTimerCounter},"
+# Two namespaces through the namespaces option: the order and its customer are
in different namespaces
+- route:
+ from:
+ uri: "timer:multi"
+ parameters:
+ repeatCount: 6
+ period: 10
+ includeMetadata: true
+ steps:
+ - setBody:
+ constant: '<o:order xmlns:o="urn:example:orders"
xmlns:c="urn:example:customers"><c:customer><c:name>Ann</c:name></c:customer></o:order>'
+ - choice:
+ when:
+ - simple: "${header.CamelTimerCounter} == 2"
+ steps:
+ - setBody:
+ constant: '<o:order xmlns:o="urn:example:orders"
xmlns:c="urn:example:customers"><c:customer><c:name>Bob</c:name></c:customer></o:order>'
+ - simple: "${header.CamelTimerCounter} == 3"
+ steps:
+ - setBody:
+ constant: '<order
xmlns="urn:example:orders"><customer><name>Ann</name></customer></order>'
+ - simple: "${header.CamelTimerCounter} == 6"
+ steps:
+ - log: "Multi kept: ${variable.global:multi}|"
+ - stop: {}
+ - choice:
+ when:
+ # Spaces around the mappings are ignored
+ - simple: "${header.CamelTimerCounter} == 4"
+ steps:
+ - to:
+ uri: "kamelet:xpath-filter-action"
+ parameters:
+ expression: "/o:order[c:customer/c:name = 'Ann']"
+ namespaces: "o=urn:example:orders,
c=urn:example:customers"
+ # namespacePrefix wins over a mapping of the same prefix
+ - simple: "${header.CamelTimerCounter} == 5"
+ steps:
+ - to:
+ uri: "kamelet:xpath-filter-action"
+ parameters:
+ expression: "/o:order[c:customer/c:name = 'Ann']"
+ namespaces:
"o=urn:example:wrong,c=urn:example:customers"
+ namespacePrefix: "o"
+ namespaceUri: "urn:example:orders"
+ otherwise:
+ steps:
+ - to:
+ uri: "kamelet:xpath-filter-action"
+ parameters:
+ expression: "/o:order[c:customer/c:name = 'Ann']"
+ namespaces:
"o=urn:example:orders,c=urn:example:customers"
+ - log: "MultiKept ${header.CamelTimerCounter}: ${body}|"
+ - setVariable:
+ name: "global:multi"
+ simple: "${variable.global:multi}${header.CamelTimerCounter},"
- route:
from:
uri: "timer:xxe"
diff --git
a/tests/camel-kamelets-itest/src/test/resources/xml/xpath-filter-action.citrus.it.yaml
b/tests/camel-kamelets-itest/src/test/resources/xml/xpath-filter-action.citrus.it.yaml
index 8d2575d10..70af3117f 100644
---
a/tests/camel-kamelets-itest/src/test/resources/xml/xpath-filter-action.citrus.it.yaml
+++
b/tests/camel-kamelets-itest/src/test/resources/xml/xpath-filter-action.citrus.it.yaml
@@ -62,6 +62,19 @@ actions:
verify:
integration: "xpath-filter-action-route"
logMessage: 'Namespace kept: 1,5,|'
+ # namespaces declares both prefixes: the order of Ann is kept (1), not the
one of Bob (2), nor the one whose customer
+ # is in the namespace of the order (3); spaces after a comma are ignored
(4); namespacePrefix wins over a mapping
+ # of the same prefix (5)
+ - camel:
+ cli:
+ verify:
+ integration: "xpath-filter-action-route"
+ logMessage: 'MultiKept 1: <o:order xmlns:o="urn:example:orders"
xmlns:c="urn:example:customers"><c:customer><c:name>Ann</c:name></c:customer></o:order>|'
+ - camel:
+ cli:
+ verify:
+ integration: "xpath-filter-action-route"
+ logMessage: 'Multi kept: 1,4,5,|'
# A message with a DOCTYPE fails: the external entity is never read
- camel:
cli: