marcinwieckowski opened a new pull request, #25843:
URL: https://github.com/apache/camel/pull/25843
# Description
Adds a `namespacesRef` attribute to `NamespaceAwareExpression` (the shared
base class for
`XPathExpression`, `XQueryExpression` and
`XMLTokenizerExpression`/xtokenize), letting users
reference a registry bean instead of declaring namespaces inline.
**Why**: today, namespace-aware languages only support namespaces declared
inline via
`<namespace>` child elements (or programmatically via a `Map`/`Namespaces`
object in Java DSL).
There is no way to define a reusable set of namespace mappings once and
reference it by name
from multiple expressions/routes, which becomes repetitive for XML payloads
with many
namespaces reused across a route's XPath conditions.
**How**:
- `namespacesRef` is a plain string attribute (bean id) on
`NamespaceAwareExpression`, marked
`@Metadata(label = "advanced")`, and inherited by XPath, XQuery and
Xtokenize.
- Each reifier (`XPathExpressionReifier`, `XQueryExpressionReifier`,
`XMLTokenizerExpressionReifier`) resolves the namespaces in
`createProperties()`, i.e.
**before the language builds the expression**, via a private
`resolveNamespaces()`. If no
inline namespaces are present, the bean is looked up with
`mandatoryLookup(ref, Namespaces.class)` - intentionally typed to
`org.apache.camel.support.builder.Namespaces`, the same class used in Java
DSL for xpath
namespaces - and the result is passed through `parseMap()` so property
placeholders resolve.
- Resolving before the expression is built (rather than afterwards via
`configureExpression`/`configurePredicate`) is required for Saxon, which
resolves prefixes at
compile time; injecting namespaces after the fact fails with "Namespace
prefix has not been
declared". This is what blocked the earlier attempt in #25561.
- Inline `<namespace>` elements still take priority when present, for
backward compatibility.
- Works across all three configuration surfaces: Java DSL (model setter plus
a `namespacesRef`
method on the shared expression builder), XML DSL and YAML DSL - including
defining the
`Namespaces` bean itself declaratively (XML `<bean>` / YAML `beans:`),
using Camel's
`foo[bar]` map-syntax property binding against
`Namespaces.getNamespaces()`.
**Tests**: four, one per configuration surface plus a Saxon regression test.
`camel-core`, `camel-xml-io-dsl` and `camel-yaml-dsl` each cover a bean with
two namespace
prefixes, a positive match per namespace, a data-mismatch negative (correct
namespace, wrong
value) and a namespace-mismatch negative (correct value, wrong namespace
URI).
`camel-saxon` repeats this against a Saxon `XPathFactory`, and additionally
covers the
`resultType` path, to pin the compile-time resolution behaviour described
above.
**Documentation**: new sections in the XPath, XQuery and XML Tokenize
language pages showing the
bean definition and `namespacesRef` usage in XML and YAML, and noting that
inline namespaces
win. The XML Tokenize page had no namespaces section at all previously.
Supersedes #25561, which I closed myself pending the Saxon issue now fixed
here.
# Target
- [x] I checked that the commit is targeting the correct branch (Camel 4
uses the `main` branch)
# Tracking
- [x] If this is a large change, bug fix, or code improvement, I checked
there is a
[JIRA issue](https://issues.apache.org/jira/browse/CAM
(I didn't create a JIRA for this feature. It is something we needed in our
internal
project and decided it is general and useful enough to share)
# Apache Camel coding standards and style
- [x] I checked that each commit in the pull request hasd body.
- [x] I have run `mvn clean install -DskipTests` locally from root folder
and I have committed
all auto-generated changes.
# AI-assisted contributions
- [x] If this PR includes AI-generated code, commits havution
(e.g., `Co-authored-by` trailers) and the PR description identifies the AI
tool used.
_Written with Claude Code (Claude Opus 5) on behalf of @marcinwieckowski._
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]