2015-07-22 63 views
1

我有一個騾流是試圖合併2 xml到一個單一的xml作爲有效載荷傳遞給一個休息API調用。 XML1 -合併2 xmls到一個單一的xslt在一個騾流

<?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> 
 
\t <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> 
 
\t <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的情況下在騾流中執行此操作?

+3

「*有人可以請幫助我的XSLT文件,因爲我不能夠創建它。*」我們來這裏是爲了幫助你的編碼 - 不爲你做。 –

+0

你的騾子流哪裏?讓我們看看這2個xml有效載荷如何進入您的流程。請分享您的Mule配置 –

回答

0

您可以嘗試以下步驟: -
1.Parse的XML1XPATH3,所有的節點值存儲在變量,如下列: -

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

....等等

  • 執行相同的XML2,並存儲在變量中的所有節點值:
  • <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"/>

    等等,直到你提取並存儲所有的變量值。

  • 現在,在你的騾流把XSLT轉換和傳遞變量值到你的XSLT這樣的: -
  • <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 togather與值

    0

    嘗試使用xsl:copy-of。 這樣的 -

    <?xml version="1.0" encoding="UTF-8"?> 
     
    
     
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
     
        \t <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
     
        \t <!-- Getting values from Mule variables --> 
     
        \t <xsl:param name="xml1" /> 
     
        \t <xsl:param name="xml2" /> 
     
    
     
        \t <xsl:template match="/"> 
     
        \t  <ROOT-Application> 
     
    \t \t <xsl:copy> 
     
    \t  \t \t <xsl:copy-of select="document($xml1)" /> 
     
    \t  \t \t <xsl:copy-of select="document($xml2)" /> 
     
    \t  \t </xsl:copy> 
     
    \t  </ROOT-Application> 
     
    \t </xsl:template> 
     
    </xsl:stylesheet>