Isranis
Isranis

Reputation: 103

extract particular xml nodes from xml list in mulesoft

i have a below xml request

<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>

And i want to extract below xml from above 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>

**I dont want to use dataweave , how can achieve this logic thru xpath expression and let me any other way do to it **

Upvotes: 1

Views: 677

Answers (1)

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

Pls note your input xml is invalid, pls fix the root & base tag to make it valid

#Solution1 Since you are not ready to use Dataweave here is another solution below using xslt transformer and XPath as you required:-:-

   <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>

Put the transform.xsl under src/main/resource location :

<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: Using XPath3 and and for-each within the flow:

<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(&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;&quot;, &quot;&quot;)]" level="INFO" doc:name="Logger"/>
             <set-payload value="#[payload.replace(&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;&quot;, &quot;&quot;)]" 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>

Both cases, the response will be as follows:
enter image description here

Upvotes: 2

Related Questions