在带有 Enterprise Integration Pack 的 Azure 逻辑应用中创建用于在格式之间转换 XML 的映射Create maps that transform XML between formats in Azure Logic Apps with Enterprise Integration Pack

企业集成转换连接器可将数据从一种格式转换为另一种格式。The Enterprise integration Transform connector converts data from one format to another format. 例如,传入消息可能包含 YearMonthDay 格式的当前日期。For example, you may have an incoming message that contains the current date in the YearMonthDay format. 可以使用转换将日期格式重新设置为 MonthDayYear 格式。You can use a transform to reformat the date to be in the MonthDayYear format.

转换的作用是什么?What does a transform do?

转换(也称为映射)由源 XML 架构(输入)和目标 XML 架构(输出)组成。A Transform, which is also known as a map, consists of a Source XML schema (the input) and a Target XML schema (the output). 可以使用不同的内置函数帮助操作或控制数据,包括字符串操作、条件分配、算术表达式、日期时间格式化程序甚至是循环构造。You can use different built-in functions to help manipulate or control the data, including string manipulations, conditional assignments, arithmetic expressions, date time formatters, and even looping constructs.

如何创建转换?How to create a transform?

可以使用 Visual Studio 企业集成 SDK 创建转换/映射。You can create a transform/map by using the Visual Studio Enterprise Integration SDK. 完成转换的创建和测试之后,需将转换上传到集成帐户中。When you are finished creating and testing the transform, you upload the transform into your integration account.

如何使用转换How to use a transform

将转换或映射上传到集成帐户中之后,可以使用它创建逻辑应用。After you upload the transform/map into your integration account, you can use it to create a Logic app. 逻辑应用随后会在每次触发逻辑应用(以及存在需要进行转换的输入内容)时运行转换。The Logic app runs your transformations whenever the Logic app is triggered (and there is input content that needs to be transformed).

下面是使用转换的步骤Here are the steps to use a transform:

必备条件Prerequisites

  • 创建集成帐户并向其中添加映射Create an integration account and add a map to it

现在已满足了先决条件,可以创建逻辑应用了:Now that you've taken care of the prerequisites, it's time to create your Logic app:

  1. 创建一个逻辑应用并将其链接到包含映射的集成帐户Create a Logic app and link it to your integration account that contains the map.

  2. 将“请求” 触发器添加到逻辑应用Add a Request trigger to your Logic app

  3. 通过首先选择“添加操作”来添加“转换 XML”操作 Add the Transform XML action by first selecting Add an action

  4. 在搜索框中输入“转换” 一词,以便在所有操作中筛选出要使用的操作Enter the word transform in the search box to filter all the actions to the one that you want to use

  5. 选择“转换 XML” 操作Select the Transform XML action

  6. 添加转换的 XML“内容” 。Add the XML CONTENT that you transform. 可使用在 HTTP 请求中收到的任何 XML 数据作为“内容” 。You can use any XML data you receive in the HTTP request as the CONTENT. 在此示例中,选择触发了逻辑应用的 HTTP 请求的正文。In this example, select the body of the HTTP request that triggered the Logic app.

    备注

    请确保“转换 XML” 的内容是 XML。Make sure that the content for the Transform XML is XML. 如果内容不是 XML 格式或采用 base64 编码,则必须指定用于处理该内容的表达式。If the content is not in XML or is base64-encoded, you must specify an expression that processes the content. 例如,可以使用函数(如 @base64ToBinary)来解码内容,或使用 @xml 来处理 XML 格式的内容。For example, you can use functions, like @base64ToBinary for decoding content or @xml for processing the content as XML.

  7. 选择要用于执行转换的“映射” 的名称。Select the name of the MAP that you want to use to perform the transformation. 该映射必须已处于集成帐户中。The map must already be in your integration account. 在前面的步骤中,已向逻辑应用授予了对包含映射的集成帐户的访问权限。In an earlier step, you already gave your Logic app access to your integration account that contains your map.

  8. 保存工作Save your work

此时,已完成映射设置。At this point, you are finished setting up your map. 在实际应用程序中,可能要将转换的数据存储在 LOB 应用程序中,如 SalesForce。In a real world application, you may want to store the transformed data in an LOB application such as SalesForce. 可以轻松地添加操作以将转换的输出发送给 Salesforce。You can easily as an action to send the output of the transform to Salesforce.

