Contact Us 1-800-596-4880

XML Namespaces

When dealing with XML documents in Mule you need to declare any namespaces used by the document. You can specify a namespace globally so that it can be used by XPath expressions across Mule. You can declare the namespace in any XML file in your Mule instance. To declare a namespace, include the mule-xml.xsd schema in your XML file:

<mule xmlns="http://www.mulesoft.org/schema/mule/core"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
       xsi:schemaLocation="
           http://www.mulesoft.org/schema/mule/xml
           http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
           http://www.mulesoft.org/schema/mule/core
           http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

Next, specify the <namespace-manager> element, and then add one or more <namespace> elements within it to declare the prefix and URI of each namespace you want to add to the namespace manager. If you already declared a namespace at the top of the file in the <mule> element, you can set the includeConfigNamespaces attribute to true to have the namespace manager pick up those namespaces as well.

<mulexml:namespace-manager includeConfigNamespaces="true">
    <mulexml:namespace prefix="foo" uri="http://foo.com"/>
  </mulexml:namespace-manager>

You can also declare a namespace locally in an expression filter, router, or transformer using the <namespace> element without the <namespace-manager> element. You can then use that prefix within the XPath expression. For example, the following Jaxen filter declares a namespace with the prefix "e", which is then used in the filter expression:

<outbound>
  <filtering-router>
    <outbound-endpoint address="vm://echo" synchronous="true"/>
    <mulexml:jaxen-filter pattern="/e:purchaseOrder/e:shipTo/@country" expectedValue="US">
      <mulexml:namespace prefix="e" uri="http://www.example.com"/>
    </mulexml:jaxen-filter>
  </filtering-router>
....
</outbound>

If you had a global namespace with the "e" prefix, the local namespace URI would override the global namespace URI.

You can specify the namespace on any XML-based functionality in Mule, including the JXPath filter, Jaxen filter, XPath filter, filter-based splitter, expression splitter, round-robin splitter, JXPath extractor transformer, and XPath expression transformer in XML Module Overview and XPath Annotation.

View on GitHub