逻辑应用jsonpath表达式根据另一个子子值更新子子节点

问题描述 投票:0回答:2

我在逻辑应用程序中有一个 XML,如下所示:

<JournalCollection>
    <Journal>
        <ChangeCode>
            <Code>a1</Code>
        </ChangeCode>
    </Journal>
    <Journal>
        <ChangeCode>
            <Code>b1</Code>
        </ChangeCode>
        <Description>Description</Description>
    </Journal>
    <Journal>
        <ChangeCode>
            <Code>c1</Code>
        </ChangeCode>
    </Journal>
</JournalCollection>

我只需要为代码 = b1 添加“描述”字段。我在变量中拥有该位置,但我无法使用 json 路径表达式,因为我发现逻辑应用程序不支持这一点:

在 json 路径表达式中添加变量来设置位置值会引发此错误:

我还尝试使用转换 XML 形状,但由于我需要检查目标架构中子元素的位置,因此我无法更新它。

有什么方法可以根据逻辑应用程序中的另一个子子节点值来更新子子节点的 XML?

xpath azure-logic-apps jsonpath azure-logic-app-standard updatexml
2个回答
0
投票
Java(或我知道的任何语言)本身不支持 JSON 路径。您需要一个 JSON 路径处理器,例如

Jayway。您可能需要以字符串形式向库提供路径,然后它将解析该路径并为您提供您正在寻找的结果。

不过,我不确定如何在 Azure 逻辑应用程序的上下文中包含外部库。


0
投票
您可以在

高级数据操作连接器中使用C# 脚本执行操作。

https://statesolutions.com.au/c-script-execute/

就像JavaScript操作,但显然它使用C#,体验有点不同。

这是剧本...

// Load the XML into an XmlDocument var xmlDoc = new System.Xml.XmlDocument(); xmlDoc.LoadXml(parameters.xml); // Find the <Journal> elements System.Xml.XmlNodeList journalNodes = xmlDoc.SelectNodes("//Journal"); // Loop through each <Journal> element foreach (System.Xml.XmlNode journalNode in journalNodes) { // Find the <ChangeCode> element var changeCodeNode = journalNode.SelectSingleNode("ChangeCode"); // Find the <Code> element under <ChangeCode> var codeNode = changeCodeNode.SelectSingleNode("Code"); // Check if the value of <Code> is "b1" if (codeNode.InnerText == "b1") { // Create a new <Description> element var descriptionElement = xmlDoc.CreateElement("Description"); descriptionElement.InnerText = "Description"; // Insert the <Description> element as a sibling to <ChangeCode> journalNode.InsertAfter(descriptionElement, changeCodeNode); } } // Display the modified XML return xmlDoc.OuterXml;
...除此之外,我还创建了一个名为 

XML

 的变量,用于存储不带 
<Description>
 元素的 XML 负载。然后,我将该值传递到 C# 脚本执行操作的 
Parameters
 ...

{ "xml": @{variables('XML')} }
...这是配置为运行时的示例...

结果

如您所见,返回值为您提供了您想要的 XML(作为字符串)...

{ "returnValue": "<JournalCollection><Journal><ChangeCode><Code>a1</Code></ChangeCode></Journal><Journal><ChangeCode><Code>b1</Code></ChangeCode><Description>Description</Description></Journal><Journal><ChangeCode><Code>c1</Code></ChangeCode></Journal></JournalCollection>", "log": [] }
当然,如果需要的话,可以将脚本更改为更加通用/宽容,它很有针对性,但这是一种非常有效的方法。

© www.soinside.com 2019 - 2024. All rights reserved.