英文:
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.
"<?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>"
答案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:
"680"
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:
{
"externalId": "680",
"internalId": "1426",
"type": "listRel:Customer"
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论