Azure集成帐户(Maps-XSLT)-使用Logic Apps将2种不同的XML合并为单个xml

时间:2019-10-31 16:28:53

标签: xml azure xslt transformation azure-logic-apps

在Azure Logic应用中,我有2个不同 JSON格式的xml文件Parent.xml和Children.xml。

要求- 我需要使用集成帐户中存储的XSLT将这2个文件合并到单个Family.xml文件中。

**在使用内置C#代码创建XSLT(用于自定义代码逻辑)时需要帮助。必须将此XSLT放置在Azure集成帐户的地图中。 **

以下是示例:-

Parent.xml

    <ParentRoot>
     <ParentName>
      <Father>
        <FirstName>FFirstName</FirstName>
        <LastName>FLastName</LastName>
        <Age>35</Age>
      </Father>
      <Mother>
        <FirstName>MFirstName</FirstName>
        <LastName>MLastName</LastName>
        <Age>33</Age>
      </Mother>
     </ParentName>            
    </ParentRoot>

Children.xml

<ChildrenRoot>
<Child>
    <FirstName>Child1FirstName</FirstName>
    <LastName>Child1LastName</LastName>
    <Age>14</Age>
</Child>
<Child>
    <FirstName>Child2FirstName</FirstName>
    <LastName>Child2LastName</LastName>
    <Age>10</Age>
</Child>
</ChildrenRoot>

Family.xml

<FamilyRoot>
  <ParentName>
          <Father>
            <FirstName>FFirstName</FirstName>
            <LastName>FLastName</LastName>
            <Age>35</Age>
          </Father>
          <Mother>
            <FirstName>MFirstName</FirstName>
            <LastName>MLastName</LastName>
            <Age>33</Age>
          </Mother>
  </ParentName>
<TotalChildren>2</TotalChildren>
<ChildrenCollection>
    <Child>
        <FirstName>Child1FirstName</FirstName>
        <LastName>Child1LastName</LastName>
        <Age>14</Age>
    </Child>
    <Child>
        <FirstName>Child2FirstName</FirstName>
        <LastName>Child2LastName</LastName>
        <Age>10</Age>             
    </Child>
</ChildrenCollection>     
</FamilyRoot>

1 个答案:

答案 0 :(得分:0)

首先,我认为您最好将两个xml资源合并为一个xml,作为逻辑应用程序中的资源,如下所示:

<root>
    <ParentRoot>
         <ParentName>
              <Father>
                <FirstName>FFirstName</FirstName>
                <LastName>FLastName</LastName>
                <Age>35</Age>
              </Father>
              <Mother>
                <FirstName>MFirstName</FirstName>
                <LastName>MLastName</LastName>
                <Age>33</Age>
              </Mother>
         </ParentName>            
    </ParentRoot>

    <ChildrenRoot>
        <Child>
            <FirstName>Child1FirstName</FirstName>
            <LastName>Child1LastName</LastName>
            <Age>14</Age>
        </Child>
        <Child>
            <FirstName>Child2FirstName</FirstName>
            <LastName>Child2LastName</LastName>
            <Age>10</Age>
        </Child>
    </ChildrenRoot>
</root>

然后,我编写一个xsl代码以转换xml供您参考:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="/">
        <xsl:element name="FamilyRoot">
            <xsl:element name="ParentName">
                <xsl:element name="Father">
                    <xsl:element name="FirstName">
                        <xsl:value-of select="root/ParentRoot/ParentName/Father/FirstName" />
                    </xsl:element>
                    <xsl:element name="LastName">
                        <xsl:value-of select="root/ParentRoot/ParentName/Father/LastName" />
                    </xsl:element>
                    <xsl:element name="Age">
                        <xsl:value-of select="root/ParentRoot/ParentName/Father/Age" />
                    </xsl:element>
                </xsl:element>
                <xsl:element name="Mother">
                    <xsl:element name="FirstName">
                        <xsl:value-of select="root/ParentRoot/ParentName/Father/FirstName" />
                    </xsl:element>
                    <xsl:element name="LastName">
                        <xsl:value-of select="root/ParentRoot/ParentName/Father/LastName" />
                    </xsl:element>
                    <xsl:element name="Age">
                        <xsl:value-of select="root/ParentRoot/ParentName/Father/Age" />
                    </xsl:element>
                </xsl:element>
            </xsl:element>

            <xsl:element name="TotalChildren">
                <xsl:value-of disable-output-escaping="yes" select="count(//Child)"/>
            </xsl:element>

            <xsl:element name="ChildrenCollection">
                <xsl:for-each select="root/ChildrenRoot/Child">
                    <xsl:element name="Child">
                        <xsl:element name="FirstName">
                            <xsl:value-of select="FirstName" />
                        </xsl:element>
                        <xsl:element name="LastName">
                            <xsl:value-of select="LastName" />
                        </xsl:element>
                        <xsl:element name="Age">
                            <xsl:value-of select="Age" />
                        </xsl:element>
                    </xsl:element>
                </xsl:for-each>
            </xsl:element>

        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

之后,我们可以将上面的xsl代码作为映射上传到集成帐户,并在逻辑应用中使用“ Transform XML”操作来转换xml资源。

希望对您的问题有帮助〜