xml - Azure 集成帐户( map - XSLT)- 使用逻辑应用将 2 个不同的 XML 合并为单个 xml

标签 xml azure xslt transformation azure-logic-apps

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

要求 - 我需要使用集成帐户中存储的 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>

最佳答案

首先我认为你最好将两个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 资源。

希望对您的问题有所帮助~

关于xml - Azure 集成帐户( map - XSLT)- 使用逻辑应用将 2 个不同的 XML 合并为单个 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58647618/

相关文章:

xml - .net 3.5 中的 DtdProcessing.Ignore

xslt - 将 XSL 函数放在 XSL 文档中的什么位置?

xml - 如何使用 xslt 将 <configSections> 添加到 web.config

.net - SQL Azure : SSL Provider, 错误:0 - 等待操作超时

sql-server - 将数千行数据插入 Azure SQL DB 的最高效方法?

azure - 如何防止在 WebJob 处理时重复消息不插入到服务总线队列中?

xslt - 您使用什么 XSLT 来格式化 CruiseControl.Net 中的 MsBuild XML 输出?

xml - 将 KML 导入 Google map 后,点显示在南极洲

java - 在 Java 中将外部 XML 解析为 JSON?

java - AspectJ - 来自外部 JAR 的方面