1

I have a Mule flow that is attempting to merge 2 xmls into a single xml to be passed as a payload to a rest api call. xml1 --

<?xml version="1.0" encoding="utf-8"?>
  <ProviderInformation>
  <Provider_Name>
    <FirstName/>
    <LastName/>
    <Title/>
    </Provider_Name>
    <DBA>
      <Using_DBA/>
      <DBA_Name/>
      <Years_DBA/>
      <Months_DBA/>
      <Using_Former_DBA/>
      <Former_DBA_Name/>
      <Years_Former_DBA/>
    </DBA>
    <TIN>
    <SSN/>
    <Using_EIN/>
    <EIN/>
    <NPI/>
    <Ownership_Type/>
    <Gender/>
    <DOB/>
    <Provider_Email_Address/>
    <Provider_Email_Address_Verify/>
    <Practice_Website_Address/>
    </TIN>     
 </ProviderInformation>

xml2 --

<?xml version="1.0" encoding="utf-8"?>
<Medicare>
    <Enrolled_In_Medicaid/>
    <Medicare_ID/>
    <Medicare_NPI/>
    <Medicare_Enrollment_Date></Medicare_Enrollment_Date>
	<Using_Other_Medicaid_CHIP/>
    <Other_Medicaid_ID state="SC">
      <Other_Medicaid_ID/>
      <Other_Medicaid_NPI/>
      <Other_medicaid_Effective_Date></Other_medicaid_Effective_Date>
        <Other_Medicaid_Enrollment_Date></Other_Medicaid_Enrollment_Date>
      <Other_Medicaid_State/>
    </Other_Medicaid_ID>
    <Other_Medicaid_ID state="GA">
      <Other_Medicaid_ID/>
      <Other_Medicaid_NPI/>
      <Other_medicaid_Effective_Date></Other_medicaid_Effective_Date>
         <Other_Medicaid_Enrollment_Date></Other_Medicaid_Enrollment_Date>
      <Other_Medicaid_State/>
    </Other_Medicaid_ID>
</Medicare>

output --

<?xml version="1.0" encoding="utf-8"?>
<ROOT-Application>
  <ProviderInformation>
  <Provider_Name>
    <FirstName/>
    <LastName/>
    <Title/>
    </Provider_Name>
    <DBA>
      <Using_DBA/>
      <DBA_Name/>
      <Years_DBA/>
      <Months_DBA/>
      <Using_Former_DBA/>
      <Former_DBA_Name/>
      <Years_Former_DBA/>
    </DBA>
    <TIN>
    <SSN/>
    <Using_EIN/>
    <EIN/>
    <NPI/>
    <Ownership_Type/>
    <Gender/>
    <DOB/>
    <Provider_Email_Address/>
    <Provider_Email_Address_Verify/>
    <Practice_Website_Address/>
    </TIN>
</ProviderInformation>
   <Medicare>
    <Enrolled_In_Medicaid/>
    <Medicare_ID/>
    <Medicare_NPI/>
    <Medicare_Enrollment_Date></Medicare_Enrollment_Date>
	<Using_Other_Medicaid_CHIP/>
    <Other_Medicaid_ID state="SC">
      <Other_Medicaid_ID/>
      <Other_Medicaid_NPI/>
      <Other_medicaid_Effective_Date></Other_medicaid_Effective_Date>
       <Other_Medicaid_Enrollment_Date></Other_Medicaid_Enrollment_Date>
      <Other_Medicaid_State/>
    </Other_Medicaid_ID>
    <Other_Medicaid_ID state="GA">
      <Other_Medicaid_ID/>
      <Other_Medicaid_NPI/>
      <Other_medicaid_Effective_Date></Other_medicaid_Effective_Date>
      <Other_Medicaid_Enrollment_Date></Other_Medicaid_Enrollment_Date>
      <Other_Medicaid_State/>
    </Other_Medicaid_ID>
   </Medicare>
</ROOT-Application>
I think one way of doing it is using xslt. Can someone pls help me with the xslt file as I am not able to create it. Or is there any other way to do this in a mule flow without using xslt ?

2
  • 3
    "Can someone pls help me with the xslt file as I am not able to create it." We are here to help you with your coding - not to do it for you. Jul 22, 2015 at 7:46
  • where is your Mule flow ? let us see how these 2 xml payload are coming into your flow .. Pls share your Mule config Jul 22, 2015 at 8:11

2 Answers 2

0

You can try following steps:-
1.Parse the XML1 with XPATH3 and store all the node values in variables like the following :-

<set-variable variableName="FirstName" value="#[xpath3('//ProviderInformation/Provider_Name/FirstName')]" doc:name="Variable"/>
<set-variable variableName="LastName" value="#[xpath3('//ProviderInformation/Provider_Name/LastName')]" doc:name="Variable"/>

