|
Page Edited :
SM :
XPath Router
XPath Router has been edited by Aaron Mulder (Mar 04, 2008). Content:Its often a requirement to perform content based routing in an ESB. This means you route messages around your service bus based on the message properties or the content of the messages. When integrating systems across language boundaries its common to use XML as a universal message format; so XPath is an ideal tool to perform content based routing and transformation. We support a XPath based routing and transformation mechanism designed for high performance and power. ExampleThe following is an example routing and transformation template. Firstly we reuse the XSLT basd transform component. <sm:activationSpec componentName="transformer" service="foo:transformer"> <sm:component><bean class="org.apache.servicemix.components.xslt.XsltComponent"> <property name="xsltResource" value="classpath:org/apache/servicemix/components/xslt/router.xsl"/>
<!-- lets disable automatic output of the result of the transform; only if we perform
an invoke in the XSLT will we invoke another endpoint -->
<property name="disableOutput" value="true"/>
</bean></sm:component>
</sm:activationSpec>
Then we provide a file in XSLT format, with some JBI extensions <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:jbi="xalan://org.apache.servicemix.components.xslt.XalanExtension" extension-element-prefixes="jbi" xmlns:foo="http://servicemix.org/cheese/" version="1.0"> <xsl:template match="/*"> <xsl:choose> <!-- lets forward the inbound message to a service --> <xsl:when test="@id = '4'"> <jbi:forward service="foo:trace"/> </xsl:when> <!-- lets generate the output XML to use as input, copy the input properties and define some new propertes --> <xsl:when test="@id = '12'"> <jbi:invoke service="foo:script"> <jbi:copyProperties/> <jbi:setOutProperty name="foo" select="@sent"/> <cheese code="[EMAIL PROTECTED]"> <description>This is some content generated from the routing XSL</description> </cheese> </jbi:invoke> </xsl:when> <xsl:when test="@id != '2'"> <jbi:forward service="foo:receiver"/> </xsl:when> <xsl:otherwise> <jbi:forward service="foo:trace"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
In the above example, depending on the value of the @id attribute, we invoke different services. Notice that we declare the jbi namespace and some attributes on the root XSLT stylesheet element. We then have the following extension elements available to the XSTL Xalan extension summaryThe following new XML tags are available in Xalan when working with ServiceMix
Invoking multiple service endpointsThe following example shows a number of services being invoked one after the other, using optionally different properties. In each case the original XML message is used as the body of the new inbound request. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:jbi="xalan://org.apache.servicemix.components.xslt.XalanExtension" extension-element-prefixes="jbi" xmlns:foo="http://servicemix.org/cheese/" version="1.0"> <xsl:strip-space elements="*"/> <xsl:template match="/"> <!-- lets invoke a number of services one after the other --> <jbi:invoke service="foo:service1"> <jbi:copyProperties/> <jbi:setOutProperty name="foo" select="/sample"/> <xsl:copy-of select="/"/> </jbi:invoke> <jbi:invoke service="foo:service2"> <jbi:copyProperties/> <jbi:setOutProperty name="bar" select="/sample/@id"/> <xsl:copy-of select="/"/> </jbi:invoke> <jbi:invoke service="foo:service3"> <jbi:copyProperties/> <jbi:setOutProperty name="foo" select="string(/sample)"/> <jbi:setOutProperty name="bar" select="string(/sample/@id)"/> <xsl:copy-of select="/"/> </jbi:invoke> <jbi:invoke service="foo:receiver"> <jbi:copyProperties/> <jbi:setOutProperty name="bar" select="/sample/@id"/> <xsl:copy-of select="/"/> </jbi:invoke> </xsl:template> </xsl:stylesheet>
Splitting up an XML message into multiple service invocationsThis example demonstrates a message being split into multiple different sections of XML which are then invoked on different services. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:jbi="xalan://org.apache.servicemix.components.xslt.XalanExtension" extension-element-prefixes="jbi" xmlns:foo="http://servicemix.org/cheese/" version="1.0"> <xsl:template match="/"> <!-- lets pass a new message body --> <jbi:invoke service="foo:service1"> <cheese id="{/foo/@id}">Edam</cheese> </jbi:invoke> <!-- lets split the message --> <jbi:invoke service="foo:service2"> <xsl:copy-of select="/foo/beer"/> </jbi:invoke> <!-- 1-many split --> <xsl:for-each select="/foo/lineitem"> <jbi:invoke service="foo:service3"> <xsl:copy-of select="."/> </jbi:invoke> </xsl:for-each> <!-- pass the entire message to the final endpoint --> <jbi:invoke service="foo:receiver"> <jbi:copyProperties/> <jbi:setOutProperty name="bar" select="/sample/@id"/> <xsl:copy-of select="/"/> </jbi:invoke> </xsl:template> </xsl:stylesheet>
Why use Xalan?We use Xalan to implement our XPath based router and transformation engine. The main reasons for doing so are
|
Unsubscribe or edit your notifications preferences