现在可以通过向 HTTP 终结点发出请求来测试转换。You can now test your transform by making a request to the HTTP endpoint.

功能和用例Features and use cases

  • 在映射中创建的转换可以非常简单,如将名称和地址从一个文档复制到另一个文档。The transformation created in a map can be simple, such as copying a name and address from one document to another. 或者,可以使用现成可用的映射操作创建更复杂的转换。Or, you can create more complex transformations using the out-of-the-box map operations.
  • 有多个映射操作或函数现成可用,包括字符串、日期时间函数等等。Multiple map operations or functions are readily available, including strings, date time functions, and so on.
  • 可以在架构之间执行直接数据复制。You can do a direct data copy between the schemas. 在 SDK 中包含的映射程序中,这简单到只需绘制一条线将源架构中的元素与目标架构中的对应项连接。In the Mapper included in the SDK, this is as simple as drawing a line that connects the elements in the source schema with their counterparts in the destination schema.
  • 创建映射时,可查看映射的图形表示形式,其中会显示创建的所有关系和链接。When creating a map, you view a graphical representation of the map, which shows all the relationships and links you create.
  • 使用“测试映射”功能可添加示例 XML 消息。Use the Test Map feature to add a sample XML message. 只需进行简单的单击,便可以测试创建的映射,并查看生成的输出。With a simple click, you can test the map you created, and see the generated output.
  • 上传现有映射Upload existing maps
  • 包括对 XML 格式的支持。Includes support for the XML format.

高级功能Advanced features

映射中的引用程序集或自定义代码Reference assembly or custom code from maps

转换操作还支持包含外部程序集引用的映射或转换。The transform action also supports maps or transforms with reference to external assembly. 此功能可直接从 XSLT 映射调用自定义 .NET 代码。This capability enables calls to custom .NET code directly from XSLT maps. 以下是在映射中使用程序集的先决条件。Here are the prerequisites to use assembly in maps.

  • 映射和从映射引用的程序集需要上传到集成帐户The map and the assembly referenced from the map needs to be uploaded to integration account.

    备注

    映射和程序集需按特定顺序上传。Map and assembly are required to be uploaded in a specific order. 上传引用程序集的映射之前,必须先上传程序集。You must upload the assembly before you upload the map that references the assembly.

  • 映射必须还具有这些属性和 CDATA 节,该节包含对程序集代码的调用:The map must also have these attributes and a CDATA section that contains the call to the assembly code:

    • “名称”是自定义程序集名称 。name is the custom assembly name.
    • “命名空间”是包含自定义代码的程序集中的命名空间 。namespace is the namespace in your assembly that includes the custom code.

    此示例演示了一个映射,该映射引用名为“XslUtilitiesLib”的程序集并从该程序集调用 circumreference 方法。This example shows a map that references an assembly named "XslUtilitiesLib" and calls the circumreference method from the assembly.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">
    <msxsl:script language="C#" implements-prefix="user">
        <msxsl:assembly name="XsltHelperLib"/>
        <msxsl:using namespace="XsltHelpers"/>
        <![CDATA[public double circumference(int radius){ XsltHelper helper = new XsltHelper(); return helper.circumference(radius); }]]>
    </msxsl:script>
    <xsl:template match="data">
        <circles>
            <xsl:for-each select="circle">
                <circle>
                    <xsl:copy-of select="node()"/>
                        <circumference>
                            <xsl:value-of select="user:circumference(radius)"/>
                        </circumference>
                </circle>
            </xsl:for-each>
        </circles>
    </xsl:template>
    </xsl:stylesheet>
    

字节顺序标记Byte Order Mark

默认情况下,转换的响应会以字节顺序标记 (BOM) 开头。By default, the response from the transformation starts with the Byte Order Mark (BOM). 仅在“代码视图”编辑器中工作时,才可访问此功能。You can access this functionality only while working in the Code View editor. 若要禁用此功能,请为 transformOptions 属性指定 disableByteOrderMarkTo disable this functionality, specify disableByteOrderMark for the transformOptions property:

"Transform_XML": {
    "inputs": {
        "content": "@{triggerBody()}",
        "integrationAccount": {
            "map": {
                "name": "TestMap"
            }
        },
        "transformOptions": "disableByteOrderMark"
    },
    "runAfter": {},
    "type": "Xslt"
}

了解详细信息Learn more