.... and so on

  1. Do the same for XML2, and store all the node value in variables :

<set-variable variableName="Medicare_ID" value="#[xpath3('//Medicare/Enrolled_In_Medicaid/Medicare_ID')]" doc:name="Variable"/> <set-variable variableName="Medicare_NPI" value="#[xpath3('//Medicare/Enrolled_In_Medicaid/Medicare_NPI')]" doc:name="Variable"/>

and so on till you extract and store all the values in variables.

  1. Now in your Mule flow put the XSLT transformer and pass the variable values to your XSLT like the following :-

<mulexml:xslt-transformer name="mergeXml" xsl-file="Transform.xsl" outputEncoding="UTF-8" encoding="UTF-8" maxIdleTransformers="2" maxActiveTransformers="5" returnClass="java.lang.String" doc:name="XSLT"> <mulexml:context-property key="FirstName" value="#[flowVars.FirstName]"/> <!-- Passing the variables in XSLT to produce the XML dynamically --> <mulexml:context-property key="LastName" value="#[flowVars.LastName]"/> <!-- Passing the variables in XSLT to produce the XML dynamically --> <mulexml:context-property key="Medicare_ID" value="#[flowVars.Medicare_ID]"/> <!-- Passing the variables in XSLT to produce the XML dynamically --> <mulexml:context-property key="Medicare_NPI" value="#[flowVars.Medicare_NPI]"/> <!-- Passing the variables in XSLT to produce the XML dynamically --> </mulexml:xslt-transformer>

Please note, you need to pass all the variables that you have used to store the extracted value.. Here I have shown you only 4 as an example ....

Now, in your Transform.xsl :-

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
   <!-- Getting values from Mule variables -->
   <xsl:param name="FirstName"/>
   <xsl:param name="LastName"/>
   <xsl:param name="Medicare_ID"/>
   <xsl:param name="Medicare_NPI"/>
  <!-- Add all the variables  -->
<xsl:template match="/">
 <ROOT-Application>
  <ProviderInformation>
  <Provider_Name>
    <FirstName><xsl:value-of select="$FirstName"/></FirstName>
    <LastName><xsl:value-of select="$LastName"/></LastName>
    <Title/>
    </Provider_Name>
    <DBA>
      <Using_DBA/>
      <DBA_Name/>
      <Years_DBA/>
      <Months_DBA/>
      <Using_Former_DBA/>
      <Former_DBA_Name/>
      <Years_Former_DBA/>
    </DBA>
    <TIN>
    <SSN/>
    <Using_EIN/>
    <EIN/>
    <NPI/>
    <Ownership_Type/>
    <Gender/>
    <DOB/>
    <Provider_Email_Address/>
    <Provider_Email_Address_Verify/>
    <Practice_Website_Address/>
    </TIN>
</ProviderInformation>
   <Medicare>
    <Enrolled_In_Medicaid/>
    <Medicare_ID><xsl:value-of select="$Medicare_ID"/></Medicare_ID>
    <Medicare_NPI><xsl:value-of select="$Medicare_NPI"/></Medicare_NPI>
    <Medicare_Enrollment_Date></Medicare_Enrollment_Date>
    <Using_Other_Medicaid_CHIP/>
    <Other_Medicaid_ID state="SC">
      <Other_Medicaid_ID/>
      <Other_Medicaid_NPI/>
      <Other_medicaid_Effective_Date></Other_medicaid_Effective_Date>
       <Other_Medicaid_Enrollment_Date></Other_Medicaid_Enrollment_Date>
      <Other_Medicaid_State/>
    </Other_Medicaid_ID>
    <Other_Medicaid_ID state="GA">
      <Other_Medicaid_ID/>
      <Other_Medicaid_NPI/>
      <Other_medicaid_Effective_Date></Other_medicaid_Effective_Date>
      <Other_Medicaid_Enrollment_Date></Other_Medicaid_Enrollment_Date>
      <Other_Medicaid_State/>
    </Other_Medicaid_ID>
   </Medicare>
</ROOT-Application>
</xsl:template>
</xsl:stylesheet>  

Please note that you need to fill all the node values from the variables.. I have only shown you just 4 variables ( FirstName,LastName, Medicare_ID, Medicare_NPI ) here as an example. This will merge both your XML togather with values

0

Try using xsl:copy-of. Like this --

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  	<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
   	<!-- Getting values from Mule variables -->
   	<xsl:param name="xml1" />
   	<xsl:param name="xml2" />

   	<xsl:template match="/">
  	     <ROOT-Application>  
		 <xsl:copy>
	    		<xsl:copy-of select="document($xml1)" />
	    		<xsl:copy-of select="document($xml2)" /> 
	    	</xsl:copy>
	     </ROOT-Application>
	</xsl:template>
</xsl:stylesheet> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.