如何在NiFi中提取XML命名空间属性d:bar到FlowFile属性?
Got it, let's break down why your current EvaluateXPath setup isn't working and get this sorted out—no need to modify your original FlowFile content at all.
Why Your Original Expression Failed
Your XPath //*:foo/@d:bar isn't hitting the mark for two likely reasons:
- Unmapped Namespace Prefix: The
dprefix on thebarattribute either doesn't have a correspondingxmlns:ddeclaration in your XML (which makes the XML technically invalid, but NiFi might still process it) or you didn't map thednamespace in the EvaluateXPath processor. XPath engines require explicit mappings for any prefixes you use in expressions. - Element Namespace Mismatch: Your element is
a:foo, but*:foomatches any namespace-prefixedfoo—but if thed:barattribute's prefix isn't resolved, the engine still can't locate it.
Solution 1: Fix EvaluateXPath with Namespace-Agnostic XPath
The easiest way to bypass namespace headaches here is to use local-name() in your XPath to match elements and attributes by their un-prefixed names, regardless of their namespace (or missing namespace declarations):
- Open your EvaluateXPath processor's configuration
- In the Attributes to Set section:
- Set the property name (e.g.,
foo) to this expression://*[local-name()='foo']/@*[local-name()='bar']
- Set the property name (e.g.,
- Make sure the Return Type is set to
Node Value(this tells NiFi to pull the actual value of the attribute, not the attribute node itself) - Save and run the processor
Solution 2: Use ExtractXmlAttributes (Simpler Alternative)
If you prefer a processor built specifically for this use case, ExtractXmlAttributes is perfect—it’s designed to pull XML attributes directly into FlowFile properties with less setup:
- Add an ExtractXmlAttributes processor to your flow
- In the Attributes section:
- Set your target property name (e.g.,
foo) to the same namespace-agnostic XPath from above://*[local-name()='foo']/@*[local-name()='bar']
- Set your target property name (e.g.,
- No extra return type configuration needed—this processor automatically extracts attribute values
Bonus: If You Do Have Valid Namespaces
If your actual XML includes proper namespace declarations for a and d (e.g., <a:foo xmlns:a="http://example.com/a" xmlns:d="http://example.com/d" d:bar="eleven">), you can use explicit namespace mappings in EvaluateXPath:
- In the processor’s Namespaces property, add:
a=http://example.com/a d=http://example.com/d - Then use the cleaner XPath:
//a:foo/@d:bar
Either of these methods will pull the eleven value into your FlowFile's foo property without touching the original content.
内容的提问来源于stack exchange,提问作者DumbNewbie

