26.5 Routing xml messages using XPath

Two Router implementations based on XPath are provided XPathSingleChannelRouter and XPathMultiChannelRouter. The implementations differ in respect to how many channels any given message may be routed to, exactly one in the case of the single channel version or zero or more in the case of the multichannel router. Both evaluate an XPath expression against the xml payload of the message, supported payload types by default are Node, Document and String. For other payload types a custom implementation of XmlPayloadConverter can be provided. The router implementations use ChannelResolver to convert the result(s) of the XPath expression to a channel name. By default a BeanFactoryChannelResolver strategy will be used, this means that the string returned by the XPath evaluation should correspond directly to the name of a channel. Where this is not the case an alternative implementation of ChannelResolver can be used. Where there is a simple mapping from Xpath result to channel name the provided MapBasedChannelResolver can be used.

<!-- Expects a channel for each value of order type to exist  -->
<bean id="singleChannelRoutingEndpoint"
      class="org.springframework.integration.endpoint.SubscribingConsumerEndpoint">
    <constructor-arg>
        <bean class="org.springframework.integration.xml.router.XPathSingleChannelRouter">
            <constructor-arg value="/order/@type" />
        </bean>
    </constructor-arg>
	<constructor-arg ref="orderChannel" />
</bean>
	
	
<!-- Multi channel router which uses a map channel resolver to resolve the channel name
     based on the XPath evaluation result Since the router is multi channel it may deliver
     message to one or both of the configured channels -->
<bean id="multiChannelRoutingEndpoint"
      class="org.springframework.integration.endpoint.SubscribingConsumerEndpoint">
    <constructor-arg>
        <bean class="org.springframework.integration.xml.router.XPathMultiChannelRouter">
            <constructor-arg value="/order/recipient" />
            <property name="channelResolver">
                <bean class="org.springframework.integration.channel.MapBasedChannelResolver">
                    <constructor-arg>
                        <map>
                            <entry key="accounts"
                                   value-ref="accountConfirmationChannel" />
                            <entry key="humanResources"
                                   value-ref="humanResourcesConfirmationChannel" />
                        </map>
                     </constructor-arg>
	             </bean>
            </property>
        </bean>
    </constructor-arg>
    <constructor-arg ref="orderChannel" />
</bean>