我有一个xml以下的请求
<root>
<base>
<status>
<type>A</type>
<set>B</set>
</status>
<status>
<type>A</type>
<set>B</set>
</status>
<status>
<type>A</type>
<set>B</set>
</status>
</root>
</base>
我想从上面的xml中提取xml
<status>
<type>A</type>
<set>B</set>
</status>
<status>
<type>A</type>
<set>B</set>
</status>
<status>
<type>A</type>
<set>B</set>
</status>
**我不想使用dataweave,如何通过xpath表达式实现这个逻辑,让我以任何其他方式做到这一点**
答案 0 :(得分:2)
请注意您的输入xml无效,请修复root&amp;基本标签使其有效
#Solution1 由于未准备好使用 Dataweave ,下面是使用 xslt 转换器的另一种解决方案根据需要添加XPath: - : -
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<logger message="input xml:- #[payload]" level="INFO" doc:name="Logger"/>
<mulexml:xslt-transformer xsl-file="transform.xsl" maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT" mimeType="application/xml">
</mulexml:xslt-transformer>
</flow>
将transform.xsl放在src/main/resource
位置:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:for-each select="root/base/status">
<status>
<type>
<xsl:value-of select="type" />
</type>
<set>
<xsl:value-of select="set" />
</set>
</status>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
#Solution2:在流程中使用 XPath3 和for-each
:
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<expression-component doc:name="Expression">
<![CDATA[
StringBuffer sb = new StringBuffer();
flowVars.stBuffer=sb;
]]>
</expression-component>
<foreach collection="#[xpath3('//root/base/status', message.payload, 'NODESET')]" doc:name="For Each">
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
<logger message="#[payload.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "")]" level="INFO" doc:name="Logger"/>
<set-payload value="#[payload.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "")]" doc:name="Set Payload"/>
<set-payload value="#[flowVars.stBuffer.append(message.payload).toString()]" doc:name="Set Payload"/>
</foreach>
<set-payload value="#[flowVars.stBuffer]" doc:name="Set Payload" mimeType="application/xml"/>
<mulexml:dom-to-xml-transformer doc:name="DOM to XML"/>
</flow>