如何使用 DataWeave 从 XML 中提取属性

huangapple go评论51阅读模式
英文:

How to extract attributes from an XML using DataWeave

问题

I have a XML like this below and i want to extract the attributes (example: externalid) of it using DataWeave.

"<?xml version="1.0" encoding="UTF-8"?>
<platformCore:record xmlns="urn:messages.platform.webservices.netsuite.com" xmlns:listRel="urn:relationships.lists.webservices.netsuite.com" xmlns:platformCore="urn:core.platform.webservices.netsuite.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" externalId="680" internalId="1426" xsi:type="listRel:Customer">
    <listRel:entityId>CUST611 Comercial Vega</listRel:entityId>
    <listRel:isPerson>false</listRel:isPerson>
</platformCore:record>"
英文:

I have a XML like this below and i want to extract the attributes (example: externalid) of it using DataWeave.

&quot;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;platformCore:record xmlns=&quot;urn:messages.platform.webservices.netsuite.com&quot; xmlns:listRel=&quot;urn:relationships.lists.webservices.netsuite.com&quot; xmlns:platformCore=&quot;urn:core.platform.webservices.netsuite.com&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; externalId=&quot;680&quot; internalId=&quot;1426&quot; xsi:type=&quot;listRel:Customer&quot;&gt;
    &lt;listRel:entityId&gt;CUST611 Comercial Vega&lt;/listRel:entityId&gt;
    &lt;listRel:isPerson&gt;false&lt;/listRel:isPerson&gt;
&lt;/platformCore:record&gt;&quot;

答案1

得分: 1

使用属性选择器来获取键的属性。

使用属性名称,它将返回属性值:

%dw 2.0
output application/json
ns platformCore urn:core.platform.webservices.netsuite.com
---
payload.platformCore#record.@externalId

输出:

"680"

如果没有键名,它将返回具有该键的所有属性的名称和值的对象:

%dw 2.0
output application/json
ns platformCore urn:core.platform.webservices.netsuite.com
---
payload.platformCore#record.@

输出:

{
  "externalId": "680",
  "internalId": "1426",
  "type": "listRel:Customer"
}

我在输出中使用JSON格式以提高清晰度。如果您稍后要使用这些值,我建议使用输出application/java以避免重复解析。

英文:

Use the attribute selector on a key to get its attributes.

With an attribute name it will return the attribute value:

%dw 2.0
output application/json
ns platformCore urn:core.platform.webservices.netsuite.com
---
payload.platformCore#record.@externalId

Output:

&quot;680&quot;

Without a key name it returns an object with the names and values of all the attributes for that key:

%dw 2.0
output application/json
ns platformCore urn:core.platform.webservices.netsuite.com
---
payload.platformCore#record.@

Output:

{
  &quot;externalId&quot;: &quot;680&quot;,
  &quot;internalId&quot;: &quot;1426&quot;,
  &quot;type&quot;: &quot;listRel:Customer&quot;
}

I'm using JSON for the output for clarity. If you are using the values later I would recommend to use output application/java to avoid duplicated parsing.

huangapple
  • 本文由 发表于 2023年3月1日 14:02:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600065.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定