在骡子流中将2 xmls合并为xslt

时间:2015-07-22 07:26:34

标签: xml xslt mule

我有一个Mule流试图将2个xmls合并为一个xml,作为有效负载传递给rest api调用。 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>

输出 -

<?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>
我认为这样做的一种方法是使用xslt。 有人请帮助我使用xslt文件,因为我无法创建它。或者有没有其他方法在不使用xslt的情况下在骡子流中执行此操作?

2 个答案:

答案 0 :(得分:0)

您可以尝试以下步骤: -
1.使用XPATH3解析 XML1 ,并将所有节点值存储在如下变量中: -

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

....等等

  1. XML2 执行相同操作,并将所有节点值存储在变量中:
  2. <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"/>

    等等,直到您将所有值提取并存储在变量中。

    1. 现在在你的Mule流程中放入XSLT转换器并将变量值传递给你的XSLT,如下所示: -
    2. <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>

      请注意,您需要传递用于存储提取的值的所有变量。这里我只向您展示了4个作为示例....

      现在,在您的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>  
      

      请注意,您需要填充变量中的所有节点值。我这里仅展示了4个变量(FirstName,LastName,Medicare_ID,Medicare_NPI)作为示例。 这会将您的 XML 与值

      合并

答案 1 :(得分:0)

尝试使用xsl:copy-of。 像这样 -

&#13;
&#13;
<?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> 
&#13;
&#13;
&#13;

相关问题