![wso2](https://i-blog.csdnimg.cn/blog_migrate/b536a81fce4531165a21782916752897.png)
wso2
每个集成架构师或开发人员都应该熟悉Gregor Hohpe和Bobby Woolf所描述的企业集成模式(EIP) 。 模式之一是“内容消息过滤器” (不要与消息过滤器模式混淆)。 使用不同的Mediator在WSO2中有多种方法可以实现此目的。 一种方法是使用XSLT介体,您可以在其中简单地使用XSLT进行过滤。 另一个(根据其名称不太明显)是Enrich Mediator 。这是一个如何使用Enrich Mediator的示例。 想象一下原始消息是:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tmp="http://www.pascalalma.net/order">
<soapenv:Header/>
<soapenv:Body>
<tmp:message>
<tmp:document>
<tmp:order>
<tmp:id>123</tmp:id>
</tmp:order>
</tmp:document>
</tmp:message>
</soapenv:Body>
</soapenv:Envelope>
我们真正想要的是一条仅以“ order”元素作为有效载荷的Soap消息。 我们可以使用具有以下配置的Enrich介体来完成此操作:
<enrich xmlns:tmp="http://www.pascalalma.net/order">
<source clone="false" type="custom" xpath="//tmp:document/*" />
<target action="replace" type="body" />
</enrich>
因此,通过这种配置,我们告诉中介者应将'document'元素的内容作为源,并将此内容放入传入的SOAP消息的正文中。
当您选择使用XSLT介体时,这里有一个示例XSLT,可用于从XML文档中删除某些元素。 您可以在以下XML消息上使用它:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tmp="http://www.pascalalma.net/order">
<soapenv:Header/>
<soapenv:Body>
<tmp:message>
<tmp:document>
<tmp:order>
<tmp:id>123</tmp:id>
<tmp:type>backorder</tmp:type>
<tmp:status>open</tmp:status>
<tmp:description>open</tmp:description>
</tmp:order>
</tmp:document>
</tmp:message>
</soapenv:Body>
</soapenv:Envelope>
如果我们想要这个相同的XML文档,但没有元素'tmp:type'和'tmp:description',我们可以这样定义XSLT介体:
<xslt key="xslt/remove-elements-v1.xslt" description="remove unwanted elements">
<property name="removeElementsNamed" value="type,description" />
</xslt>
使这项工作有效的XSLT代码(我在stackoverflow网站上找到了它):
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:param name="removeElementsNamed" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[local-name()=tokenize($removeElementsNamed,'[\|, \t]')]"/>
</xsl:stylesheet>
请注意,此XSLT不考虑名称空间,只是删除所有本地名称与提供的名称匹配的元素!
翻译自: https://www.javacodegeeks.com/2015/03/message-content-filtering-with-wso2-esb.html
wso2