问题
I have a very simple hashmap payload being converted to application/json. I have done this in other flows by just dragging the dataweave component in, and writing the mapping expression.
now for some reason, I am getting the following error:
The prefix "metadata" for attribute "metadata:id" associated with an element type "dw:transform-message" is not bound.
when I look at the xml, I see that my new dataweave component does not have a metadata:id attribute, but the other dataweave component i used does have a metadata:id attribute.
- why are these acting differently?
- why is a metadata:id not getting auto generated like it was for the other flow?
- why do I need a metadata:id attribute in the first place? in both cases I use a JSON to Object component prior to calling the mapper.
回答1:
When you use DataWeave component, you need to declare xml namespaces. If you are using Studio designer, then Studio adds related namespaces as soon as you drag and drop components to your configuration.
So when you drag and drop dataweave component, studio would add below namespaces and schema location to config -
xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
In Mule, you can define MetaData for every component which would help to see data structure at design time. All these metadata definitions are stored under {project_home}\catalog
folder with file names that looks like UUID
. These file names are then added to your component definitions with metadata:id
attribute. You need metadata namespace even if one component (doesnt have to DW as metadata is feature common to all components).
<dw:transform-message metadata:id="262e6569-8f38-4e0b-a61d-15550870101e" doc:name="Transform Message">
If you add metadata from Studio designer then Studio should automatically add below namespace and schema location. If you add it manually or copy-paste from one xml to another which doesn't have it then you would need to add it your self -
xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata"
Example config with Dataweave and metadata can look like below -
<mule xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
回答2:
I believe the inclusion of the metadata
attribute is to point to a resource where you've defined some metadata for your DataWeave input and/or output structure off the back of a sample file. The error you've detailed above looks like schema validation, please check you have the following in your opening mule
tag in the XML config:
xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata"
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
Hope that helps!
来源:https://stackoverflow.com/questions/36511445/mule-dataweave-transform-message-failing-with-missing-metadataid-attribute